From 75e6e2132006770156d9df1881b4114862919c94 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Fri, 24 Nov 2006 14:45:10 +0100 Subject: Add -v and --abbrev options to git-branch The new -v option makes git-branch show the abbreviated sha1 + subjectline for each branch. Additionally, minimum abbreviation length can be specified with --abbrev= Signed-off-by: Lars Hjemli Signed-off-by: Junio C Hamano --- builtin-branch.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 22e3285a4c..69b7b55d86 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) | [-l] [-f] [] | [-r] | [-a]"; +"git-branch (-d | -D) | [-l] [-f] [] | [-r | -a] [-v] [--abbrev=] "; static const char *head; @@ -87,10 +87,11 @@ static void delete_branches(int argc, const char **argv, int force) struct ref_item { char *name; unsigned int kind; + unsigned char sha1[20]; }; struct ref_list { - int index, alloc; + int index, alloc, maxwidth; struct ref_item *list; int kinds; }; @@ -100,6 +101,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags, struct ref_list *ref_list = (struct ref_list*)(cb_data); struct ref_item *newitem; int kind = REF_UNKNOWN_TYPE; + int len; /* Detect kind */ if (!strncmp(refname, "refs/heads/", 11)) { @@ -128,6 +130,10 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags, newitem = &(ref_list->list[ref_list->index++]); newitem->name = xstrdup(refname); newitem->kind = kind; + hashcpy(newitem->sha1, sha1); + len = strlen(newitem->name); + if (len > ref_list->maxwidth) + ref_list->maxwidth = len; return 0; } @@ -151,7 +157,24 @@ static int ref_cmp(const void *r1, const void *r2) return strcmp(c1->name, c2->name); } -static void print_ref_list(int kinds) +static void print_ref_info(const unsigned char *sha1, int abbrev) +{ + struct commit *commit; + char subject[256]; + + + commit = lookup_commit(sha1); + if (commit && !parse_commit(commit)) + pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, + subject, sizeof(subject), 0, + NULL, NULL, 0); + else + strcpy(subject, " **** invalid ref ****"); + + printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject); +} + +static void print_ref_list(int kinds, int verbose, int abbrev) { int i; char c; @@ -169,7 +192,13 @@ static void print_ref_list(int kinds) !strcmp(ref_list.list[i].name, head)) c = '*'; - printf("%c %s\n", c, ref_list.list[i].name); + if (verbose) { + printf("%c %-*s", c, ref_list.maxwidth, + ref_list.list[i].name); + print_ref_info(ref_list.list[i].sha1, abbrev); + } + else + printf("%c %s\n", c, ref_list.list[i].name); } free_ref_list(&ref_list); @@ -215,6 +244,7 @@ static void create_branch(const char *name, const char *start, int cmd_branch(int argc, const char **argv, const char *prefix) { int delete = 0, force_delete = 0, force_create = 0; + int verbose = 0, abbrev = DEFAULT_ABBREV; int reflog = 0; int kinds = REF_LOCAL_BRANCH; int i; @@ -255,6 +285,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix) reflog = 1; continue; } + if (!strncmp(arg, "--abbrev=", 9)) { + abbrev = atoi(arg+9); + continue; + } + if (!strcmp(arg, "-v")) { + verbose = 1; + continue; + } usage(builtin_branch_usage); } @@ -268,7 +306,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (delete) delete_branches(argc - i, argv + i, force_delete); else if (i == argc) - print_ref_list(kinds); + print_ref_list(kinds, verbose, abbrev); else if (i == argc - 1) create_branch(argv[i], head, force_create, reflog); else if (i == argc - 2) -- cgit v1.2.1 From 67affd5173da059ca60aab7896985331acacd9b4 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 24 Nov 2006 23:10:23 -0800 Subject: git-branch -D: make it work even when on a yet-to-be-born branch This makes "git branch -D other_branch" work even when HEAD points at a yet-to-be-born branch. Earlier, we checked the HEAD ref for the purpose of "subset" check even when the deletion was forced (i.e. not -d but -D). Because of this, you cannot delete a branch even with -D while on a yet-to-be-born branch. With this change, the following sequence that now works: mkdir newdir && cd newdir git init-db git fetch -k $other_repo refs/heads/master:refs/heads/othre # oops, typo git branch other othre git branch -D othre Signed-off-by: Junio C Hamano --- builtin-branch.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 69b7b55d86..3d5cb0e4b2 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -38,12 +38,16 @@ static int in_merge_bases(const unsigned char *sha1, static void delete_branches(int argc, const char **argv, int force) { - struct commit *rev, *head_rev; + struct commit *rev, *head_rev = head_rev; unsigned char sha1[20]; char *name; int i; - head_rev = lookup_commit_reference(head_sha1); + if (!force) { + head_rev = lookup_commit_reference(head_sha1); + if (!head_rev) + die("Couldn't look up commit object for HEAD"); + } for (i = 0; i < argc; i++) { if (!strcmp(head, argv[i])) die("Cannot delete the branch you are currently on."); @@ -53,8 +57,8 @@ static void delete_branches(int argc, const char **argv, int force) die("Branch '%s' not found.", argv[i]); rev = lookup_commit_reference(sha1); - if (!rev || !head_rev) - die("Couldn't look up commit objects."); + if (!rev) + die("Couldn't look up commit object for '%s'", name); /* This checks whether the merge bases of branch and * HEAD contains branch -- which means that the HEAD -- cgit v1.2.1 From c976d415e5352886f0650f8e2edba81866c38587 Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Tue, 28 Nov 2006 15:47:40 +0100 Subject: git-branch: add options and tests for branch renaming Extend git-branch with the following options: git-branch -m|-M [] 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 Signed-off-by: Junio C Hamano --- builtin-branch.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'builtin-branch.c') 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) | [-l] [-f] [] | [-r | -a] [-v] [--abbrev=] "; + "git-branch (-d | -D) | [-l] [-f] [] | (-m | -M) [] | [-r | -a] [-v [--abbrev=]]"; 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) -- cgit v1.2.1 From 678d0f4cbfa7a3b529c6e894f2977bef6a2d3e4c Mon Sep 17 00:00:00 2001 From: Lars Hjemli Date: Thu, 30 Nov 2006 03:16:56 +0100 Subject: git-branch: let caller specify logmsg This changes the signature of rename_ref() in refs.[hc] to include a logmessage for the reflogs. Also, builtin-branch.c is modified to provide a proper logmessage + call setup_ident() before any logmessages are written. Signed-off-by: Lars Hjemli Signed-off-by: Junio C Hamano --- builtin-branch.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 153682601e..3fc6f84773 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -247,7 +247,7 @@ static void create_branch(const char *name, const char *start, static void rename_branch(const char *oldname, const char *newname, int force) { - char oldref[PATH_MAX], newref[PATH_MAX]; + 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)) @@ -265,7 +265,10 @@ static void rename_branch(const char *oldname, const char *newname, int force) if (resolve_ref(newref, sha1, 1, NULL) && !force) die("A branch named '%s' already exists.", newname); - if (rename_ref(oldref, newref)) + 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)) @@ -281,6 +284,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) int kinds = REF_LOCAL_BRANCH; int i; + setup_ident(); git_config(git_default_config); for (i = 1; i < argc; i++) { -- cgit v1.2.1 From a1158caeadec4d48b185068f4120d85423de5970 Mon Sep 17 00:00:00 2001 From: Andy Parkins Date: Tue, 12 Dec 2006 06:41:52 +0000 Subject: Colourise git-branch output I wanted to have a visual indication of which branches are local and which are remote in git-branch -a output; however Junio was concerned that someone might be using the output in a script. This patch addresses the problem by colouring the git-branch output - which in "auto" mode won't be activated. I've based it off the colouring code for builtin-diff.c; which means there is a branch color configuration variable that needs setting to something before the color will appear. The colour parameter is "color.branch" rather than "branch.color" to avoid clashing with the default namespace for default branch merge definitions. This patch chooses green for local, red for remote and bold green for current. Signed-off-by: Andy Parkins Signed-off-by: Junio C Hamano --- builtin-branch.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 5 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 3d5cb0e4b2..7c87b8d579 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -5,6 +5,7 @@ * Based on git-branch.sh by Junio C Hamano. */ +#include "color.h" #include "cache.h" #include "refs.h" #include "commit.h" @@ -17,6 +18,58 @@ static const char builtin_branch_usage[] = static const char *head; static unsigned char head_sha1[20]; +static int branch_use_color; +static char branch_colors[][COLOR_MAXLEN] = { + "\033[m", /* reset */ + "", /* PLAIN (normal) */ + "\033[31m", /* REMOTE (red) */ + "\033[32m", /* LOCAL (green) */ + "\033[1;32m", /* CURRENT (boldgreen) */ +}; +enum color_branch { + COLOR_BRANCH_RESET = 0, + COLOR_BRANCH_PLAIN = 1, + COLOR_BRANCH_REMOTE = 2, + COLOR_BRANCH_LOCAL = 3, + COLOR_BRANCH_CURRENT = 4, +}; + +static int parse_branch_color_slot(const char *var, int ofs) +{ + if (!strcasecmp(var+ofs, "plain")) + return COLOR_BRANCH_PLAIN; + if (!strcasecmp(var+ofs, "reset")) + return COLOR_BRANCH_RESET; + if (!strcasecmp(var+ofs, "remote")) + return COLOR_BRANCH_REMOTE; + if (!strcasecmp(var+ofs, "local")) + return COLOR_BRANCH_LOCAL; + if (!strcasecmp(var+ofs, "current")) + return COLOR_BRANCH_CURRENT; + die("bad config variable '%s'", var); +} + +int git_branch_config(const char *var, const char *value) +{ + if (!strcmp(var, "color.branch")) { + branch_use_color = git_config_colorbool(var, value); + return 0; + } + if (!strncmp(var, "color.branch.", 13)) { + int slot = parse_branch_color_slot(var, 13); + color_parse(value, var, branch_colors[slot]); + return 0; + } + return git_default_config(var, value); +} + +const char *branch_get_color(enum color_branch ix) +{ + if (branch_use_color) + return branch_colors[ix]; + return ""; +} + static int in_merge_bases(const unsigned char *sha1, struct commit *rev1, struct commit *rev2) @@ -183,6 +236,7 @@ static void print_ref_list(int kinds, int verbose, int abbrev) int i; char c; struct ref_list ref_list; + int color; memset(&ref_list, 0, sizeof(ref_list)); ref_list.kinds = kinds; @@ -191,18 +245,38 @@ static void print_ref_list(int kinds, int verbose, int abbrev) qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); for (i = 0; i < ref_list.index; i++) { + switch( ref_list.list[i].kind ) { + case REF_LOCAL_BRANCH: + color = COLOR_BRANCH_LOCAL; + break; + case REF_REMOTE_BRANCH: + color = COLOR_BRANCH_REMOTE; + break; + default: + color = COLOR_BRANCH_PLAIN; + break; + } + c = ' '; if (ref_list.list[i].kind == REF_LOCAL_BRANCH && - !strcmp(ref_list.list[i].name, head)) + !strcmp(ref_list.list[i].name, head)) { c = '*'; + color = COLOR_BRANCH_CURRENT; + } if (verbose) { - printf("%c %-*s", c, ref_list.maxwidth, - ref_list.list[i].name); + printf("%c %s%-*s%s", c, + branch_get_color(color), + ref_list.maxwidth, + ref_list.list[i].name, + branch_get_color(COLOR_BRANCH_RESET)); print_ref_info(ref_list.list[i].sha1, abbrev); } else - printf("%c %s\n", c, ref_list.list[i].name); + printf("%c %s%s%s\n", c, + branch_get_color(color), + ref_list.list[i].name, + branch_get_color(COLOR_BRANCH_RESET)); } free_ref_list(&ref_list); @@ -253,7 +327,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) int kinds = REF_LOCAL_BRANCH; int i; - git_config(git_default_config); + git_config(git_branch_config); for (i = 1; i < argc; i++) { const char *arg = argv[i]; @@ -297,6 +371,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix) verbose = 1; continue; } + if (!strcmp(arg, "--color")) { + branch_use_color = 1; + continue; + } + if (!strcmp(arg, "--no-color")) { + branch_use_color = 0; + continue; + } usage(builtin_branch_usage); } -- cgit v1.2.1 From 753f96a455534ad60b670376fb3d89179281e541 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 13 Dec 2006 10:55:21 -0800 Subject: branch --color: change default color selection. Showing local and remote branches in green and red was simply overkill, as all we wanted was to make it easy to tell them apart (local ones can be built on top by committing, but the remote tracking ones can't). Use plain coloring for local branches and paint remotes in red. Signed-off-by: Junio C Hamano --- builtin-branch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 7c87b8d579..d1c243d372 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -23,8 +23,8 @@ static char branch_colors[][COLOR_MAXLEN] = { "\033[m", /* reset */ "", /* PLAIN (normal) */ "\033[31m", /* REMOTE (red) */ - "\033[32m", /* LOCAL (green) */ - "\033[1;32m", /* CURRENT (boldgreen) */ + "", /* LOCAL (normal) */ + "\033[32m", /* CURRENT (green) */ }; enum color_branch { COLOR_BRANCH_RESET = 0, -- cgit v1.2.1 From f3d985c38090b77e46dcc00d42574b9ba49bdc22 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 17 Dec 2006 23:58:16 -0800 Subject: Teach git-branch to delete tracking branches with -r -d Because -r already means "remote" when listing, you can say: $ git branch -d -r origin/todo origin/html origin/man I just twisted it not to check fast-forwardness with the current branch when you are removing a tracking branch. Most likely, removal of a tracking branch is not because you are "done with" it (for a local branch, it usually means "you merged it up"), but because you are not even interested in it. In other words, remote tracking branches are more like tags than branches. Signed-off-by: Junio C Hamano --- builtin-branch.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 560309cb15..7fb93e7796 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -12,8 +12,12 @@ #include "builtin.h" static const char builtin_branch_usage[] = - "git-branch (-d | -D) | [-l] [-f] [] | (-m | -M) [] | [-r | -a] [-v [--abbrev=]]"; + "git-branch [-r] (-d | -D) | [-l] [-f] [] | (-m | -M) [] | [-r | -a] [-v [--abbrev=]]"; +#define REF_UNKNOWN_TYPE 0x00 +#define REF_LOCAL_BRANCH 0x01 +#define REF_REMOTE_BRANCH 0x02 +#define REF_TAG 0x04 static const char *head; static unsigned char head_sha1[20]; @@ -89,25 +93,40 @@ static int in_merge_bases(const unsigned char *sha1, return ret; } -static void delete_branches(int argc, const char **argv, int force) +static void delete_branches(int argc, const char **argv, int force, int kinds) { struct commit *rev, *head_rev = head_rev; unsigned char sha1[20]; char *name; + const char *fmt, *remote; int i; + switch (kinds) { + case REF_REMOTE_BRANCH: + fmt = "refs/remotes/%s"; + remote = "remote "; + force = 1; + break; + case REF_LOCAL_BRANCH: + fmt = "refs/heads/%s"; + remote = ""; + break; + default: + die("cannot use -a with -d"); + } + if (!force) { head_rev = lookup_commit_reference(head_sha1); if (!head_rev) die("Couldn't look up commit object for HEAD"); } for (i = 0; i < argc; i++) { - if (!strcmp(head, argv[i])) + if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) die("Cannot delete the branch you are currently on."); - name = xstrdup(mkpath("refs/heads/%s", argv[i])); + name = xstrdup(mkpath(fmt, argv[i])); if (!resolve_ref(name, sha1, 1, NULL)) - die("Branch '%s' not found.", argv[i]); + die("%sbranch '%s' not found.", remote, argv[i]); rev = lookup_commit_reference(sha1); if (!rev) @@ -128,19 +147,15 @@ static void delete_branches(int argc, const char **argv, int force) } if (delete_ref(name, sha1)) - printf("Error deleting branch '%s'\n", argv[i]); + printf("Error deleting %sbranch '%s'\n", remote, + argv[i]); else - printf("Deleted branch %s.\n", argv[i]); + printf("Deleted %sbranch %s.\n", remote, argv[i]); free(name); } } -#define REF_UNKNOWN_TYPE 0x00 -#define REF_LOCAL_BRANCH 0x01 -#define REF_REMOTE_BRANCH 0x02 -#define REF_TAG 0x04 - struct ref_item { char *name; unsigned int kind; @@ -435,7 +450,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) head += 11; if (delete) - delete_branches(argc - i, argv + i, force_delete); + delete_branches(argc - i, argv + i, force_delete, kinds); else if (i == argc) print_ref_list(kinds, verbose, abbrev); else if (rename && (i == argc - 1)) -- cgit v1.2.1 From b8e9a00d40839a76928c54366a1c46ad54d7fd5d Mon Sep 17 00:00:00 2001 From: Quy Tonthat Date: Tue, 19 Dec 2006 09:42:16 +1100 Subject: git-branch -d: do not stop at the first failure. If there are more than one branches to be deleted, failure on one will no longer stop git-branch to process the next ones. The command still reports failures by exitting non-zero status. Signed-off-by: Quy Tonthat Signed-off-by: Junio C Hamano --- builtin-branch.c | 57 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 7fb93e7796..011ef3a265 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -93,13 +93,14 @@ static int in_merge_bases(const unsigned char *sha1, return ret; } -static void delete_branches(int argc, const char **argv, int force, int kinds) +static int delete_branches(int argc, const char **argv, int force, int kinds) { struct commit *rev, *head_rev = head_rev; unsigned char sha1[20]; - char *name; + char *name = NULL; const char *fmt, *remote; int i; + int ret = 0; switch (kinds) { case REF_REMOTE_BRANCH: @@ -121,16 +122,30 @@ static void delete_branches(int argc, const char **argv, int force, int kinds) die("Couldn't look up commit object for HEAD"); } for (i = 0; i < argc; i++) { - if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) - die("Cannot delete the branch you are currently on."); + if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) { + error("Cannot delete the branch '%s' " + "which you are currently on.", argv[i]); + ret = 1; + continue; + } + + if (name) + free(name); name = xstrdup(mkpath(fmt, argv[i])); - if (!resolve_ref(name, sha1, 1, NULL)) - die("%sbranch '%s' not found.", remote, argv[i]); + if (!resolve_ref(name, sha1, 1, NULL)) { + error("%sbranch '%s' not found.", + remote, argv[i]); + ret = 1; + continue; + } rev = lookup_commit_reference(sha1); - if (!rev) - die("Couldn't look up commit object for '%s'", name); + if (!rev) { + error("Couldn't look up commit object for '%s'", name); + ret = 1; + continue; + } /* This checks whether the merge bases of branch and * HEAD contains branch -- which means that the HEAD @@ -139,21 +154,27 @@ static void delete_branches(int argc, const char **argv, int force, int kinds) if (!force && !in_merge_bases(sha1, rev, head_rev)) { - fprintf(stderr, - "The branch '%s' is not a strict subset of your current HEAD.\n" - "If you are sure you want to delete it, run 'git branch -D %s'.\n", - argv[i], argv[i]); - exit(1); + error("The branch '%s' is not a strict subset of " + "your current HEAD.\n" + "If you are sure you want to delete it, " + "run 'git branch -D %s'.", argv[i], argv[i]); + ret = 1; + continue; } - if (delete_ref(name, sha1)) - printf("Error deleting %sbranch '%s'\n", remote, + if (delete_ref(name, sha1)) { + error("Error deleting %sbranch '%s'", remote, argv[i]); - else + ret = 1; + } else printf("Deleted %sbranch %s.\n", remote, argv[i]); - free(name); } + + if (name) + free(name); + + return(ret); } struct ref_item { @@ -450,7 +471,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) head += 11; if (delete) - delete_branches(argc - i, argv + i, force_delete, kinds); + return delete_branches(argc - i, argv + i, force_delete, kinds); else if (i == argc) print_ref_list(kinds, verbose, abbrev); else if (rename && (i == argc - 1)) -- cgit v1.2.1 From 85023577a8f4b540aa64aa37f6f44578c0c305a3 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 19 Dec 2006 14:34:12 -0800 Subject: simplify inclusion of system header files. This is a mechanical clean-up of the way *.c files include system header files. (1) sources under compat/, platform sha-1 implementations, and xdelta code are exempt from the following rules; (2) the first #include must be "git-compat-util.h" or one of our own header file that includes it first (e.g. config.h, builtin.h, pkt-line.h); (3) system headers that are included in "git-compat-util.h" need not be included in individual C source files. (4) "git-compat-util.h" does not have to include subsystem specific header files (e.g. expat.h). Signed-off-by: Junio C Hamano --- builtin-branch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 560309cb15..515330c155 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -5,8 +5,8 @@ * Based on git-branch.sh by Junio C Hamano. */ -#include "color.h" #include "cache.h" +#include "color.h" #include "refs.h" #include "commit.h" #include "builtin.h" -- cgit v1.2.1 From 2ecd2bbcbe5335c1d9209b6ce28513e4e9d3491b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 19 Dec 2006 00:14:04 -0800 Subject: Move in_merge_bases() to commit.c This reasonably useful function was hidden inside builtin-branch.c --- builtin-branch.c | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) (limited to 'builtin-branch.c') diff --git a/builtin-branch.c b/builtin-branch.c index 903d5cf056..745ee04d6e 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -74,25 +74,6 @@ const char *branch_get_color(enum color_branch ix) return ""; } -static int in_merge_bases(const unsigned char *sha1, - struct commit *rev1, - struct commit *rev2) -{ - struct commit_list *bases, *b; - int ret = 0; - - bases = get_merge_bases(rev1, rev2, 1); - for (b = bases; b; b = b->next) { - if (!hashcmp(sha1, b->item->object.sha1)) { - ret = 1; - break; - } - } - - free_commit_list(bases); - return ret; -} - static int delete_branches(int argc, const char **argv, int force, int kinds) { struct commit *rev, *head_rev = head_rev; @@ -153,7 +134,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds) */ if (!force && - !in_merge_bases(sha1, rev, head_rev)) { + !in_merge_bases(rev, head_rev)) { error("The branch '%s' is not a strict subset of " "your current HEAD.\n" "If you are sure you want to delete it, " -- cgit v1.2.1