Skip to content

Commit c976d41

Browse files
hjemliJunio C Hamano
authored andcommitted
git-branch: add options and tests for branch renaming
Extend git-branch with the following options: git-branch -m|-M [<oldbranch>] newbranch The -M variation is required to force renaming over an exsisting branchname. This also indroduces $GIT_DIR/RENAME_REF which is a "metabranch" used when renaming branches. It will always hold the original sha1 for the latest renamed branch. Additionally, if $GIT_DIR/logs/RENAME_REF exists, all branch rename events are logged there. Finally, some testcases are added to verify the new options. Signed-off-by: Lars Hjemli <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ebe185 commit c976d41

File tree

5 files changed

+250
-26
lines changed

5 files changed

+250
-26
lines changed

Documentation/git-branch.txt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ git-branch - List, create, or delete branches.
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git-branch' [-r] [-a] [-v] [--abbrev=<length>]
11+
'git-branch' [-r | -a] [-v [--abbrev=<length>]]
1212
'git-branch' [-l] [-f] <branchname> [<start-point>]
13+
'git-branch' (-m | -M) [<oldbranch>] <newbranch>
1314
'git-branch' (-d | -D) <branchname>...
1415

1516
DESCRIPTION
@@ -24,6 +25,12 @@ It will start out with a head equal to the one given as <start-point>.
2425
If no <start-point> is given, the branch will be created with a head
2526
equal to that of the currently checked out branch.
2627

28+
With a '-m' or '-M' option, <oldbranch> will be renamed to <newbranch>.
29+
If <oldbranch> had a corresponding reflog, it is renamed to match
30+
<newbranch>, and a reflog entry is created to remember the branch
31+
renaming. If <newbranch> exists, -M must be used to force the rename
32+
to happen.
33+
2734
With a `-d` or `-D` option, `<branchname>` will be deleted. You may
2835
specify more than one branch for deletion. If the branch currently
2936
has a ref log then the ref log will also be deleted.
@@ -46,14 +53,20 @@ OPTIONS
4653
Force the creation of a new branch even if it means deleting
4754
a branch that already exists with the same name.
4855

56+
-m::
57+
Move/rename a branch and the corresponding reflog.
58+
59+
-M::
60+
Move/rename a branch even if the new branchname already exists.
61+
4962
-r::
5063
List the remote-tracking branches.
5164

5265
-a::
5366
List both remote-tracking branches and local branches.
5467

5568
-v::
56-
Show sha1 and subject message for each head.
69+
Show sha1 and commit subjectline for each head.
5770

5871
--abbrev=<length>::
5972
Alter minimum display length for sha1 in output listing,
@@ -70,6 +83,12 @@ OPTIONS
7083
be given as a branch name, a commit-id, or a tag. If this option
7184
is omitted, the current branch is assumed.
7285

86+
<oldbranch>::
87+
The name of an existing branch to rename.
88+
89+
<newbranch>::
90+
The new name for an existing branch. The same restrictions as for
91+
<branchname> applies.
7392

7493

7594
Examples

builtin-branch.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "builtin.h"
1212

1313
static const char builtin_branch_usage[] =
14-
"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r | -a] [-v] [--abbrev=<length>] ";
14+
"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [-r | -a] [-v [--abbrev=<length>]]";
1515

1616

1717
static const char *head;
@@ -245,9 +245,37 @@ static void create_branch(const char *name, const char *start,
245245
die("Failed to write ref: %s.", strerror(errno));
246246
}
247247

248+
static void rename_branch(const char *oldname, const char *newname, int force)
249+
{
250+
char oldref[PATH_MAX], newref[PATH_MAX];
251+
unsigned char sha1[20];
252+
253+
if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
254+
die("Old branchname too long");
255+
256+
if (check_ref_format(oldref))
257+
die("Invalid branch name: %s", oldref);
258+
259+
if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
260+
die("New branchname too long");
261+
262+
if (check_ref_format(newref))
263+
die("Invalid branch name: %s", newref);
264+
265+
if (resolve_ref(newref, sha1, 1, NULL) && !force)
266+
die("A branch named '%s' already exists.", newname);
267+
268+
if (rename_ref(oldref, newref))
269+
die("Branch rename failed");
270+
271+
if (!strcmp(oldname, head) && create_symref("HEAD", newref))
272+
die("Branch renamed to %s, but HEAD is not updated!", newname);
273+
}
274+
248275
int cmd_branch(int argc, const char **argv, const char *prefix)
249276
{
250277
int delete = 0, force_delete = 0, force_create = 0;
278+
int rename = 0, force_rename = 0;
251279
int verbose = 0, abbrev = DEFAULT_ABBREV;
252280
int reflog = 0;
253281
int kinds = REF_LOCAL_BRANCH;
@@ -277,6 +305,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
277305
force_create = 1;
278306
continue;
279307
}
308+
if (!strcmp(arg, "-m")) {
309+
rename = 1;
310+
continue;
311+
}
312+
if (!strcmp(arg, "-M")) {
313+
rename = 1;
314+
force_rename = 1;
315+
continue;
316+
}
280317
if (!strcmp(arg, "-r")) {
281318
kinds = REF_REMOTE_BRANCH;
282319
continue;
@@ -300,6 +337,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
300337
usage(builtin_branch_usage);
301338
}
302339

340+
if ((delete && rename) || (delete && force_create) ||
341+
(rename && force_create))
342+
usage(builtin_branch_usage);
343+
303344
head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
304345
if (!head)
305346
die("Failed to resolve HEAD as a valid ref.");
@@ -311,6 +352,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
311352
delete_branches(argc - i, argv + i, force_delete);
312353
else if (i == argc)
313354
print_ref_list(kinds, verbose, abbrev);
355+
else if (rename && (i == argc - 1))
356+
rename_branch(head, argv[i], force_rename);
357+
else if (rename && (i == argc - 2))
358+
rename_branch(argv[i], argv[i + 1], force_rename);
314359
else if (i == argc - 1)
315360
create_branch(argv[i], head, force_create, reflog);
316361
else if (i == argc - 2)

refs.c

Lines changed: 146 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,29 @@ static int remove_empty_directories(char *file)
610610
return remove_empty_dir_recursive(path, len);
611611
}
612612

613+
static int is_refname_available(const char *ref, const char *oldref,
614+
struct ref_list *list, int quiet)
615+
{
616+
int namlen = strlen(ref); /* e.g. 'foo/bar' */
617+
while (list) {
618+
/* list->name could be 'foo' or 'foo/bar/baz' */
619+
if (!oldref || strcmp(oldref, list->name)) {
620+
int len = strlen(list->name);
621+
int cmplen = (namlen < len) ? namlen : len;
622+
const char *lead = (namlen < len) ? list->name : ref;
623+
if (!strncmp(ref, list->name, cmplen) &&
624+
lead[cmplen] == '/') {
625+
if (!quiet)
626+
error("'%s' exists; cannot create '%s'",
627+
list->name, ref);
628+
return 0;
629+
}
630+
}
631+
list = list->next;
632+
}
633+
return 1;
634+
}
635+
613636
static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
614637
{
615638
char *ref_file;
@@ -643,29 +666,14 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
643666
orig_ref, strerror(errno));
644667
goto error_return;
645668
}
646-
if (is_null_sha1(lock->old_sha1)) {
647-
/* The ref did not exist and we are creating it.
648-
* Make sure there is no existing ref that is packed
649-
* whose name begins with our refname, nor a ref whose
650-
* name is a proper prefix of our refname.
651-
*/
652-
int namlen = strlen(ref); /* e.g. 'foo/bar' */
653-
struct ref_list *list = get_packed_refs();
654-
while (list) {
655-
/* list->name could be 'foo' or 'foo/bar/baz' */
656-
int len = strlen(list->name);
657-
int cmplen = (namlen < len) ? namlen : len;
658-
const char *lead = (namlen < len) ? list->name : ref;
659-
660-
if (!strncmp(ref, list->name, cmplen) &&
661-
lead[cmplen] == '/') {
662-
error("'%s' exists; cannot create '%s'",
663-
list->name, ref);
664-
goto error_return;
665-
}
666-
list = list->next;
667-
}
668-
}
669+
/* When the ref did not exist and we are creating it,
670+
* make sure there is no existing ref that is packed
671+
* whose name begins with our refname, nor a ref whose
672+
* name is a proper prefix of our refname.
673+
*/
674+
if (is_null_sha1(lock->old_sha1) &&
675+
!is_refname_available(ref, NULL, get_packed_refs(), 0))
676+
goto error_return;
669677

670678
lock->lk = xcalloc(1, sizeof(struct lock_file));
671679

@@ -776,6 +784,121 @@ int delete_ref(const char *refname, unsigned char *sha1)
776784
return ret;
777785
}
778786

