Skip to content

Commit 3b8572a

Browse files
committed
Merge branch 'mv/maint-branch-m-symref' into maint
* mv/maint-branch-m-symref: update-ref --no-deref -d: handle the case when the pointed ref is packed git branch -m: forbid renaming of a symref Fix git update-ref --no-deref -d. rename_ref(): handle the case when the reflog of a ref does not exist Fix git branch -m for symrefs.
2 parents 16088d8 + 045a476 commit 3b8572a

11 files changed

+65
-24
lines changed

builtin-branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
160160
continue;
161161
}
162162

163-
if (delete_ref(name, sha1)) {
163+
if (delete_ref(name, sha1, 0)) {
164164
error("Error deleting %sbranch '%s'", remote,
165165
argv[i]);
166166
ret = 1;

builtin-remote.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static int remove_branches(struct string_list *branches)
340340
const char *refname = item->string;
341341
unsigned char *sha1 = item->util;
342342

343-
if (delete_ref(refname, sha1))
343+
if (delete_ref(refname, sha1, 0))
344344
result |= error("Could not remove branch %s", refname);
345345
}
346346
return result;
@@ -570,7 +570,7 @@ static int prune(int argc, const char **argv)
570570
const char *refname = states.stale.items[i].util;
571571

572572
if (!dry_run)
573-
result |= delete_ref(refname, NULL);
573+
result |= delete_ref(refname, NULL, 0);
574574

575575
printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
576576
abbrev_ref(refname, "refs/remotes/"));

builtin-reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
279279
update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
280280
}
281281
else if (old_orig)
282-
delete_ref("ORIG_HEAD", old_orig);
282+
delete_ref("ORIG_HEAD", old_orig, 0);
283283
prepend_reflog_action("updating HEAD", msg, sizeof(msg));
284284
update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
285285

builtin-send-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
232232
if (args.verbose)
233233
fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
234234
if (ref->deletion) {
235-
delete_ref(rs.dst, NULL);
235+
delete_ref(rs.dst, NULL, 0);
236236
} else
237237
update_ref("update by push", rs.dst,
238238
ref->new_sha1, NULL, 0, 0);

builtin-tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
125125
static int delete_tag(const char *name, const char *ref,
126126
const unsigned char *sha1)
127127
{
128-
if (delete_ref(ref, sha1))
128+
if (delete_ref(ref, sha1, 0))
129129
return 1;
130130
printf("Deleted tag '%s'\n", name);
131131
return 0;

builtin-update-ref.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
1313
{
1414
const char *refname, *oldval, *msg=NULL;
1515
unsigned char sha1[20], oldsha1[20];
16-
int delete = 0, no_deref = 0;
16+
int delete = 0, no_deref = 0, flags = 0;
1717
struct option options[] = {
1818
OPT_STRING( 'm', NULL, &msg, "reason", "reason of the update"),
1919
OPT_BOOLEAN('d', NULL, &delete, "deletes the reference"),
@@ -47,9 +47,11 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
4747
if (oldval && *oldval && get_sha1(oldval, oldsha1))
4848
die("%s: not a valid old SHA1", oldval);
4949

50+
if (no_deref)
51+
flags = REF_NODEREF;
5052
if (delete)
51-
return delete_ref(refname, oldval ? oldsha1 : NULL);
53+
return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
5254
else
5355
return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
54-
no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
56+
flags, DIE_ON_ERR);
5557
}

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ extern int commit_locked_index(struct lock_file *);
424424
extern void set_alternate_index_output(const char *);
425425
extern int close_lock_file(struct lock_file *);
426426
extern void rollback_lock_file(struct lock_file *);
427-
extern int delete_ref(const char *, const unsigned char *sha1);
427+
extern int delete_ref(const char *, const unsigned char *sha1, int delopt);
428428

429429
/* Environment bits from configuration mechanism */
430430
extern int trust_executable_bit;

receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static const char *update(struct command *cmd)
222222
warning ("Allowing deletion of corrupt ref.");
223223
old_sha1 = NULL;
224224
}
225-
if (delete_ref(name, old_sha1)) {
225+
if (delete_ref(name, old_sha1, 0)) {
226226
error("failed to delete %s", name);
227227
return "failed to delete";
228228
}

refs.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -915,25 +915,33 @@ static int repack_without_ref(const char *refname)
915915
return commit_lock_file(&packlock);
916916
}
917917

