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

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 <hjemli@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Lars Hjemli committed Nov 28, 2006 at 15:47 UTC c976d415e5352886f0650f8e2edba81866c38587
5 files changed +250 -26
Documentation/git-branch.txt
+21 -2
@@ -8,8 +8,9 @@ git-branch - List, create, or delete branches.
8 SYNOPSIS
9 --------
10 [verse]
11 -'git-branch' [-r] [-a] [-v] [--abbrev=<length>]
11 +'git-branch' [-r | -a] [-v [--abbrev=<length>]]
12 'git-branch' [-l] [-f] <branchname> [<start-point>]
13 +'git-branch' (-m | -M) [<oldbranch>] <newbranch>
14 'git-branch' (-d | -D) <branchname>...
15
16 DESCRIPTION
@@ -24,6 +25,12 @@ It will start out with a head equal to the one given as <start-point>.
25 If no <start-point> is given, the branch will be created with a head
26 equal to that of the currently checked out branch.
27
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 +
34 With a `-d` or `-D` option, `<branchname>` will be deleted. You may
35 specify more than one branch for deletion. If the branch currently
36 has a ref log then the ref log will also be deleted.
@@ -46,6 +53,12 @@ OPTIONS
53 Force the creation of a new branch even if it means deleting
54 a branch that already exists with the same name.
55
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 +
62 -r::
63 List the remote-tracking branches.
64
@@ -53,7 +66,7 @@ OPTIONS
66 List both remote-tracking branches and local branches.
67
68 -v::
56 - Show sha1 and subject message for each head.
69 + Show sha1 and commit subjectline for each head.
70
71 --abbrev=<length>::
72 Alter minimum display length for sha1 in output listing,
@@ -70,6 +83,12 @@ OPTIONS
83 be given as a branch name, a commit-id, or a tag. If this option
84 is omitted, the current branch is assumed.
85
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.
92
93
94 Examples
builtin-branch.c
+46 -1
@@ -11,7 +11,7 @@
11 #include "builtin.h"
12
13 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>]]";
15
16
17 static const char *head;
@@ -245,9 +245,37 @@ static void create_branch(const char *name, const char *start,
245 die("Failed to write ref: %s.", strerror(errno));
246 }
247
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 +
275 int cmd_branch(int argc, const char **argv, const char *prefix)
276 {
277 int delete = 0, force_delete = 0, force_create = 0;
278 + int rename = 0, force_rename = 0;
279 int verbose = 0, abbrev = DEFAULT_ABBREV;
280 int reflog = 0;
281 int kinds = REF_LOCAL_BRANCH;
@@ -277,6 +305,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
305 force_create = 1;
306 continue;
307 }
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 + }
317 if (!strcmp(arg, "-r")) {
318 kinds = REF_REMOTE_BRANCH;
319 continue;
@@ -300,6 +337,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
337 usage(builtin_branch_usage);
338 }
339
340 + if ((delete && rename) || (delete && force_create) ||
341 + (rename && force_create))
342 + usage(builtin_branch_usage);
343 +
344 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
345 if (!head)
346 die("Failed to resolve HEAD as a valid ref.");
@@ -311,6 +352,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
352 delete_branches(argc - i, argv + i, force_delete);
353 else if (i == argc)
354 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);
359 else if (i == argc - 1)
360 create_branch(argv[i], head, force_create, reflog);
361 else if (i == argc - 2)
refs.c
+146 -23
@@ -610,6 +610,29 @@ static int remove_empty_directories(char *file)
610 return remove_empty_dir_recursive(path, len);
611 }
612
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 +
636 static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
637 {
638 char *ref_file;
@@ -643,29 +666,14 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
666 orig_ref, strerror(errno));
667 goto error_return;
668 }
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;
677
678 lock->lk = xcalloc(1, sizeof(struct lock_file));
679
@@ -776,6 +784,121 @@ int delete_ref(const char *refname, unsigned char *sha1)
784 return ret;
785 }
786
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 +
902 void unlock_ref(struct ref_lock *lock)
903 {
904 if (lock->lock_fd >= 0) {
refs.h
+3
@@ -47,4 +47,7 @@ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned
47 /** Returns 0 if target has the right format for a ref. **/
48 extern int check_ref_format(const char *target);
49
50 +/** rename ref, return 0 on success **/
51 +extern int rename_ref(const char *oldref, const char *newref);
52 +
53 #endif /* REFS_H */
t/t3200-branch.sh
+34
@@ -70,4 +70,38 @@ test_expect_success \
70 git-branch -d l/m &&
71 git-branch l'
72
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 +
107 test_done