787+
int rename_ref(const char *oldref, const char *newref)
788+
{
789+
static const char renamed_ref[] = "RENAMED-REF";
790+
unsigned char sha1[20], orig_sha1[20];
791+
int flag = 0, logmoved = 0;
792+
struct ref_lock *lock;
793+
char msg[PATH_MAX*2 + 100];
794+
struct stat loginfo;
795+
int log = !stat(git_path("logs/%s", oldref), &loginfo);
796+
797+
if (S_ISLNK(loginfo.st_mode))
798+
return error("reflog for %s is a symlink", oldref);
799+
800+
if (!resolve_ref(oldref, orig_sha1, 1, &flag))
801+
return error("refname %s not found", oldref);
802+
803+
if (!is_refname_available(newref, oldref, get_packed_refs(), 0))
804+
return 1;
805+
806+
if (!is_refname_available(newref, oldref, get_loose_refs(), 0))
807+
return 1;
808+
809+
if (snprintf(msg, sizeof(msg), "renamed %s to %s", oldref, newref) > sizeof(msg))
810+
return error("Refnames to long");
811+
812+
lock = lock_ref_sha1_basic(renamed_ref, NULL, NULL);
813+
if (!lock)
814+
return error("unable to lock %s", renamed_ref);
815+
lock->force_write = 1;
816+
if (write_ref_sha1(lock, orig_sha1, msg))
817+
return error("unable to save current sha1 in %s", renamed_ref);
818+
819+
if (log && rename(git_path("logs/%s", oldref), git_path("tmp-renamed-log")))
820+
return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
821+
oldref, strerror(errno));
822+
823+
if (delete_ref(oldref, orig_sha1)) {
824+
error("unable to delete old %s", oldref);
825+
goto rollback;
826+
}
827+
828+
if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1)) {
829+
if (errno==EISDIR) {
830+
if (remove_empty_directories(git_path("%s", newref))) {
831+
error("Directory not empty: %s", newref);
832+
goto rollback;
833+
}
834+
} else {
835+
error("unable to delete existing %s", newref);
836+
goto rollback;
837+
}
838+
}
839+
840+
if (log && safe_create_leading_directories(git_path("logs/%s", newref))) {
841+
error("unable to create directory for %s", newref);
842+
goto rollback;
843+
}
844+
845+
retry:
846+
if (log && rename(git_path("tmp-renamed-log"), git_path("logs/%s", newref))) {
847+
if (errno==EISDIR) {
848+
if (remove_empty_directories(git_path("logs/%s", newref))) {
849+
error("Directory not empty: logs/%s", newref);
850+
goto rollback;
851+
}
852+
goto retry;
853+
} else {
854+
error("unable to move logfile tmp-renamed-log to logs/%s: %s",
855+
newref, strerror(errno));
856+
goto rollback;
857+
}
858+
}
859+
logmoved = log;
860+
861+
lock = lock_ref_sha1_basic(newref, NULL, NULL);
862+
if (!lock) {
863+
error("unable to lock %s for update", newref);
864+
goto rollback;
865+
}
866+
867+
lock->force_write = 1;
868+
hashcpy(lock->old_sha1, orig_sha1);
869+
if (write_ref_sha1(lock, orig_sha1, msg)) {
870+
error("unable to write current sha1 into %s", newref);
871+
goto rollback;
872+
}
873+
874+
return 0;
875+
876+
rollback:
877+
lock = lock_ref_sha1_basic(oldref, NULL, NULL);
878+
if (!lock) {
879+
error("unable to lock %s for rollback", oldref);
880+
goto rollbacklog;
881+
}
882+
883+
lock->force_write = 1;
884+
flag = log_all_ref_updates;
885+
log_all_ref_updates = 0;
886+
if (write_ref_sha1(lock, orig_sha1, NULL))
887+
error("unable to write current sha1 into %s", oldref);
888+
log_all_ref_updates = flag;
889+
890+
rollbacklog:
891+
if (logmoved && rename(git_path("logs/%s", newref), git_path("logs/%s", oldref)))
892+
error("unable to restore logfile %s from %s: %s",
893+
oldref, newref, strerror(errno));
894+
if (!logmoved && log &&
895+
rename(git_path("tmp-renamed-log"), git_path("logs/%s", oldref)))
896+
error("unable to restore logfile %s from tmp-renamed-log: %s",
897+
oldref, strerror(errno));
898+
899+
return 1;
900+
}
901+
779902
void unlock_ref(struct ref_lock *lock)
780903
{
781904
if (lock->lock_fd >= 0) {

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned
4747
/** Returns 0 if target has the right format for a ref. **/
4848
extern int check_ref_format(const char *target);
4949

50+
/** rename ref, return 0 on success **/
51+
extern int rename_ref(const char *oldref, const char *newref);
52+
5053
#endif /* REFS_H */

t/t3200-branch.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,38 @@ test_expect_success \
7070
git-branch -d l/m &&
7171
git-branch l'
7272

73+
test_expect_success \
74+
'git branch -m m m/m should work' \
75+
'git-branch -l m &&
76+
git-branch -m m m/m &&
77+
test -f .git/logs/refs/heads/m/m'
78+
79+
test_expect_success \
80+
'git branch -m n/n n should work' \
81+
'git-branch -l n/n &&
82+
git-branch -m n/n n
83+
test -f .git/logs/refs/heads/n'
84+
85+
test_expect_failure \
86+
'git branch -m o/o o should fail when o/p exists' \
87+
'git-branch o/o &&
88+
git-branch o/p &&
89+
git-branch -m o/o o'
90+
91+
test_expect_failure \
92+
'git branch -m q r/q should fail when r exists' \
93+
'git-branch q &&
94+
git-branch r &&
95+
git-branch -m q r/q'
96+
97+
test_expect_success \
98+
'git branch -m s/s s should work when s/t is deleted' \
99+
'git-branch -l s/s &&
100+
test -f .git/logs/refs/heads/s/s &&
101+
git-branch -l s/t &&
102+
test -f .git/logs/refs/heads/s/t &&
103+
git-branch -d s/t &&
104+
git-branch -m s/s s &&
105+
test -f .git/logs/refs/heads/s'
106+
73107
test_done

0 commit comments

Comments
 (0)