Make ref resolution saner
The old code used to totally mix up the notion of a ref-name and the path that that ref was associated with. That was not only horribly ugly (a number of users got the path, and then wanted to try to turn it back into a ref-name again), but it fundamnetally doesn't work at all once we do any setup where a ref doesn't have a 1:1 relationship with a particular pathname. This fixes things up so that we use the ref-name throughout, and only turn it into a pathname once we actually look it up in the filesystem. That makes a lot of things much clearer and more straightforward. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Sep 11, 2006 at 20:17 UTC
ed378ec7e85fd2c5cfcc7bd64b454236357fdd97
7 files changed
+74
-84
builtin-fmt-merge-msg.c
+2
-5
@@ -249,7 +249,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
249
FILE *in = stdin;
250
const char *sep = "";
251
unsigned char head_sha1[20];
252
- const char *head, *current_branch;
252
+ const char *current_branch;
253
254
git_config(fmt_merge_msg_config);
255
@@ -277,10 +277,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
277
usage(fmt_merge_msg_usage);
278
279
/* get current branch */
280
- head = xstrdup(git_path("HEAD"));
281
- current_branch = resolve_ref(head, head_sha1, 1);
282
- current_branch += strlen(head) - 4;
283
- free((char *)head);
280
+ current_branch = resolve_ref("HEAD", head_sha1, 1);
281
if (!strncmp(current_branch, "refs/heads/", 11))
282
current_branch += 11;
283
builtin-init-db.c
+2
-2
@@ -218,8 +218,8 @@ static void create_default_files(const char *git_dir, const char *template_path)
218
* branch, if it does not exist yet.
219
*/
220
strcpy(path + len, "HEAD");
221
- if (read_ref(path, sha1) < 0) {
222
- if (create_symref(path, "refs/heads/master") < 0)
221
+ if (read_ref("HEAD", sha1) < 0) {
222
+ if (create_symref("HEAD", "refs/heads/master") < 0)
223
exit(1);
224
}
225
builtin-show-branch.c
+19
-27
@@ -437,21 +437,13 @@ static void snarf_refs(int head, int tag)
437
}
438
}
439
440
-static int rev_is_head(char *head_path, int headlen, char *name,
440
+static int rev_is_head(char *head, int headlen, char *name,
441
unsigned char *head_sha1, unsigned char *sha1)
442
{
443
- int namelen;
444
- if ((!head_path[0]) ||
443
+ if ((!head[0]) ||
444
(head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
445
return 0;
447
- namelen = strlen(name);
448
- if ((headlen < namelen) ||
449
- memcmp(head_path + headlen - namelen, name, namelen))
450
- return 0;
451
- if (headlen == namelen ||
452
- head_path[headlen - namelen - 1] == '/')
453
- return 1;
454
- return 0;
446
+ return !strcmp(head, name);
447
}
448
449
static int show_merge_base(struct commit_list *seen, int num_rev)
@@ -559,9 +551,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
551
int all_heads = 0, all_tags = 0;
552
int all_mask, all_revs;
553
int lifo = 1;
562
- char head_path[128];
563
- const char *head_path_p;
564
- int head_path_len;
554
+ char head[128];
555
+ const char *head_p;
556
+ int head_len;
557
unsigned char head_sha1[20];
558
int merge_base = 0;
559
int independent = 0;
@@ -638,31 +630,31 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
630
ac--; av++;
631
}
632
641
- head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
642
- if (head_path_p) {
643
- head_path_len = strlen(head_path_p);
644
- memcpy(head_path, head_path_p, head_path_len + 1);
633
+ head_p = resolve_ref("HEAD", head_sha1, 1);
634
+ if (head_p) {
635
+ head_len = strlen(head_p);
636
+ memcpy(head, head_p, head_len + 1);
637
}
638
else {
647
- head_path_len = 0;
648
- head_path[0] = 0;
639
+ head_len = 0;
640
+ head[0] = 0;
641
}
642
651
- if (with_current_branch && head_path_p) {
643
+ if (with_current_branch && head_p) {
644
int has_head = 0;
645
for (i = 0; !has_head && i < ref_name_cnt; i++) {
646
/* We are only interested in adding the branch
647
* HEAD points at.
648
*/
657
- if (rev_is_head(head_path,
658
- head_path_len,
649
+ if (rev_is_head(head,
650
+ head_len,
651
ref_name[i],
652
head_sha1, NULL))
653
has_head++;
654
}
655
if (!has_head) {
664
- int pfxlen = strlen(git_path("refs/heads/"));
665
- append_one_rev(head_path + pfxlen);
656
+ int pfxlen = strlen("refs/heads/");
657
+ append_one_rev(head + pfxlen);
658
}
659
}
660
@@ -713,8 +705,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
705
if (1 < num_rev || extra < 0) {
706
for (i = 0; i < num_rev; i++) {
707
int j;
716
- int is_head = rev_is_head(head_path,
717
- head_path_len,
708
+ int is_head = rev_is_head(head,
709
+ head_len,
710
ref_name[i],
711
head_sha1,
712
rev[i]->object.sha1);
builtin-symbolic-ref.c
+5
-9
@@ -7,15 +7,11 @@ static const char git_symbolic_ref_usage[] =
7
static void check_symref(const char *HEAD)
8
{
9
unsigned char sha1[20];
10
- const char *git_HEAD = xstrdup(git_path("%s", HEAD));
11
- const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 0);
12
- if (git_refs_heads_master) {
13
- /* we want to strip the .git/ part */
14
- int pfxlen = strlen(git_HEAD) - strlen(HEAD);
15
- puts(git_refs_heads_master + pfxlen);
16
- }
17
- else
10
+ const char *refs_heads_master = resolve_ref("HEAD", sha1, 0);
11
+
12
+ if (!refs_heads_master)
13
die("No such ref: %s", HEAD);
14
+ puts(refs_heads_master);
15
}
16
17
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
@@ -26,7 +22,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
22
check_symref(argv[1]);
23
break;
24
case 3:
29
- create_symref(xstrdup(git_path("%s", argv[1])), argv[2]);
25
+ create_symref(argv[1], argv[2]);
26
break;
27
default:
28
usage(git_symbolic_ref_usage);
cache.h
+2
-2
@@ -287,8 +287,8 @@ extern int get_sha1_hex(const char *hex, unsigned char *sha1);
287
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
288
extern int read_ref(const char *filename, unsigned char *sha1);
289
extern const char *resolve_ref(const char *path, unsigned char *sha1, int);
290
-extern int create_symref(const char *git_HEAD, const char *refs_heads_master);
291
-extern int validate_symref(const char *git_HEAD);
290
+extern int create_symref(const char *ref, const char *refs_heads_master);
291
+extern int validate_symref(const char *ref);
292
293
extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
294
extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
refs.c
+37
-32
@@ -93,11 +93,11 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
93
if (dir) {
94
struct dirent *de;
95
int baselen = strlen(base);
96
- char *path = xmalloc(baselen + 257);
96
+ char *ref = xmalloc(baselen + 257);
97
98
- memcpy(path, base, baselen);
98
+ memcpy(ref, base, baselen);
99
if (baselen && base[baselen-1] != '/')
100
- path[baselen++] = '/';
100
+ ref[baselen++] = '/';
101
102
while ((de = readdir(dir)) != NULL) {
103
unsigned char sha1[20];
@@ -111,20 +111,20 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
111
continue;
112
if (has_extension(de->d_name, ".lock"))
113
continue;
114
- memcpy(path + baselen, de->d_name, namelen+1);
115
- if (stat(git_path("%s", path), &st) < 0)
114
+ memcpy(ref + baselen, de->d_name, namelen+1);
115
+ if (stat(git_path("%s", ref), &st) < 0)
116
continue;
117
if (S_ISDIR(st.st_mode)) {
118
- list = get_ref_dir(path, list);
118
+ list = get_ref_dir(ref, list);
119
continue;
120
}
121
- if (read_ref(git_path("%s", path), sha1) < 0) {
122
- error("%s points nowhere!", path);
121
+ if (read_ref(ref, sha1) < 0) {
122
+ error("%s points nowhere!", ref);
123
continue;
124
}
125
- list = add_ref(path, sha1, list);
125
+ list = add_ref(ref, sha1, list);
126
}
127
- free(path);
127
+ free(ref);
128
closedir(dir);
129
}
130
return list;
@@ -145,12 +145,14 @@ static struct ref_list *get_loose_refs(void)
145
/* We allow "recursive" symbolic refs. Only within reason, though */
146
#define MAXDEPTH 5
147
148
-const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
148
+const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
149
{
150
int depth = MAXDEPTH, len;
151
char buffer[256];
152
+ static char ref_buffer[256];
153
154
for (;;) {
155
+ const char *path = git_path("%s", ref);
156
struct stat st;
157
char *buf;
158
int fd;
@@ -169,14 +171,16 @@ const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
171
if (reading || errno != ENOENT)
172
return NULL;
173
hashclr(sha1);
172
- return path;
174
+ return ref;
175
}
176
177
/* Follow "normalized" - ie "refs/.." symlinks by hand */
178
if (S_ISLNK(st.st_mode)) {
179
len = readlink(path, buffer, sizeof(buffer)-1);
180
if (len >= 5 && !memcmp("refs/", buffer, 5)) {
179
- path = git_path("%.*s", len, buffer);
181
+ buffer[len] = 0;
182
+ strcpy(ref_buffer, buffer);
183
+ ref = ref_buffer;
184
continue;
185
}
186
}
@@ -201,19 +205,22 @@ const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
205
while (len && isspace(*buf))
206
buf++, len--;
207
while (len && isspace(buf[len-1]))
204
- buf[--len] = 0;
205
- path = git_path("%.*s", len, buf);
208
+ len--;
209
+ buf[len] = 0;
210
+ memcpy(ref_buffer, buf, len + 1);
211
+ ref = ref_buffer;
212
}
213
if (len < 40 || get_sha1_hex(buffer, sha1))
214
return NULL;
209
- return path;
215
+ return ref;
216
}
217
212
-int create_symref(const char *git_HEAD, const char *refs_heads_master)
218
+int create_symref(const char *ref_target, const char *refs_heads_master)
219
{
220
const char *lockpath;
221
char ref[1000];
222
int fd, len, written;
223
+ const char *git_HEAD = git_path("%s", ref_target);
224
225
#ifndef NO_SYMLINK_HEAD
226
if (prefer_symlink_refs) {
@@ -251,9 +258,9 @@ int create_symref(const char *git_HEAD, const char *refs_heads_master)
258
return 0;
259
}
260
254
-int read_ref(const char *filename, unsigned char *sha1)
261
+int read_ref(const char *ref, unsigned char *sha1)
262
{
256
- if (resolve_ref(filename, sha1, 1))
263
+ if (resolve_ref(ref, sha1, 1))
264
return 0;
265
return -1;
266
}
@@ -306,7 +313,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
313
int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
314
{
315
unsigned char sha1[20];
309
- if (!read_ref(git_path("HEAD"), sha1))
316
+ if (!read_ref("HEAD", sha1))
317
return fn("HEAD", sha1);
318
return 0;
319
}
@@ -335,7 +342,7 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)
342
{
343
if (check_ref_format(ref))
344
return -1;
338
- return read_ref(git_path("refs/%s", ref), sha1);
345
+ return read_ref(mkpath("refs/%s", ref), sha1);
346
}
347
348
/*
@@ -416,31 +423,30 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
423
return lock;
424
}
425
419
-static struct ref_lock *lock_ref_sha1_basic(const char *path,
426
+static struct ref_lock *lock_ref_sha1_basic(const char *ref,
427
int plen,
428
const unsigned char *old_sha1, int mustexist)
429
{
423
- const char *orig_path = path;
430
+ const char *orig_ref = ref;
431
struct ref_lock *lock;
432
struct stat st;
433
434
lock = xcalloc(1, sizeof(struct ref_lock));
435
lock->lock_fd = -1;
436
430
- plen = strlen(path) - plen;
431
- path = resolve_ref(path, lock->old_sha1, mustexist);
432
- if (!path) {
437
+ ref = resolve_ref(ref, lock->old_sha1, mustexist);
438
+ if (!ref) {
439
int last_errno = errno;
440
error("unable to resolve reference %s: %s",
435
- orig_path, strerror(errno));
441
+ orig_ref, strerror(errno));
442
unlock_ref(lock);
443
errno = last_errno;
444
return NULL;
445
}
446
lock->lk = xcalloc(1, sizeof(struct lock_file));
447
442
- lock->ref_file = xstrdup(path);
443
- lock->log_file = xstrdup(git_path("logs/%s", lock->ref_file + plen));
448
+ lock->ref_file = xstrdup(git_path("%s", ref));
449
+ lock->log_file = xstrdup(git_path("logs/%s", ref));
450
lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
451
452
if (safe_create_leading_directories(lock->ref_file))
@@ -455,15 +461,14 @@ struct ref_lock *lock_ref_sha1(const char *ref,
461
{
462
if (check_ref_format(ref))
463
return NULL;
458
- return lock_ref_sha1_basic(git_path("refs/%s", ref),
464
+ return lock_ref_sha1_basic(mkpath("refs/%s", ref),
465
5 + strlen(ref), old_sha1, mustexist);
466
}
467
468
struct ref_lock *lock_any_ref_for_update(const char *ref,
469
const unsigned char *old_sha1, int mustexist)
470
{
465
- return lock_ref_sha1_basic(git_path("%s", ref),
466
- strlen(ref), old_sha1, mustexist);
471
+ return lock_ref_sha1_basic(ref, strlen(ref), old_sha1, mustexist);
472
}
473
474
void unlock_ref(struct ref_lock *lock)
sha1_name.c
+7
-7
@@ -247,8 +247,8 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
247
NULL
248
};
249
static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
250
- const char **p, *pathname;
251
- char *real_path = NULL;
250
+ const char **p, *ref;
251
+ char *real_ref = NULL;
252
int refs_found = 0, am;
253
unsigned long at_time = (unsigned long)-1;
254
unsigned char *this_result;
@@ -276,10 +276,10 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
276
277
for (p = fmt; *p; p++) {
278
this_result = refs_found ? sha1_from_ref : sha1;
279
- pathname = resolve_ref(git_path(*p, len, str), this_result, 1);
280
- if (pathname) {
279
+ ref = resolve_ref(mkpath(*p, len, str), this_result, 1);
280
+ if (ref) {
281
if (!refs_found++)
282
- real_path = xstrdup(pathname);
282
+ real_ref = xstrdup(ref);
283
if (!warn_ambiguous_refs)
284
break;
285
}
@@ -293,12 +293,12 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
293
294
if (at_time != (unsigned long)-1) {
295
read_ref_at(
296
- real_path + strlen(git_path(".")) - 1,
296
+ real_ref,
297
at_time,
298
sha1);
299
}
300
301
- free(real_path);
301
+ free(real_ref);
302
return 0;
303
}
304