summaryrefslogtreecommitdiff
path: root/builtin-branch.c
diff options
context:
space:
mode:
authorLars Hjemli <hjemli@gmail.com>2006-11-28 15:47:40 +0100
committerJunio C Hamano <junkio@cox.net>2006-12-05 23:50:57 -0800
commitc976d415e5352886f0650f8e2edba81866c38587 (patch)
tree49731f2430197df384f3259a899a0bba8156e284 /builtin-branch.c
parent8ebe185bbf3f1f4f59bcc61e3d1849a76f6af983 (diff)
downloadgit-c976d415e5352886f0650f8e2edba81866c38587.tar.gz
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>
Diffstat (limited to 'builtin-branch.c')
-rw-r--r--builtin-branch.c47
1 files changed, 46 insertions, 1 deletions
diff --git a/builtin-branch.c b/builtin-branch.c
index 3d5cb0e4b2..153682601e 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -11,7 +11,7 @@
#include "builtin.h"
static const char builtin_branch_usage[] =
-"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r | -a] [-v] [--abbrev=<length>] ";
+ "git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [-r | -a] [-v [--abbrev=<length>]]";
static const char *head;
@@ -245,9 +245,37 @@ 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];
+
+ if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
+ die("Old branchname too long");
+
+ if (check_ref_format(oldref))
+ die("Invalid branch name: %s", oldref);
+
+ if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
+ die("New branchname too long");
+
+ 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.", newname);
+
+ if (rename_ref(oldref, newref))
+ die("Branch rename failed");
+
+ if (!strcmp(oldname, head) && create_symref("HEAD", newref))
+ die("Branch renamed to %s, but HEAD is not updated!", newname);
+}
+
int cmd_branch(int argc, const char **argv, const char *prefix)
{
int delete = 0, force_delete = 0, force_create = 0;
+ int rename = 0, force_rename = 0;
int verbose = 0, abbrev = DEFAULT_ABBREV;
int reflog = 0;
int kinds = REF_LOCAL_BRANCH;
@@ -277,6 +305,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
force_create = 1;
continue;
}
+ if (!strcmp(arg, "-m")) {
+ rename = 1;
+ continue;
+ }
+ if (!strcmp(arg, "-M")) {
+ rename = 1;
+ force_rename = 1;
+ continue;
+ }
if (!strcmp(arg, "-r")) {
kinds = REF_REMOTE_BRANCH;
continue;
@@ -300,6 +337,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
usage(builtin_branch_usage);
}
+ if ((delete && rename) || (delete && force_create) ||
+ (rename && force_create))
+ usage(builtin_branch_usage);
+
head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
if (!head)
die("Failed to resolve HEAD as a valid ref.");
@@ -311,6 +352,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_rename);
+ else if (rename && (i == argc - 2))
+ rename_branch(argv[i], argv[i + 1], force_rename);
else if (i == argc - 1)
create_branch(argv[i], head, force_create, reflog);
else if (i == argc - 2)