Skip to contentSkip to conversation

Teach git-branch howto rename a branch

PATCH In review Lars Hjemli 3 participants 4 messages Aug 11, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Teach git-branch howto rename a branch

This adds a '--rename' option to git branch. If specified, branch creation becomes branch renaming.

With a single branchname, the current branch is renamed and .git/HEAD is updated.

With two branchnames, the second name is renamed to the first.

---

This seems to do the right thing for both refs and reflogs, but 'make test' probably should be expanded with some evil test-cases to confirm my manual testing.

 builtin-branch.c |   30 ++++++++++-
 refs.c           |  162 ++++++++++++++++++++++++++++++++++++++++++++++--------
 refs.h           |    3 +
 3 files changed, 171 insertions(+), 24 deletions(-)
builtin-branch.c +29 -1
index 3d5cb0e..2a21263 100644
@@ -245,9 +245,29 @@ static void create_branch(const char *name, const char *start,
 		die("Failed to write ref: %s.", strerror(errno));
 }
 
+static void rename_branch(const char *oldname, const char *newname, int force)
+{
+	char oldref[PATH_MAX], newref[PATH_MAX];
+	unsigned char sha1[20];
+
+	snprintf(oldref, sizeof oldref, "refs/heads/%s", oldname);
+	if (check_ref_format(oldref))
+		die("Invalid branch name: %s", oldref);
+
+	snprintf(newref, sizeof newref, "refs/heads/%s", newname);
+	if (check_ref_format(newref))
+		die("Invalid branch name: %s", newref);
+
+	if (resolve_ref(newref, sha1, 1, NULL) && !force)
+		die("A branch named '%s' already exists.\n", newname);
+
+	if (!rename_ref(oldref, newref) && !strcmp(oldname, head))
+		create_symref("HEAD", newref);
+}
+
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
-	int delete = 0, force_delete = 0, force_create = 0;
+	int delete = 0, rename = 0, force_delete = 0, force_create = 0;
 	int verbose = 0, abbrev = DEFAULT_ABBREV;
 	int reflog = 0;
 	int kinds = REF_LOCAL_BRANCH;
@@ -277,6 +297,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			force_create = 1;
 			continue;
 		}
+		if (!strcmp(arg, "--rename")) {
+			rename = 1;
+			continue;
+		}
 		if (!strcmp(arg, "-r")) {
 			kinds = REF_REMOTE_BRANCH;
 			continue;
@@ -311,6 +335,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		delete_branches(argc - i, argv + i, force_delete);
 	else if (i == argc)
 		print_ref_list(kinds, verbose, abbrev);
+	else if (rename && (i == argc - 1))
+		rename_branch(head, argv[i], force_create);
+	else if (rename && (i == argc - 2))
+		rename_branch(argv[i + 1], argv[i], force_create);
 	else if (i == argc - 1)
 		create_branch(argv[i], head, force_create, reflog);
 	else if (i == argc - 2)
refs.c +139 -23

180 lines not on this page · raw message

refs.h +3 -0
index a57d437..61419db 100644
@@ -44,4 +44,7 @@ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned
 /** Returns 0 if target has the right format for a ref. **/
 extern int check_ref_format(const char *target);
 
+/** rename ref, return 0 on success **/
+extern int rename_ref(const char *oldref, const char *newref);
+
 #endif /* REFS_H */
Signature · 1 line
1.4.4.1.gf64d
#2 lore raw
In reply to Junio C Hamano
Quoted 9 lines · Junio C Hamano
Quoted 2 lines · Lars Hjemli

With two branchnames, the second name is renamed to the first.

Thanks.

"--rename newname oldname" feels funny, as already mentioned a few times on the list. rename(2) is "rename(old, new)" and mv(1) is "mv old new".

Ok, how about

git branch [-m|-M] [oldbranch] newbranch

where -m is 'move' and -M is 'force move'?

Quoted 6 lines
builtin-branch.c +3 -0
+       if (!rename_ref(oldref, newref) && !strcmp(oldname, head))
+               create_symref("HEAD", newref);
+}

Can create_symref() fail?

Yes... But what can been done if/when it fails? create_symref() already seems to be pretty verbose about errors, so the only thing I can think of is to return the errorcode to the caller (which I should have done in the first place, git-branch ought to have a usable exitcode)

