Add 'resolve_gitlink_ref()' helper function
This new function resolves a ref in *another* git repository. It's named for its intended use: to look up the git link to a subproject. It's not actually wired up to anything yet, but we're getting closer to having fundamental plumbing support for "links" from one git directory to another, which is the basis of subproject support. [jc: amended a FILE* leak] Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Apr 9, 2007 at 21:14 UTC
0ebde32c87da2efac5985a808e6bd4130831b7a8
2 files changed
+83
refs.c
+80
@@ -215,6 +215,86 @@ static struct ref_list *get_loose_refs(void)
215
216
/* We allow "recursive" symbolic refs. Only within reason, though */
217
#define MAXDEPTH 5
218
+#define MAXREFLEN (1024)
219
+
220
+static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refname, unsigned char *result)
221
+{
222
+ FILE *f;
223
+ struct cached_refs refs;
224
+ struct ref_list *ref;
225
+ int retval;
226
+
227
+ strcpy(name + pathlen, "packed-refs");
228
+ f = fopen(name, "r");
229
+ if (!f)
230
+ return -1;
231
+ read_packed_refs(f, &refs);
232
+ fclose(f);
233
+ ref = refs.packed;
234
+ retval = -1;
235
+ while (ref) {
236
+ if (!strcmp(ref->name, refname)) {
237
+ retval = 0;
238
+ memcpy(result, ref->sha1, 20);
239
+ break;
240
+ }
241
+ ref = ref->next;
242
+ }
243
+ free_ref_list(refs.packed);
244
+ return retval;
245
+}
246
+
247
+static int resolve_gitlink_ref_recursive(char *name, int pathlen, const char *refname, unsigned char *result, int recursion)
248
+{
249
+ int fd, len = strlen(refname);
250
+ char buffer[128], *p;
251
+
252
+ if (recursion > MAXDEPTH || len > MAXREFLEN)
253
+ return -1;
254
+ memcpy(name + pathlen, refname, len+1);
255
+ fd = open(name, O_RDONLY);
256
+ if (fd < 0)
257
+ return resolve_gitlink_packed_ref(name, pathlen, refname, result);
258
+
259
+ len = read(fd, buffer, sizeof(buffer)-1);
260
+ close(fd);
261
+ if (len < 0)
262
+ return -1;
263
+ while (len && isspace(buffer[len-1]))
264
+ len--;
265
+ buffer[len] = 0;
266
+
267
+ /* Was it a detached head or an old-fashioned symlink? */
268
+ if (!get_sha1_hex(buffer, result))
269
+ return 0;
270
+
271
+ /* Symref? */
272
+ if (strncmp(buffer, "ref:", 4))
273
+ return -1;
274
+ p = buffer + 4;
275
+ while (isspace(*p))
276
+ p++;
277
+
278
+ return resolve_gitlink_ref_recursive(name, pathlen, p, result, recursion+1);
279
+}
280
+
281
+int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *result)
282
+{
283
+ int len = strlen(path), retval;
284
+ char *gitdir;
285
+
286
+ while (len && path[len-1] == '/')
287
+ len--;
288
+ if (!len)
289
+ return -1;
290
+ gitdir = xmalloc(len + MAXREFLEN + 8);
291
+ memcpy(gitdir, path, len);
292
+ memcpy(gitdir + len, "/.git/", 7);
293
+
294
+ retval = resolve_gitlink_ref_recursive(gitdir, len+6, refname, result, 0);
295
+ free(gitdir);
296
+ return retval;
297
+}
298
299
const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
300
{
refs.h
+3
@@ -60,4 +60,7 @@ extern int check_ref_format(const char *target);
60
/** rename ref, return 0 on success **/
61
extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
62
63
+/** resolve ref in nested "gitlink" repository */
64
+extern int resolve_gitlink_ref(const char *name, const char *refname, unsigned char *result);
65
+
66
#endif /* REFS_H */