Skip to content

Commit ca8db14

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
[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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9b143c6 commit ca8db14

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ extern int has_pack_index(const unsigned char *sha1);
229229
extern int get_sha1(const char *str, unsigned char *sha1);
230230
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
231231
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
232+
extern int read_ref(const char *filename, unsigned char *sha1);
232233

233234
/* General helper functions */
234235
extern void usage(const char *err) NORETURN;

refs.c

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,43 @@
22
#include "cache.h"
33

44
#include <errno.h>
5+
#include <ctype.h>
56

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)
711
{
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);
1018

11-
if (fd >= 0) {
12-
char buffer[60];
13-
if (read(fd, buffer, sizeof(buffer)) >= 40)
14-
ret = get_sha1_hex(buffer, sha1);
1519
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;
1642
}
1743
return ret;
1844
}
@@ -54,7 +80,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
5480
break;
5581
continue;
5682
}
57-
if (read_ref(path, sha1) < 0)
83+
if (read_ref(git_path("%s", path), sha1) < 0)
5884
continue;
5985
if (!has_sha1_file(sha1))
6086
continue;
@@ -71,7 +97,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
7197
int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
7298
{
7399
unsigned char sha1[20];
74-
if (!read_ref("HEAD", sha1))
100+
if (!read_ref(git_path("HEAD"), sha1))
75101
return fn("HEAD", sha1);
76102
return 0;
77103
}
@@ -101,33 +127,14 @@ static char *ref_lock_file_name(const char *ref)
101127
return ret;
102128
}
103129

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-
121130
int get_ref_sha1(const char *ref, unsigned char *sha1)
122131
{
123-
char *filename;
124-
int retval;
132+
const char *filename;
133+
125134
if (check_ref_format(ref))
126135
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);
131138
}
132139

133140
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,
140147
return error("Couldn't open lock file for %s: %s",
141148
filename, strerror(errno));
142149
}
143-
retval = read_ref_file(filename, current_sha1);
150+
retval = read_ref(filename, current_sha1);
144151
if (old_sha1) {
145152
if (retval) {
146153
close(fd);

sha1_name.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,6 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1)
119119
return -1;
120120
}
121121

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-
137122
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
138123
{
139124
static const char *prefix[] = {
@@ -150,7 +135,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
150135

151136
for (p = prefix; *p; p++) {
152137
char *pathname = git_path("%s/%.*s", *p, len, str);
153-
if (!get_sha1_file(pathname, sha1))
138+
if (!read_ref(pathname, sha1))
154139
return 0;
155140
}
156141

0 commit comments

Comments
 (0)