Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git branch" |
| 3 | * |
Junio C Hamano | d972cce | 2007-07-11 22:52:45 -0700 | [diff] [blame] | 4 | * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com> |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 5 | * Based on git-branch.sh by Junio C Hamano. |
| 6 | */ |
| 7 | |
| 8 | #include "cache.h" |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 9 | #include "color.h" |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 10 | #include "refs.h" |
| 11 | #include "commit.h" |
| 12 | #include "builtin.h" |
Johannes Schindelin | 6f084a5 | 2007-07-10 18:50:44 +0100 | [diff] [blame] | 13 | #include "remote.h" |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 14 | #include "parse-options.h" |
Daniel Barkalow | e496c00 | 2008-02-07 11:40:08 -0500 | [diff] [blame] | 15 | #include "branch.h" |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 16 | |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 17 | static const char * const builtin_branch_usage[] = { |
Lars Hjemli | e8b404c | 2008-04-17 22:24:50 +0200 | [diff] [blame] | 18 | "git-branch [options] [-r | -a] [--merged | --no-merged]", |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 19 | "git-branch [options] [-l] [-f] <branchname> [<start-point>]", |
| 20 | "git-branch [options] [-r] (-d | -D) <branchname>", |
| 21 | "git-branch [options] (-m | -M) [<oldbranch>] <newbranch>", |
| 22 | NULL |
| 23 | }; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 24 | |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 25 | #define REF_UNKNOWN_TYPE 0x00 |
| 26 | #define REF_LOCAL_BRANCH 0x01 |
| 27 | #define REF_REMOTE_BRANCH 0x02 |
| 28 | #define REF_TAG 0x04 |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 29 | |
| 30 | static const char *head; |
| 31 | static unsigned char head_sha1[20]; |
| 32 | |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 33 | static int branch_use_color = -1; |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 34 | static char branch_colors[][COLOR_MAXLEN] = { |
| 35 | "\033[m", /* reset */ |
| 36 | "", /* PLAIN (normal) */ |
| 37 | "\033[31m", /* REMOTE (red) */ |
Junio C Hamano | 753f96a | 2006-12-13 10:55:21 -0800 | [diff] [blame] | 38 | "", /* LOCAL (normal) */ |
| 39 | "\033[32m", /* CURRENT (green) */ |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 40 | }; |
| 41 | enum color_branch { |
| 42 | COLOR_BRANCH_RESET = 0, |
| 43 | COLOR_BRANCH_PLAIN = 1, |
| 44 | COLOR_BRANCH_REMOTE = 2, |
| 45 | COLOR_BRANCH_LOCAL = 3, |
| 46 | COLOR_BRANCH_CURRENT = 4, |
| 47 | }; |
| 48 | |
Lars Hjemli | e8b404c | 2008-04-17 22:24:50 +0200 | [diff] [blame] | 49 | static int mergefilter = -1; |
| 50 | |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 51 | static int parse_branch_color_slot(const char *var, int ofs) |
| 52 | { |
| 53 | if (!strcasecmp(var+ofs, "plain")) |
| 54 | return COLOR_BRANCH_PLAIN; |
| 55 | if (!strcasecmp(var+ofs, "reset")) |
| 56 | return COLOR_BRANCH_RESET; |
| 57 | if (!strcasecmp(var+ofs, "remote")) |
| 58 | return COLOR_BRANCH_REMOTE; |
| 59 | if (!strcasecmp(var+ofs, "local")) |
| 60 | return COLOR_BRANCH_LOCAL; |
| 61 | if (!strcasecmp(var+ofs, "current")) |
| 62 | return COLOR_BRANCH_CURRENT; |
| 63 | die("bad config variable '%s'", var); |
| 64 | } |
| 65 | |
Pierre Habouzit | 52fae7d | 2007-06-07 22:45:00 +0200 | [diff] [blame] | 66 | static int git_branch_config(const char *var, const char *value) |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 67 | { |
| 68 | if (!strcmp(var, "color.branch")) { |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 69 | branch_use_color = git_config_colorbool(var, value, -1); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 72 | if (!prefixcmp(var, "color.branch.")) { |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 73 | int slot = parse_branch_color_slot(var, 13); |
Junio C Hamano | 5768c98 | 2008-02-11 10:45:50 -0800 | [diff] [blame] | 74 | if (!value) |
| 75 | return config_error_nonbool(var); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 76 | color_parse(value, var, branch_colors[slot]); |
| 77 | return 0; |
| 78 | } |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 79 | return git_color_default_config(var, value); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Pierre Habouzit | 52fae7d | 2007-06-07 22:45:00 +0200 | [diff] [blame] | 82 | static const char *branch_get_color(enum color_branch ix) |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 83 | { |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 84 | if (branch_use_color > 0) |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 85 | return branch_colors[ix]; |
| 86 | return ""; |
| 87 | } |
| 88 | |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 89 | static int delete_branches(int argc, const char **argv, int force, int kinds) |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 90 | { |
Junio C Hamano | 67affd5 | 2006-11-24 23:10:23 -0800 | [diff] [blame] | 91 | struct commit *rev, *head_rev = head_rev; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 92 | unsigned char sha1[20]; |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 93 | char *name = NULL; |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 94 | const char *fmt, *remote; |
Gerrit Pape | f1eccba | 2007-06-09 12:40:35 +0000 | [diff] [blame] | 95 | char section[PATH_MAX]; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 96 | int i; |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 97 | int ret = 0; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 98 | |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 99 | switch (kinds) { |
| 100 | case REF_REMOTE_BRANCH: |
| 101 | fmt = "refs/remotes/%s"; |
| 102 | remote = "remote "; |
| 103 | force = 1; |
| 104 | break; |
| 105 | case REF_LOCAL_BRANCH: |
| 106 | fmt = "refs/heads/%s"; |
| 107 | remote = ""; |
| 108 | break; |
| 109 | default: |
| 110 | die("cannot use -a with -d"); |
| 111 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 112 | |
Junio C Hamano | 67affd5 | 2006-11-24 23:10:23 -0800 | [diff] [blame] | 113 | if (!force) { |
| 114 | head_rev = lookup_commit_reference(head_sha1); |
| 115 | if (!head_rev) |
| 116 | die("Couldn't look up commit object for HEAD"); |
| 117 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 118 | for (i = 0; i < argc; i++) { |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 119 | if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) { |
| 120 | error("Cannot delete the branch '%s' " |
| 121 | "which you are currently on.", argv[i]); |
| 122 | ret = 1; |
| 123 | continue; |
| 124 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 125 | |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 126 | free(name); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 127 | |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 128 | name = xstrdup(mkpath(fmt, argv[i])); |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 129 | if (!resolve_ref(name, sha1, 1, NULL)) { |
| 130 | error("%sbranch '%s' not found.", |
| 131 | remote, argv[i]); |
| 132 | ret = 1; |
| 133 | continue; |
| 134 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 135 | |
| 136 | rev = lookup_commit_reference(sha1); |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 137 | if (!rev) { |
| 138 | error("Couldn't look up commit object for '%s'", name); |
| 139 | ret = 1; |
| 140 | continue; |
| 141 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 142 | |
| 143 | /* This checks whether the merge bases of branch and |
| 144 | * HEAD contains branch -- which means that the HEAD |
| 145 | * contains everything in both. |
| 146 | */ |
| 147 | |
| 148 | if (!force && |
Junio C Hamano | 03840fc | 2007-01-08 23:22:31 -0800 | [diff] [blame] | 149 | !in_merge_bases(rev, &head_rev, 1)) { |
J. Bruce Fields | 00ae828 | 2007-11-02 22:39:44 -0400 | [diff] [blame] | 150 | error("The branch '%s' is not an ancestor of " |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 151 | "your current HEAD.\n" |
| 152 | "If you are sure you want to delete it, " |
| 153 | "run 'git branch -D %s'.", argv[i], argv[i]); |
| 154 | ret = 1; |
| 155 | continue; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 158 | if (delete_ref(name, sha1)) { |
| 159 | error("Error deleting %sbranch '%s'", remote, |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 160 | argv[i]); |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 161 | ret = 1; |
Gerrit Pape | f1eccba | 2007-06-09 12:40:35 +0000 | [diff] [blame] | 162 | } else { |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 163 | printf("Deleted %sbranch %s.\n", remote, argv[i]); |
Gerrit Pape | f1eccba | 2007-06-09 12:40:35 +0000 | [diff] [blame] | 164 | snprintf(section, sizeof(section), "branch.%s", |
| 165 | argv[i]); |
| 166 | if (git_config_rename_section(section, NULL) < 0) |
| 167 | warning("Update of config-file failed"); |
| 168 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 169 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 170 | |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 171 | free(name); |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 172 | |
| 173 | return(ret); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 174 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 175 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 176 | struct ref_item { |
| 177 | char *name; |
| 178 | unsigned int kind; |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 179 | unsigned char sha1[20]; |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | struct ref_list { |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 183 | int index, alloc, maxwidth; |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 184 | struct ref_item *list; |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 185 | struct commit_list *with_commit; |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 186 | int kinds; |
| 187 | }; |
| 188 | |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 189 | static int has_commit(const unsigned char *sha1, struct commit_list *with_commit) |
| 190 | { |
| 191 | struct commit *commit; |
| 192 | |
| 193 | if (!with_commit) |
| 194 | return 1; |
| 195 | commit = lookup_commit_reference_gently(sha1, 1); |
| 196 | if (!commit) |
| 197 | return 0; |
| 198 | while (with_commit) { |
| 199 | struct commit *other; |
| 200 | |
| 201 | other = with_commit->item; |
| 202 | with_commit = with_commit->next; |
| 203 | if (in_merge_bases(other, &commit, 1)) |
| 204 | return 1; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 209 | static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 210 | { |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 211 | struct ref_list *ref_list = (struct ref_list*)(cb_data); |
| 212 | struct ref_item *newitem; |
| 213 | int kind = REF_UNKNOWN_TYPE; |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 214 | int len; |
Lars Hjemli | e8b404c | 2008-04-17 22:24:50 +0200 | [diff] [blame] | 215 | static struct commit_list branch; |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 216 | |
| 217 | /* Detect kind */ |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 218 | if (!prefixcmp(refname, "refs/heads/")) { |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 219 | kind = REF_LOCAL_BRANCH; |
| 220 | refname += 11; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 221 | } else if (!prefixcmp(refname, "refs/remotes/")) { |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 222 | kind = REF_REMOTE_BRANCH; |
| 223 | refname += 13; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 224 | } else if (!prefixcmp(refname, "refs/tags/")) { |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 225 | kind = REF_TAG; |
| 226 | refname += 10; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 229 | /* Filter with with_commit if specified */ |
| 230 | if (!has_commit(sha1, ref_list->with_commit)) |
| 231 | return 0; |
| 232 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 233 | /* Don't add types the caller doesn't want */ |
| 234 | if ((kind & ref_list->kinds) == 0) |
| 235 | return 0; |
| 236 | |
Lars Hjemli | e8b404c | 2008-04-17 22:24:50 +0200 | [diff] [blame] | 237 | if (mergefilter > -1) { |
| 238 | branch.item = lookup_commit_reference_gently(sha1, 1); |
| 239 | if (!branch.item) |
| 240 | die("Unable to lookup tip of branch %s", refname); |
| 241 | if (mergefilter == 0 && has_commit(head_sha1, &branch)) |
| 242 | return 0; |
| 243 | if (mergefilter == 1 && !has_commit(head_sha1, &branch)) |
| 244 | return 0; |
| 245 | } |
| 246 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 247 | /* Resize buffer */ |
| 248 | if (ref_list->index >= ref_list->alloc) { |
| 249 | ref_list->alloc = alloc_nr(ref_list->alloc); |
| 250 | ref_list->list = xrealloc(ref_list->list, |
| 251 | ref_list->alloc * sizeof(struct ref_item)); |
| 252 | } |
| 253 | |
| 254 | /* Record the new item */ |
| 255 | newitem = &(ref_list->list[ref_list->index++]); |
| 256 | newitem->name = xstrdup(refname); |
| 257 | newitem->kind = kind; |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 258 | hashcpy(newitem->sha1, sha1); |
| 259 | len = strlen(newitem->name); |
| 260 | if (len > ref_list->maxwidth) |
| 261 | ref_list->maxwidth = len; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 266 | static void free_ref_list(struct ref_list *ref_list) |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 267 | { |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 268 | int i; |
| 269 | |
| 270 | for (i = 0; i < ref_list->index; i++) |
| 271 | free(ref_list->list[i].name); |
| 272 | free(ref_list->list); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 275 | static int ref_cmp(const void *r1, const void *r2) |
| 276 | { |
| 277 | struct ref_item *c1 = (struct ref_item *)(r1); |
| 278 | struct ref_item *c2 = (struct ref_item *)(r2); |
| 279 | |
| 280 | if (c1->kind != c2->kind) |
| 281 | return c1->kind - c2->kind; |
| 282 | return strcmp(c1->name, c2->name); |
| 283 | } |
| 284 | |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 285 | static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, |
| 286 | int abbrev, int current) |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 287 | { |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 288 | char c; |
| 289 | int color; |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 290 | struct commit *commit; |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 291 | |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 292 | switch (item->kind) { |
| 293 | case REF_LOCAL_BRANCH: |
| 294 | color = COLOR_BRANCH_LOCAL; |
| 295 | break; |
| 296 | case REF_REMOTE_BRANCH: |
| 297 | color = COLOR_BRANCH_REMOTE; |
| 298 | break; |
| 299 | default: |
| 300 | color = COLOR_BRANCH_PLAIN; |
| 301 | break; |
| 302 | } |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 303 | |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 304 | c = ' '; |
| 305 | if (current) { |
| 306 | c = '*'; |
| 307 | color = COLOR_BRANCH_CURRENT; |
| 308 | } |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 309 | |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 310 | if (verbose) { |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 311 | struct strbuf subject; |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 312 | const char *sub = " **** invalid ref ****"; |
| 313 | |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 314 | strbuf_init(&subject, 0); |
| 315 | |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 316 | commit = lookup_commit(item->sha1); |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 317 | if (commit && !parse_commit(commit)) { |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 318 | pretty_print_commit(CMIT_FMT_ONELINE, commit, |
Junio C Hamano | 4593fb8 | 2007-10-31 14:55:17 -0700 | [diff] [blame] | 319 | &subject, 0, NULL, NULL, 0, 0); |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 320 | sub = subject.buf; |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 321 | } |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 322 | printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color), |
| 323 | maxwidth, item->name, |
| 324 | branch_get_color(COLOR_BRANCH_RESET), |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 325 | find_unique_abbrev(item->sha1, abbrev), sub); |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 326 | strbuf_release(&subject); |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 327 | } else { |
| 328 | printf("%c %s%s%s\n", c, branch_get_color(color), item->name, |
| 329 | branch_get_color(COLOR_BRANCH_RESET)); |
| 330 | } |
Lars Hjemli | 75e6e21 | 2006-11-24 14:45:10 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 333 | static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit) |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 334 | { |
| 335 | int i; |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 336 | struct ref_list ref_list; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 337 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 338 | memset(&ref_list, 0, sizeof(ref_list)); |
| 339 | ref_list.kinds = kinds; |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 340 | ref_list.with_commit = with_commit; |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 341 | for_each_ref(append_ref, &ref_list); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 342 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 343 | qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 344 | |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 345 | detached = (detached && (kinds & REF_LOCAL_BRANCH)); |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 346 | if (detached && has_commit(head_sha1, with_commit)) { |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 347 | struct ref_item item; |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 348 | item.name = xstrdup("(no branch)"); |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 349 | item.kind = REF_LOCAL_BRANCH; |
| 350 | hashcpy(item.sha1, head_sha1); |
| 351 | if (strlen(item.name) > ref_list.maxwidth) |
| 352 | ref_list.maxwidth = strlen(item.name); |
| 353 | print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1); |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 354 | free(item.name); |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 357 | for (i = 0; i < ref_list.index; i++) { |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 358 | int current = !detached && |
| 359 | (ref_list.list[i].kind == REF_LOCAL_BRANCH) && |
Lars Hjemli | af0e4ac | 2007-01-03 21:10:09 +0100 | [diff] [blame] | 360 | !strcmp(ref_list.list[i].name, head); |
| 361 | print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose, |
| 362 | abbrev, current); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 363 | } |
Andy Parkins | bfcc921 | 2006-11-21 19:31:24 +0000 | [diff] [blame] | 364 | |
| 365 | free_ref_list(&ref_list); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 368 | static void rename_branch(const char *oldname, const char *newname, int force) |
| 369 | { |
Lars Hjemli | 678d0f4 | 2006-11-30 03:16:56 +0100 | [diff] [blame] | 370 | char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100]; |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 371 | unsigned char sha1[20]; |
Lars Hjemli | 19eba15 | 2007-04-06 14:13:00 +0200 | [diff] [blame] | 372 | char oldsection[PATH_MAX], newsection[PATH_MAX]; |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 373 | |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 374 | if (!oldname) |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 375 | die("cannot rename the current branch while not on any."); |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 376 | |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 377 | if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref)) |
| 378 | die("Old branchname too long"); |
| 379 | |
| 380 | if (check_ref_format(oldref)) |
| 381 | die("Invalid branch name: %s", oldref); |
| 382 | |
| 383 | if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref)) |
| 384 | die("New branchname too long"); |
| 385 | |
| 386 | if (check_ref_format(newref)) |
| 387 | die("Invalid branch name: %s", newref); |
| 388 | |
| 389 | if (resolve_ref(newref, sha1, 1, NULL) && !force) |
| 390 | die("A branch named '%s' already exists.", newname); |
| 391 | |
Lars Hjemli | 678d0f4 | 2006-11-30 03:16:56 +0100 | [diff] [blame] | 392 | snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s", |
| 393 | oldref, newref); |
| 394 | |
| 395 | if (rename_ref(oldref, newref, logmsg)) |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 396 | die("Branch rename failed"); |
| 397 | |
Nicolas Pitre | 8b5157e | 2007-01-26 17:26:10 -0500 | [diff] [blame] | 398 | /* no need to pass logmsg here as HEAD didn't really move */ |
| 399 | if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL)) |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 400 | die("Branch renamed to %s, but HEAD is not updated!", newname); |
Lars Hjemli | 19eba15 | 2007-04-06 14:13:00 +0200 | [diff] [blame] | 401 | |
| 402 | snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11); |
| 403 | snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11); |
| 404 | if (git_config_rename_section(oldsection, newsection) < 0) |
| 405 | die("Branch is renamed, but update of config-file failed"); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 408 | static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset) |
| 409 | { |
| 410 | unsigned char sha1[20]; |
| 411 | struct commit *commit; |
| 412 | |
| 413 | if (!arg) |
| 414 | return -1; |
| 415 | if (get_sha1(arg, sha1)) |
| 416 | die("malformed object name %s", arg); |
| 417 | commit = lookup_commit_reference(sha1); |
| 418 | if (!commit) |
| 419 | die("no such commit %s", arg); |
| 420 | commit_list_insert(commit, opt->value); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 424 | int cmd_branch(int argc, const char **argv, const char *prefix) |
| 425 | { |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 426 | int delete = 0, rename = 0, force_create = 0; |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 427 | int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0; |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 428 | int reflog = 0; |
| 429 | enum branch_track track; |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 430 | int kinds = REF_LOCAL_BRANCH; |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 431 | struct commit_list *with_commit = NULL; |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 432 | |
| 433 | struct option options[] = { |
| 434 | OPT_GROUP("Generic options"), |
| 435 | OPT__VERBOSE(&verbose), |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 436 | OPT_SET_INT( 0 , "track", &track, "set up tracking mode (see git-pull(1))", |
| 437 | BRANCH_TRACK_EXPLICIT), |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 438 | OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"), |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 439 | OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches", |
| 440 | REF_REMOTE_BRANCH), |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 441 | OPT_CALLBACK(0, "contains", &with_commit, "commit", |
| 442 | "print only branches that contain the commit", |
| 443 | opt_parse_with_commit), |
| 444 | { |
| 445 | OPTION_CALLBACK, 0, "with", &with_commit, "commit", |
| 446 | "print only branches that contain the commit", |
| 447 | PARSE_OPT_HIDDEN, opt_parse_with_commit, |
| 448 | }, |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 449 | OPT__ABBREV(&abbrev), |
| 450 | |
| 451 | OPT_GROUP("Specific git-branch actions:"), |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 452 | OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches", |
| 453 | REF_REMOTE_BRANCH | REF_LOCAL_BRANCH), |
| 454 | OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1), |
| 455 | OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2), |
| 456 | OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1), |
| 457 | OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2), |
| 458 | OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"), |
| 459 | OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"), |
Lars Hjemli | e8b404c | 2008-04-17 22:24:50 +0200 | [diff] [blame] | 460 | OPT_SET_INT(0, "merged", &mergefilter, "list only merged branches", 1), |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 461 | OPT_END(), |
| 462 | }; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 463 | |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 464 | git_config(git_branch_config); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 465 | |
| 466 | if (branch_use_color == -1) |
| 467 | branch_use_color = git_use_color_default; |
| 468 | |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 469 | track = git_branch_track; |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 470 | argc = parse_options(argc, argv, options, builtin_branch_usage, 0); |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 471 | if (!!delete + !!rename + !!force_create > 1) |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 472 | usage_with_options(builtin_branch_usage, options); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 473 | |
Jonas Fonseca | 078f838 | 2007-05-20 14:19:17 +0200 | [diff] [blame] | 474 | head = resolve_ref("HEAD", head_sha1, 0, NULL); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 475 | if (!head) |
| 476 | die("Failed to resolve HEAD as a valid ref."); |
Jonas Fonseca | 078f838 | 2007-05-20 14:19:17 +0200 | [diff] [blame] | 477 | head = xstrdup(head); |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 478 | if (!strcmp(head, "HEAD")) { |
| 479 | detached = 1; |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 480 | } else { |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 481 | if (prefixcmp(head, "refs/heads/")) |
Lars Hjemli | 0016a48 | 2007-01-03 21:10:10 +0100 | [diff] [blame] | 482 | die("HEAD not found below refs/heads!"); |
| 483 | head += 11; |
| 484 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 485 | |
| 486 | if (delete) |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 487 | return delete_branches(argc, argv, delete > 1, kinds); |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 488 | else if (argc == 0) |
Junio C Hamano | 694a577 | 2007-11-07 14:58:09 -0800 | [diff] [blame] | 489 | print_ref_list(kinds, detached, verbose, abbrev, with_commit); |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 490 | else if (rename && (argc == 1)) |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 491 | rename_branch(head, argv[0], rename > 1); |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 492 | else if (rename && (argc == 2)) |
Pierre Habouzit | d11d44f | 2007-11-07 11:20:28 +0100 | [diff] [blame] | 493 | rename_branch(argv[0], argv[1], rename > 1); |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 494 | else if (argc <= 2) |
Daniel Barkalow | e496c00 | 2008-02-07 11:40:08 -0500 | [diff] [blame] | 495 | create_branch(head, argv[0], (argc == 2) ? argv[1] : head, |
Junio C Hamano | 45994a1 | 2007-03-08 13:59:54 -0800 | [diff] [blame] | 496 | force_create, reflog, track); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 497 | else |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 498 | usage_with_options(builtin_branch_usage, options); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |