make 'got ref -d' delete both loose and packed representations of the reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
diff --git a/lib/reference.c b/lib/reference.c
index 3c1a9f7..1e9c232 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -1333,17 +1333,14 @@ done:
return err ? err : unlock_err;
}
-const struct got_error *
-got_ref_delete(struct got_reference *ref, struct got_repository *repo)
+static const struct got_error *
+delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
{
const struct got_error *err = NULL, *unlock_err = NULL;
const char *name = got_ref_get_name(ref);
char *path_refs = NULL, *path = NULL;
struct got_lockfile *lf = NULL;
- if (ref->flags & GOT_REF_IS_PACKED)
- return delete_packed_ref(ref, repo);
-
path_refs = get_refs_dir_path(repo, name);
if (path_refs == NULL) {
err = got_error_from_errno2("get_refs_dir_path", name);
@@ -1375,6 +1372,45 @@ done:
}
const struct got_error *
+got_ref_delete(struct got_reference *ref, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_reference *ref2;
+
+ if (ref->flags & GOT_REF_IS_PACKED) {
+ err = delete_packed_ref(ref, repo);
+ if (err)
+ return err;
+
+ err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 1);
+ if (err) {
+ if (err->code == GOT_ERR_NOT_REF)
+ return NULL;
+ return err;
+ }
+
+ err = delete_loose_ref(ref2, repo);
+ got_ref_close(ref2);
+ return err;
+ } else {
+ err = delete_loose_ref(ref, repo);
+ if (err)
+ return err;
+
+ err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 1);
+ if (err) {
+ if (err->code == GOT_ERR_NOT_REF)
+ return NULL;
+ return err;
+ }
+
+ err = delete_packed_ref(ref2, repo);
+ got_ref_close(ref2);
+ return err;
+ }
+}
+
+const struct got_error *
got_ref_unlock(struct got_reference *ref)
{
const struct got_error *err;
diff --git a/regress/cmdline/ref.sh b/regress/cmdline/ref.sh
index 62f1dd6..5a2b0f6 100755
--- a/regress/cmdline/ref.sh
+++ b/regress/cmdline/ref.sh
@@ -238,6 +238,40 @@ test_ref_delete() {
ret="$?"
if [ "$ret" != "0" ]; then
diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/repo && git pack-refs --all)
+
+ echo "modified alpha" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "modified alpha"
+ local commit_id2=`git_show_head $testroot/repo`
+
+ # ref 'master' now exists in both packed and loose forms
+
+ got ref -l -r $testroot/repo > $testroot/stdout
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
+ echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
+ echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -r $testroot/repo -d master
+
+ got ref -l -r $testroot/repo > $testroot/stdout
+ echo "refs/heads/ref1: $commit_id" > $testroot/stdout.expected
+ echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
fi
test_done "$testroot" "$ret"
}