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.
This commit is contained in:
Linus Torvalds 2005-07-05 11:31:32 -07:00
Родитель 7a662e896b
Коммит 723c31fea2
7 изменённых файлов: 37 добавлений и 14 удалений

Просмотреть файл

@ -158,6 +158,7 @@ extern void rollback_index_file(struct cache_file *);
#define TYPE_CHANGED 0x0040 #define TYPE_CHANGED 0x0040
/* Return a statically allocated filename matching the sha1 signature */ /* Return a statically allocated filename matching the sha1 signature */
extern char *git_path(const char *fmt, ...);
extern char *sha1_file_name(const unsigned char *sha1); extern char *sha1_file_name(const unsigned char *sha1);
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */ /* Read and unpack a sha1 file into memory, write memory to a sha1 file */

Просмотреть файл

@ -72,13 +72,9 @@ static int find_common(int fd[2], unsigned char *result_sha1, unsigned char *rem
static int get_old_sha1(const char *refname, unsigned char *sha1) static int get_old_sha1(const char *refname, unsigned char *sha1)
{ {
static char pathname[PATH_MAX];
const char *git_dir;
int fd, ret; int fd, ret;
git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT; fd = open(git_path("%s", refname), O_RDONLY);
snprintf(pathname, sizeof(pathname), "%s/%s", git_dir, refname);
fd = open(pathname, O_RDONLY);
ret = -1; ret = -1;
if (fd >= 0) { if (fd >= 0) {
char buffer[60]; char buffer[60];

9
refs.c
Просмотреть файл

@ -68,6 +68,15 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
return retval; return retval;
} }
int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
{
unsigned char sha1[20];
const char *headpath = git_path("HEAD");
if (!read_ref(headpath, sha1))
fn(headpath, sha1);
return do_for_each_ref(get_refs_directory(), fn);
}
int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1)) int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
{ {
return do_for_each_ref(get_refs_directory(), fn); return do_for_each_ref(get_refs_directory(), fn);

1
refs.h
Просмотреть файл

@ -5,6 +5,7 @@
* Calls the specified function for each ref file until it returns nonzero, * Calls the specified function for each ref file until it returns nonzero,
* and returns the value * and returns the value
*/ */
extern int head_ref(int (*fn)(const char *path, const unsigned char *sha1));
extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1)); extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1));
/** Reads the refs file specified into sha1 **/ /** Reads the refs file specified into sha1 **/

Просмотреть файл

@ -92,12 +92,9 @@ static int pack_objects(int fd, struct ref *refs)
static int read_ref(const char *ref, unsigned char *sha1) static int read_ref(const char *ref, unsigned char *sha1)
{ {
int fd, ret; int fd, ret;
static char pathname[PATH_MAX];
char buffer[60]; char buffer[60];
const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
snprintf(pathname, sizeof(pathname), "%s/%s", git_dir, ref); fd = open(git_path("%s", ref), O_RDONLY);
fd = open(pathname, O_RDONLY);
if (fd < 0) if (fd < 0)
return -1; return -1;
ret = -1; ret = -1;

Просмотреть файл

@ -102,9 +102,30 @@ char *get_index_file(void)
return git_index_file; return git_index_file;
} }
int get_sha1(const char *str, unsigned char *sha1) char *git_path(const char *fmt, ...)
{ {
static char pathname[PATH_MAX]; 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[] = { static const char *prefix[] = {
"", "",
"refs", "refs",
@ -118,11 +139,8 @@ int get_sha1(const char *str, unsigned char *sha1)
if (!get_sha1_hex(str, sha1)) if (!get_sha1_hex(str, sha1))
return 0; return 0;
if (!git_dir)
setup_git_env();
for (p = prefix; *p; p++) { for (p = prefix; *p; p++) {
snprintf(pathname, sizeof(pathname), "%s/%s/%s", char * pathname = git_path("%s/%s", *p, str);
git_dir, *p, str);
if (!get_sha1_file(pathname, sha1)) if (!get_sha1_file(pathname, sha1))
return 0; return 0;
} }

Просмотреть файл

@ -153,6 +153,7 @@ static int send_ref(const char *refname, const unsigned char *sha1)
static int upload_pack(void) static int upload_pack(void)
{ {
head_ref(send_ref);
for_each_ref(send_ref); for_each_ref(send_ref);
packet_flush(1); packet_flush(1);
nr_needs = receive_needs(); nr_needs = receive_needs();