diff options
author | Linus Torvalds <torvalds@g5.osdl.org> | 2005-07-05 11:31:32 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2005-07-05 11:31:32 -0700 |
commit | 723c31fea2f1c4994de837bda9022ffa8b6de1bb (patch) | |
tree | 4c8c636dd58a7489cf033f3ac8682aeab453385d /sha1_file.c | |
parent | 7a662e896bcd391265477e304d7af6e5d2ca1deb (diff) | |
download | git-723c31fea2f1c4994de837bda9022ffa8b6de1bb.tar.gz git-723c31fea2f1c4994de837bda9022ffa8b6de1bb.tar.xz |
Add "git_path()" and "head_ref()" helper functions.
"git_path()" returns a static pathname pointer into the git directory
using a printf-like format specifier.
"head_ref()" works like "for_each_ref()", except for just the HEAD.
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/sha1_file.c b/sha1_file.c index 8f20e2f82..74dc2aab2 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -102,9 +102,30 @@ char *get_index_file(void) return git_index_file; } -int get_sha1(const char *str, unsigned char *sha1) +char *git_path(const char *fmt, ...) { static char pathname[PATH_MAX]; + va_list args; + int len; + + if (!git_dir) + setup_git_env(); + len = strlen(git_dir); + if (len == 1 && *git_dir == '.') + len = 0; + if (len > PATH_MAX-100) + return "pad-path"; + memcpy(pathname, git_dir, len); + if (len && git_dir[len-1] != '/') + pathname[len++] = '/'; + va_start(args, fmt); + vsnprintf(pathname + len, sizeof(pathname) - len, fmt, args); + va_end(args); + return pathname; +} + +int get_sha1(const char *str, unsigned char *sha1) +{ static const char *prefix[] = { "", "refs", @@ -118,11 +139,8 @@ int get_sha1(const char *str, unsigned char *sha1) if (!get_sha1_hex(str, sha1)) return 0; - if (!git_dir) - setup_git_env(); for (p = prefix; *p; p++) { - snprintf(pathname, sizeof(pathname), "%s/%s/%s", - git_dir, *p, str); + char * pathname = git_path("%s/%s", *p, str); if (!get_sha1_file(pathname, sha1)) return 0; } |