@matheusbsbs / matheus-bagliano / commits / c0277d15ef
Fork 0

delete_ref(): delete packed ref

This implements deletion of a packed ref. Since it is a very rare event to delete a ref compared to looking up, creating and updating, this opts to remove the ref from the packed-ref file instead of doing any of the filesystem based "negative ref" trick to optimize the deletion path. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 30, 2006 at 15:02 UTC c0277d15effb9efcaaaa0cd84decf71a327ac07b
1 file changed +77 -32
refs.c
+77 -32
@@ -406,33 +406,6 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)
406 return read_ref(mkpath("refs/%s", ref), sha1);
407 }
408
409 -int delete_ref(const char *refname, unsigned char *sha1)
410 -{
411 - struct ref_lock *lock;
412 - int err, i, ret = 0;
413 -
414 - lock = lock_any_ref_for_update(refname, sha1);
415 - if (!lock)
416 - return 1;
417 - i = strlen(lock->lk->filename) - 5; /* .lock */
418 - lock->lk->filename[i] = 0;
419 - err = unlink(lock->lk->filename);
420 - if (err) {
421 - ret = 1;
422 - error("unlink(%s) failed: %s",
423 - lock->lk->filename, strerror(errno));
424 - }
425 - lock->lk->filename[i] = '.';
426 -
427 - err = unlink(lock->log_file);
428 - if (err && errno != ENOENT)
429 - fprintf(stderr, "warning: unlink(%s) failed: %s",
430 - lock->log_file, strerror(errno));
431 -
432 - invalidate_cached_refs();
433 - return ret;
434 -}
435 -
409 /*
410 * Make sure "ref" is something reasonable to have under ".git/refs/";
411 * We do not like it if:
@@ -555,7 +528,7 @@ static int remove_empty_directories(char *file)
528 return remove_empty_dir_recursive(path, len);
529 }
530
558 -static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1)
531 +static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
532 {
533 char *ref_file;
534 const char *orig_ref = ref;
@@ -567,7 +540,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
540 lock = xcalloc(1, sizeof(struct ref_lock));
541 lock->lock_fd = -1;
542
570 - ref = resolve_ref(ref, lock->old_sha1, mustexist, NULL);
543 + ref = resolve_ref(ref, lock->old_sha1, mustexist, flag);
544 if (!ref && errno == EISDIR) {
545 /* we are trying to lock foo but we used to
546 * have foo/bar which now does not exist;
@@ -580,7 +553,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
553 error("there are still refs under '%s'", orig_ref);
554 goto error_return;
555 }
583 - ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, NULL);
556 + ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, flag);
557 }
558 if (!ref) {
559 last_errno = errno;
@@ -640,12 +613,84 @@ struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
613 if (check_ref_format(ref))
614 return NULL;
615 strcpy(refpath, mkpath("refs/%s", ref));
643 - return lock_ref_sha1_basic(refpath, old_sha1);
616 + return lock_ref_sha1_basic(refpath, old_sha1, NULL);
617 }
618
619 struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1)
620 {
648 - return lock_ref_sha1_basic(ref, old_sha1);
621 + return lock_ref_sha1_basic(ref, old_sha1, NULL);
622 +}
623 +
624 +static int repack_without_ref(const char *refname)
625 +{
626 + struct ref_list *list, *packed_ref_list;
627 + int fd;
628 + int found = 0;
629 + struct lock_file packlock;
630 +
631 + packed_ref_list = get_packed_refs();
632 + for (list = packed_ref_list; list; list = list->next) {
633 + if (!strcmp(refname, list->name)) {
634 + found = 1;
635 + break;
636 + }
637 + }
638 + if (!found)
639 + return 0;
640 + memset(&packlock, 0, sizeof(packlock));
641 + fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
642 + if (fd < 0)
643 + return error("cannot delete '%s' from packed refs", refname);
644 +
645 + for (list = packed_ref_list; list; list = list->next) {
646 + char line[PATH_MAX + 100];
647 + int len;
648 +
649 + if (!strcmp(refname, list->name))
650 + continue;
651 + len = snprintf(line, sizeof(line), "%s %s\n",
652 + sha1_to_hex(list->sha1), list->name);
653 + /* this should not happen but just being defensive */
654 + if (len > sizeof(line))
655 + die("too long a refname '%s'", list->name);
656 + write_or_die(fd, line, len);
657 + }
658 + return commit_lock_file(&packlock);
659 +}
660 +
661 +int delete_ref(const char *refname, unsigned char *sha1)
662 +{
663 + struct ref_lock *lock;
664 + int err, i, ret = 0, flag = 0;
665 +
666 + lock = lock_ref_sha1_basic(refname, sha1, &flag);
667 + if (!lock)
668 + return 1;
669 + if (!(flag & REF_ISPACKED)) {
670 + /* loose */
671 + i = strlen(lock->lk->filename) - 5; /* .lock */
672 + lock->lk->filename[i] = 0;
673 + err = unlink(lock->lk->filename);
674 + if (err) {
675 + ret = 1;
676 + error("unlink(%s) failed: %s",
677 + lock->lk->filename, strerror(errno));
678 + }
679 + lock->lk->filename[i] = '.';
680 + }
681 + /* removing the loose one could have resurrected an earlier
682 + * packed one. Also, if it was not loose we need to repack
683 + * without it.
684 + */
685 + ret |= repack_without_ref(refname);
686 +
687 + err = unlink(lock->log_file);
688 + if (err && errno != ENOENT)
689 + fprintf(stderr, "warning: unlink(%s) failed: %s",
690 + lock->log_file, strerror(errno));
691 + invalidate_cached_refs();
692 + unlock_ref(lock);
693 + return ret;
694 }
695
696 void unlock_ref(struct ref_lock *lock)