918-
int delete_ref(const char *refname, const unsigned char *sha1)
918+
int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
919919
{
920920
struct ref_lock *lock;
921-
int err, i, ret = 0, flag = 0;
921+
int err, i = 0, ret = 0, flag = 0;
922922

923923
lock = lock_ref_sha1_basic(refname, sha1, 0, &flag);
924924
if (!lock)
925925
return 1;
926-
if (!(flag & REF_ISPACKED)) {
926+
if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
927927
/* loose */
928-
i = strlen(lock->lk->filename) - 5; /* .lock */
929-
lock->lk->filename[i] = 0;
930-
err = unlink(lock->lk->filename);
928+
const char *path;
929+
930+
if (!(delopt & REF_NODEREF)) {
931+
i = strlen(lock->lk->filename) - 5; /* .lock */
932+
lock->lk->filename[i] = 0;
933+
path = lock->lk->filename;
934+
} else {
935+
path = git_path(refname);
936+
}
937+
err = unlink(path);
931938
if (err && errno != ENOENT) {
932939
ret = 1;
933940
error("unlink(%s) failed: %s",
934-
lock->lk->filename, strerror(errno));
941+
path, strerror(errno));
935942
}
936-
lock->lk->filename[i] = '.';
943+
if (!(delopt & REF_NODEREF))
944+
lock->lk->filename[i] = '.';
937945
}
938946
/* removing the loose one could have resurrected an earlier
939947
* packed one. Also, if it was not loose we need to repack
@@ -958,11 +966,16 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
958966
struct ref_lock *lock;
959967
struct stat loginfo;
960968
int log = !lstat(git_path("logs/%s", oldref), &loginfo);
969+
const char *symref = NULL;
961970

962-
if (S_ISLNK(loginfo.st_mode))
971+
if (log && S_ISLNK(loginfo.st_mode))
963972
return error("reflog for %s is a symlink", oldref);
964973

965-
if (!resolve_ref(oldref, orig_sha1, 1, &flag))
974+
symref = resolve_ref(oldref, orig_sha1, 1, &flag);
975+
if (flag & REF_ISSYMREF)
976+
return error("refname %s is a symbolic ref, renaming it is not supported",
977+
oldref);
978+
if (!symref)
966979
return error("refname %s not found", oldref);
967980

968981
if (!is_refname_available(newref, oldref, get_packed_refs(), 0))
@@ -982,12 +995,12 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
982995
return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
983996
oldref, strerror(errno));
984997

985-
if (delete_ref(oldref, orig_sha1)) {
998+
if (delete_ref(oldref, orig_sha1, REF_NODEREF)) {
986999
error("unable to delete old %s", oldref);
9871000
goto rollback;
9881001
}
9891002

990-
if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1)) {
1003+
if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1, REF_NODEREF)) {
9911004
if (errno==EISDIR) {
9921005
if (remove_empty_directories(git_path("%s", newref))) {
9931006
error("Directory not empty: %s", newref);
@@ -1030,7 +1043,6 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
10301043
error("unable to lock %s for update", newref);
10311044
goto rollback;
10321045
}
1033-
10341046
lock->force_write = 1;
10351047
hashcpy(lock->old_sha1, orig_sha1);
10361048
if (write_ref_sha1(lock, orig_sha1, logmsg)) {

t/t1400-update-ref.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ test_expect_success "delete $m (by HEAD)" '
7575
'
7676
rm -f .git/$m
7777

78+
cp -f .git/HEAD .git/HEAD.orig
79+
test_expect_success "delete symref without dereference" '
80+
git update-ref --no-deref -d HEAD &&
81+
! test -f .git/HEAD
82+
'
83+
cp -f .git/HEAD.orig .git/HEAD
84+
85+
test_expect_success "delete symref without dereference when the referred ref is packed" '
86+
echo foo >foo.c &&
87+
git add foo.c &&
88+
git commit -m foo &&
89+
git pack-refs --all &&
90+
git update-ref --no-deref -d HEAD &&
91+
! test -f .git/HEAD
92+
'
93+
cp -f .git/HEAD.orig .git/HEAD
94+
git update-ref -d $m
95+
7896
test_expect_success '(not) create HEAD with old sha1' "
7997
test_must_fail git update-ref HEAD $A $B
8098
"

0 commit comments

Comments
 (0)