summaryrefslogtreecommitdiff
path: root/builtin-branch.c
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2006-12-13 11:07:51 -0800
committerJunio C Hamano <junkio@cox.net>2006-12-13 11:07:51 -0800
commitabcb49cb56fc6fa047a8838cd47fbdfa48791798 (patch)
tree5381eab9fd6fc651250d2db8635e2f557675ff7c /builtin-branch.c
parent490e092defd01ff645457cde4e96bc0d0d534ccd (diff)
parent678d0f4cbfa7a3b529c6e894f2977bef6a2d3e4c (diff)
downloadgit-abcb49cb56fc6fa047a8838cd47fbdfa48791798.tar.gz
Merge branch 'lh/branch-rename'
* lh/branch-rename: git-branch: let caller specify logmsg rename_ref: use lstat(2) when testing for symlink git-branch: add options and tests for branch renaming Conflicts: builtin-branch.c
Diffstat (limited to 'builtin-branch.c')
-rw-r--r--builtin-branch.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/builtin-branch.c b/builtin-branch.c
index d1c243d372..560309cb15 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -12,7 +12,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;
@@ -319,14 +319,46 @@ 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], logmsg[PATH_MAX*2 + 100];
+ 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);
+
+ snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
+ oldref, newref);
+
+ if (rename_ref(oldref, newref, logmsg))
+ 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;
int i;
+ setup_ident();
git_config(git_branch_config);
for (i = 1; i < argc; i++) {
@@ -351,6 +383,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;
@@ -382,6 +423,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.");
@@ -393,6 +438,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)