diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-05-01 16:36:56 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-05-01 16:36:56 -0700 |
commit | 3c249c950649a37f2871a8b193f01a0640a20aef (patch) | |
tree | 8ce5c715973ddb7ed553be593c090de979c4a5ce /sha1_file.c | |
parent | 68849b544258cafdf42f3ebe9772ee7a346f7147 (diff) | |
download | git-3c249c950649a37f2871a8b193f01a0640a20aef.tar.gz |
Add "get_sha1()" helper function.
This allows the programs to use various simplified versions of
the SHA1 names, eg just say "HEAD" for the SHA1 pointed to by
the .git/HEAD file etc.
For example, this commit has been done with
git-commit-tree $(git-write-tree) -p HEAD
instead of the traditional "$(cat .git/HEAD)" syntax.
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/sha1_file.c b/sha1_file.c index 29f48d56b1..d91e072f3e 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -45,6 +45,35 @@ int get_sha1_hex(const char *hex, unsigned char *sha1) return 0; } +int get_sha1_file(const char *path, unsigned char *result) +{ + char buffer[60]; + int fd = open(path, O_RDONLY); + int len; + + if (fd < 0) + return -1; + len = read(fd, buffer, sizeof(buffer)); + close(fd); + if (len < 40) + return -1; + return get_sha1_hex(buffer, result); +} + +int get_sha1(const char *str, unsigned char *sha1) +{ + static char pathname[PATH_MAX]; + + if (!get_sha1_hex(str, sha1)) + return 0; + if (!get_sha1_file(str, sha1)) + return 0; + snprintf(pathname, sizeof(pathname), ".git/%s", str); + if (!get_sha1_file(pathname, sha1)) + return 0; + return -1; +} + char * sha1_to_hex(const unsigned char *sha1) { static char buffer[50]; |