[PATCH] Allow reading "symbolic refs" that point to other refs
This extends the ref reading to understand a "symbolic ref": a ref file that starts with "ref: " and points to another ref file, and thus introduces the notion of ref aliases. This is in preparation of allowing HEAD to eventually not be a symlink, but one of these symbolic refs instead. [jc: Linus originally required the prefix to be "ref: " five bytes and nothing else, but I changed it to allow and strip any number of leading whitespaces to match what update-ref.c does.] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Sep 25, 2005 at 09:59 UTC
ca8db1424d1808a1f78bc9905efd267f7c154d8e
3 files changed
+42
-49
cache.h
+1
@@ -229,6 +229,7 @@ extern int has_pack_index(const unsigned char *sha1);
229
extern int get_sha1(const char *str, unsigned char *sha1);
230
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
231
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
232
+extern int read_ref(const char *filename, unsigned char *sha1);
233
234
/* General helper functions */
235
extern void usage(const char *err) NORETURN;
refs.c
+40
-33
@@ -2,17 +2,43 @@
2
#include "cache.h"
3
4
#include <errno.h>
5
+#include <ctype.h>
6
6
-static int read_ref(const char *refname, unsigned char *sha1)
7
+/* We allow "recursive" symbolic refs. Only within reason, though */
8
+#define MAXDEPTH 5
9
+
10
+int read_ref(const char *filename, unsigned char *sha1)
11
{
8
- int ret = -1;
9
- int fd = open(git_path("%s", refname), O_RDONLY);
12
+ int depth = 0;
13
+ int ret = -1, fd;
14
+
15
+ while ((fd = open(filename, O_RDONLY)) >= 0) {
16
+ char buffer[256];
17
+ int len = read(fd, buffer, sizeof(buffer)-1);
18
11
- if (fd >= 0) {
12
- char buffer[60];
13
- if (read(fd, buffer, sizeof(buffer)) >= 40)
14
- ret = get_sha1_hex(buffer, sha1);
19
close(fd);
20
+ if (len < 0)
21
+ break;
22
+
23
+ buffer[len] = 0;
24
+ while (len && isspace(buffer[len-1]))
25
+ buffer[--len] = 0;
26
+
27
+ if (!strncmp(buffer, "ref:", 4)) {
28
+ char *buf;
29
+ if (depth > MAXDEPTH)
30
+ break;
31
+ depth++;
32
+ buf = buffer + 4;
33
+ len -= 4;
34
+ while (len && isspace(*buf))
35
+ buf++, len--;
36
+ filename = git_path("%.*s", len, buf);
37
+ continue;
38
+ }
39
+ if (len >= 40)
40
+ ret = get_sha1_hex(buffer, sha1);
41
+ break;
42
}
43
return ret;
44
}
@@ -54,7 +80,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
80
break;
81
continue;
82
}
57
- if (read_ref(path, sha1) < 0)
83
+ if (read_ref(git_path("%s", path), sha1) < 0)
84
continue;
85
if (!has_sha1_file(sha1))
86
continue;
@@ -71,7 +97,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
97
int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
98
{
99
unsigned char sha1[20];
74
- if (!read_ref("HEAD", sha1))
100
+ if (!read_ref(git_path("HEAD"), sha1))
101
return fn("HEAD", sha1);
102
return 0;
103
}
@@ -101,33 +127,14 @@ static char *ref_lock_file_name(const char *ref)
127
return ret;
128
}
129
104
-static int read_ref_file(const char *filename, unsigned char *sha1) {
105
- int fd = open(filename, O_RDONLY);
106
- char hex[41];
107
- if (fd < 0) {
108
- return error("Couldn't open %s\n", filename);
109
- }
110
- if ((read(fd, hex, 41) < 41) ||
111
- (hex[40] != '\n') ||
112
- get_sha1_hex(hex, sha1)) {
113
- error("Couldn't read a hash from %s\n", filename);
114
- close(fd);
115
- return -1;
116
- }
117
- close(fd);
118
- return 0;
119
-}
120
-
130
int get_ref_sha1(const char *ref, unsigned char *sha1)
131
{
123
- char *filename;
124
- int retval;
132
+ const char *filename;
133
+
134
if (check_ref_format(ref))
135
return -1;
127
- filename = ref_file_name(ref);
128
- retval = read_ref_file(filename, sha1);
129
- free(filename);
130
- return retval;
136
+ filename = git_path("refs/%s", ref);
137
+ return read_ref(filename, sha1);
138
}
139
140
static int lock_ref_file(const char *filename, const char *lock_filename,
@@ -140,7 +147,7 @@ static int lock_ref_file(const char *filename, const char *lock_filename,
147
return error("Couldn't open lock file for %s: %s",
148
filename, strerror(errno));
149
}
143
- retval = read_ref_file(filename, current_sha1);
150
+ retval = read_ref(filename, current_sha1);
151
if (old_sha1) {
152
if (retval) {
153
close(fd);
sha1_name.c
+1
-16
@@ -119,21 +119,6 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1)
119
return -1;
120
}
121
122
-static int get_sha1_file(const char *path, unsigned char *result)
123
-{
124
- char buffer[60];
125
- int fd = open(path, O_RDONLY);
126
- int len;
127
-
128
- if (fd < 0)
129
- return -1;
130
- len = read(fd, buffer, sizeof(buffer));
131
- close(fd);
132
- if (len < 40)
133
- return -1;
134
- return get_sha1_hex(buffer, result);
135
-}
136
-
122
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
123
{
124
static const char *prefix[] = {
@@ -150,7 +135,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
135
136
for (p = prefix; *p; p++) {
137
char *pathname = git_path("%s/%.*s", *p, len, str);
153
- if (!get_sha1_file(pathname, sha1))
138
+ if (!read_ref(pathname, sha1))
139
return 0;
140
}
141