@matheusbsbs / matheus-bagliano / commits / 434cd0cd30
Fork 0

Enable the packed refs file format

This actually "turns on" the packed ref file format, now that the infrastructure to do so sanely exists (ie notably the change to make the reference reading logic take refnames rather than pathnames to the loose objects that no longer necessarily even exist). In particular, when the ref lookup hits a refname that has no loose file associated with it, it falls back on the packed-ref information. Also, the ref-locking code, while still using a loose file for the locking itself (and _creating_ a loose file for the new ref) no longer requires that the old ref be in such an unpacked state. Finally, this does a minimal hack to git-checkout.sh to rather than check the ref-file directly, do a "git-rev-parse" on the "heads/$refname". That's not really wonderful - we should rather really have a special routine to verify the names as proper branch head names, but it is a workable solution for now. With this, I can literally do something like git pack-refs find .git/refs -type f -print0 | xargs -0 rm -f -- and the end result is a largely working repository (ie I've done two commits - which creates _one_ unpacked ref file - done things like run "gitk" and "git log" etc, and it all looks ok). There are probably things missing, but I'm hoping that the missing things are now of the "small and obvious" kind, and that somebody else might want to start looking at this too. Hint hint ;) Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Sep 14, 2006 at 10:14 UTC 434cd0cd30d17bc703397a76480fdd129cecc064
3 files changed +25 -22
git-checkout.sh
+2 -2
@@ -22,7 +22,7 @@ while [ "$#" != "0" ]; do
22 shift
23 [ -z "$newbranch" ] &&
24 die "git checkout: -b needs a branch name"
25 - [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
25 + git-rev-parse --symbolic "heads/$newbranch" >&/dev/null &&
26 die "git checkout: branch $newbranch already exists"
27 git-check-ref-format "heads/$newbranch" ||
28 die "git checkout: we do not like '$newbranch' as a branch name."
@@ -51,7 +51,7 @@ while [ "$#" != "0" ]; do
51 fi
52 new="$rev"
53 new_name="$arg^0"
54 - if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
54 + if git-rev-parse "heads/$arg^0" >&/dev/null; then
55 branch="$arg"
56 fi
57 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
refs.c
+22 -19
@@ -28,6 +28,8 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
28 if (!isspace(line[40]))
29 return NULL;
30 line += 41;
31 + if (isspace(*line))
32 + return NULL;
33 if (line[len] != '\n')
34 return NULL;
35 line[len] = 0;
@@ -168,6 +170,14 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
170 * reading.
171 */
172 if (lstat(path, &st) < 0) {
173 + struct ref_list *list = get_packed_refs();
174 + while (list) {
175 + if (!strcmp(ref, list->name)) {
176 + hashcpy(sha1, list->sha1);
177 + return ref;
178 + }
179 + list = list->next;
180 + }
181 if (reading || errno != ENOENT)
182 return NULL;
183 hashclr(sha1);
@@ -400,22 +410,13 @@ int check_ref_format(const char *ref)
410 static struct ref_lock *verify_lock(struct ref_lock *lock,
411 const unsigned char *old_sha1, int mustexist)
412 {
403 - char buf[40];
404 - int nr, fd = open(lock->ref_file, O_RDONLY);
405 - if (fd < 0 && (mustexist || errno != ENOENT)) {
406 - error("Can't verify ref %s", lock->ref_file);
407 - unlock_ref(lock);
408 - return NULL;
409 - }
410 - nr = read(fd, buf, 40);
411 - close(fd);
412 - if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
413 - error("Can't verify ref %s", lock->ref_file);
413 + if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist)) {
414 + error("Can't verify ref %s", lock->ref_name);
415 unlock_ref(lock);
416 return NULL;
417 }
418 if (hashcmp(lock->old_sha1, old_sha1)) {
418 - error("Ref %s is at %s but expected %s", lock->ref_file,
419 + error("Ref %s is at %s but expected %s", lock->ref_name,
420 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
421 unlock_ref(lock);
422 return NULL;
@@ -427,6 +428,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref,
428 int plen,
429 const unsigned char *old_sha1, int mustexist)
430 {
431 + char *ref_file;
432 const char *orig_ref = ref;
433 struct ref_lock *lock;
434 struct stat st;
@@ -445,13 +447,14 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref,
447 }
448 lock->lk = xcalloc(1, sizeof(struct lock_file));
449
448 - lock->ref_file = xstrdup(git_path("%s", ref));
450 + lock->ref_name = xstrdup(ref);
451 lock->log_file = xstrdup(git_path("logs/%s", ref));
450 - lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
452 + ref_file = git_path(ref);
453 + lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
454
452 - if (safe_create_leading_directories(lock->ref_file))
453 - die("unable to create directory for %s", lock->ref_file);
454 - lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
455 + if (safe_create_leading_directories(ref_file))
456 + die("unable to create directory for %s", ref_file);
457 + lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
458
459 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
460 }
@@ -479,7 +482,7 @@ void unlock_ref(struct ref_lock *lock)
482 if (lock->lk)
483 rollback_lock_file(lock->lk);
484 }
482 - free(lock->ref_file);
485 + free(lock->ref_name);
486 free(lock->log_file);
487 free(lock);
488 }
@@ -556,7 +559,7 @@ int write_ref_sha1(struct ref_lock *lock,
559 return -1;
560 }
561 if (commit_lock_file(lock->lk)) {
559 - error("Couldn't set %s", lock->ref_file);
562 + error("Couldn't set %s", lock->ref_name);
563 unlock_ref(lock);
564 return -1;
565 }
refs.h
+1 -1
@@ -2,7 +2,7 @@
2 #define REFS_H
3
4 struct ref_lock {
5 - char *ref_file;
5 + char *ref_name;
6 char *log_file;
7 struct lock_file *lk;
8 unsigned char old_sha1[20];