Quoted 10 lines
refs.c +8 -0
+int rename_ref(const char *oldref, const char *newref)
+{
+       unsigned char sha1[20], orig_sha1[20];
+       int flag = 0, logmoved = 0;
+       struct ref_lock *lock;
+       char msg[PATH_MAX*2 + 100];
+       struct stat stat;
+       int log = !lstat(git_path("logs/%s", oldref), &stat);
Quoted 4 lines

This is not wrong per-se, but it made me stop and wonder if we want to error out when we find out "logs/oldref" is a symlink; I do not think we care about it that much, but in that case we may want to say stat() here instead... Just a minor detail.

Well, it's a good point. If it's a symlink that's a pretty strong indication that someone has been messing with the log for some reason, so to error out is probably the right thing to do.

Quoted 11 lines
refs.c +9 -0
+       lock = lock_ref_sha1_basic("tmp-renamed-ref", NULL, NULL);
+       if (!lock)
+               return error("unable to lock tmp-renamed-ref");
+       lock->force_write = 1;
+       if (write_ref_sha1(lock, orig_sha1, msg))
+               return error("unable to save current sha1 in tmp-renamed-ref");
+       if (log && rename(git_path("logs/%s", oldref), git_path("tmp-renamed-log")))
+               return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
+                       oldref, strerror(errno));
Quoted 4 lines

I am confused with this code. tmp-renamed-ref is not even a ref, you lock $GIT_DIR/tmp-renamed-ref and call write-ref_sha1() with an uninitialized msg[] buffer to write into a logfile. What is the name of that logfile? $GIT_DIR/log/tmp-renamed-ref???

My goal was to save the ref in a tmp-file before deleting the old ref, not to log the event. I think of it as a way to get out of trouble if rename_ref should fail badly.

Btw: I't _might_ be interesting to have $GIT_DIR/logs/tmp-renamed-ref (or something similar) as a branch-independent log of branch renames

Anyway, I'l fix up the mentioned issues in a new patch

--

#3 lore raw
In reply to Lars Hjemli
Quoted 7 lines · Lars Hjemli

This adds a '--rename' option to git branch. If specified, branch creation becomes branch renaming.

With a single branchname, the current branch is renamed and .git/HEAD is updated.

With two branchnames, the second name is renamed to the first.

Could you please document this new feature?

And wouldn't it be better to use <source> <target>, aka <branch> <newname> like in git-mv and other rename/move commands? "Second name is renamed to the first" although fits with "<branchname> [<start-point>]" is not natural for rename.

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
#1 lore raw
In reply to Lars Hjemli
Quoted 7 lines · Lars Hjemli

This adds a '--rename' option to git branch. If specified, branch creation becomes branch renaming.

With a single branchname, the current branch is renamed and .git/HEAD is updated.

With two branchnames, the second name is renamed to the first.

Thanks.

"--rename newname oldname" feels funny, as already mentioned a few times on the list. rename(2) is "rename(old, new)" and mv(1) is "mv old new".

Quoted 3 lines

This seems to do the right thing for both refs and reflogs, but 'make test' probably should be expanded with some evil test-cases to confirm my manual testing.

Certainly. Let's keep discipline to have tests and docs for new features.

+static void rename_branch(const char *oldname, const char *newname, int force) +{
+ char oldref[PATH_MAX], newref[PATH_MAX];
+ unsigned char sha1[20];
+
+ snprintf(oldref, sizeof oldref, "refs/heads/%s", oldname);
+ if (check_ref_format(oldref))
+ die("Invalid branch name: %s", oldref);

Although "sizeof type" is valid C, we tend to prefer "sizeof(type)". If you use snprintf(), it would make sense to check its return value.

+ if (resolve_ref(newref, sha1, 1, NULL) && !force)
+ die("A branch named '%s' already exists.\n", newname);

No trailing '\n' is necessary for die().

+ if (!rename_ref(oldref, newref) && !strcmp(oldname, head))
+ create_symref("HEAD", newref);
+}

Can create_symref() fail?

+int rename_ref(const char *oldref, const char *newref) +{
+ unsigned char sha1[20], orig_sha1[20];
+ int flag = 0, logmoved = 0;
+ struct ref_lock *lock;
+ char msg[PATH_MAX*2 + 100];
+ struct stat stat;
+ int log = !lstat(git_path("logs/%s", oldref), &stat);

This is not wrong per-se, but it made me stop and wonder if we want to error out when we find out "logs/oldref" is a symlink; I do not think we care about it that much, but in that case we may want to say stat() here instead... Just a minor detail.

+ lock = lock_ref_sha1_basic("tmp-renamed-ref", NULL, NULL);
+ if (!lock)
+ return error("unable to lock tmp-renamed-ref");
+ lock->force_write = 1;
+ if (write_ref_sha1(lock, orig_sha1, msg))
+ return error("unable to save current sha1 in tmp-renamed-ref");
+ if (log && rename(git_path("logs/%s", oldref), git_path("tmp-renamed-log")))
+ return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
+ oldref, strerror(errno));

I am confused with this code. tmp-renamed-ref is not even a ref, you lock $GIT_DIR/tmp-renamed-ref and call write-ref_sha1() with an uninitialized msg[] buffer to write into a logfile. What is the name of that logfile? $GIT_DIR/log/tmp-renamed-ref???