69b7b55d86ffb70243d69a5b61de839bff8ccf60
   1/*
   2 * Builtin "git branch"
   3 *
   4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
   5 * Based on git-branch.sh by Junio C Hamano.
   6 */
   7
   8#include "cache.h"
   9#include "refs.h"
  10#include "commit.h"
  11#include "builtin.h"
  12
  13static const char builtin_branch_usage[] =
  14"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r | -a] [-v] [--abbrev=<length>] ";
  15
  16
  17static const char *head;
  18static unsigned char head_sha1[20];
  19
  20static int in_merge_bases(const unsigned char *sha1,
  21                          struct commit *rev1,
  22                          struct commit *rev2)
  23{
  24        struct commit_list *bases, *b;
  25        int ret = 0;
  26
  27        bases = get_merge_bases(rev1, rev2, 1);
  28        for (b = bases; b; b = b->next) {
  29                if (!hashcmp(sha1, b->item->object.sha1)) {
  30                        ret = 1;
  31                        break;
  32                }
  33        }
  34
  35        free_commit_list(bases);
  36        return ret;
  37}
  38
  39static void delete_branches(int argc, const char **argv, int force)
  40{
  41        struct commit *rev, *head_rev;
  42        unsigned char sha1[20];
  43        char *name;
  44        int i;
  45
  46        head_rev = lookup_commit_reference(head_sha1);
  47        for (i = 0; i < argc; i++) {
  48                if (!strcmp(head, argv[i]))
  49                        die("Cannot delete the branch you are currently on.");
  50
  51                name = xstrdup(mkpath("refs/heads/%s", argv[i]));
  52                if (!resolve_ref(name, sha1, 1, NULL))
  53                        die("Branch '%s' not found.", argv[i]);
  54
  55                rev = lookup_commit_reference(sha1);
  56                if (!rev || !head_rev)
  57                        die("Couldn't look up commit objects.");
  58
  59                /* This checks whether the merge bases of branch and
  60                 * HEAD contains branch -- which means that the HEAD
  61                 * contains everything in both.
  62                 */
  63
  64                if (!force &&
  65                    !in_merge_bases(sha1, rev, head_rev)) {
  66                        fprintf(stderr,
  67                                "The branch '%s' is not a strict subset of your current HEAD.\n"
  68                                "If you are sure you want to delete it, run 'git branch -D %s'.\n",
  69                                argv[i], argv[i]);
  70                        exit(1);
  71                }
  72
  73                if (delete_ref(name, sha1))
  74                        printf("Error deleting branch '%s'\n", argv[i]);
  75                else
  76                        printf("Deleted branch %s.\n", argv[i]);
  77
  78                free(name);
  79        }
  80}
  81
  82#define REF_UNKNOWN_TYPE    0x00
  83#define REF_LOCAL_BRANCH    0x01
  84#define REF_REMOTE_BRANCH   0x02
  85#define REF_TAG             0x04
  86
  87struct ref_item {
  88        char *name;
  89        unsigned int kind;
  90        unsigned char sha1[20];
  91};
  92
  93struct ref_list {
  94        int index, alloc, maxwidth;
  95        struct ref_item *list;
  96        int kinds;
  97};
  98
  99static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 100{
 101        struct ref_list *ref_list = (struct ref_list*)(cb_data);
 102        struct ref_item *newitem;
 103        int kind = REF_UNKNOWN_TYPE;
 104        int len;
 105
 106        /* Detect kind */
 107        if (!strncmp(refname, "refs/heads/", 11)) {
 108                kind = REF_LOCAL_BRANCH;
 109                refname += 11;
 110        } else if (!strncmp(refname, "refs/remotes/", 13)) {
 111                kind = REF_REMOTE_BRANCH;
 112                refname += 13;
 113        } else if (!strncmp(refname, "refs/tags/", 10)) {
 114                kind = REF_TAG;
 115                refname += 10;
 116        }
 117
 118        /* Don't add types the caller doesn't want */
 119        if ((kind & ref_list->kinds) == 0)
 120                return 0;
 121
 122        /* Resize buffer */
 123        if (ref_list->index >= ref_list->alloc) {
 124                ref_list->alloc = alloc_nr(ref_list->alloc);
 125                ref_list->list = xrealloc(ref_list->list,
 126                                ref_list->alloc * sizeof(struct ref_item));
 127        }
 128
 129        /* Record the new item */
 130        newitem = &(ref_list->list[ref_list->index++]);
 131        newitem->name = xstrdup(refname);
 132        newitem->kind = kind;
 133        hashcpy(newitem->sha1, sha1);
 134        len = strlen(newitem->name);
 135        if (len > ref_list->maxwidth)
 136                ref_list->maxwidth = len;
 137
 138        return 0;
 139}
 140
 141static void free_ref_list(struct ref_list *ref_list)
 142{
 143        int i;
 144
 145        for (i = 0; i < ref_list->index; i++)
 146                free(ref_list->list[i].name);
 147        free(ref_list->list);
 148}
 149
 150static int ref_cmp(const void *r1, const void *r2)
 151{
 152        struct ref_item *c1 = (struct ref_item *)(r1);
 153        struct ref_item *c2 = (struct ref_item *)(r2);
 154
 155        if (c1->kind != c2->kind)
 156                return c1->kind - c2->kind;
 157        return strcmp(c1->name, c2->name);
 158}
 159
 160static void print_ref_info(const unsigned char *sha1, int abbrev)
 161{
 162        struct commit *commit;
 163        char subject[256];
 164
 165
 166        commit = lookup_commit(sha1);
 167        if (commit && !parse_commit(commit))
 168                pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
 169                                    subject, sizeof(subject), 0,
 170                                    NULL, NULL, 0);
 171        else
 172                strcpy(subject, " **** invalid ref ****");
 173
 174        printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
 175}
 176
 177static void print_ref_list(int kinds, int verbose, int abbrev)
 178{
 179        int i;
 180        char c;
 181        struct ref_list ref_list;
 182
 183        memset(&ref_list, 0, sizeof(ref_list));
 184        ref_list.kinds = kinds;
 185        for_each_ref(append_ref, &ref_list);
 186
 187        qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
 188
 189        for (i = 0; i < ref_list.index; i++) {
 190                c = ' ';
 191                if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
 192                                !strcmp(ref_list.list[i].name, head))
 193                        c = '*';
 194
 195                if (verbose) {
 196                        printf("%c %-*s", c, ref_list.maxwidth,
 197                               ref_list.list[i].name);
 198                        print_ref_info(ref_list.list[i].sha1, abbrev);
 199                }
 200                else
 201                        printf("%c %s\n", c, ref_list.list[i].name);
 202        }
 203
 204        free_ref_list(&ref_list);
 205}
 206
 207static void create_branch(const char *name, const char *start,
 208                          int force, int reflog)
 209{
 210        struct ref_lock *lock;
 211        struct commit *commit;
 212        unsigned char sha1[20];
 213        char ref[PATH_MAX], msg[PATH_MAX + 20];
 214
 215        snprintf(ref, sizeof ref, "refs/heads/%s", name);
 216        if (check_ref_format(ref))
 217                die("'%s' is not a valid branch name.", name);
 218
 219        if (resolve_ref(ref, sha1, 1, NULL)) {
 220                if (!force)
 221                        die("A branch named '%s' already exists.", name);
 222                else if (!strcmp(head, name))
 223                        die("Cannot force update the current branch.");
 224        }
 225
 226        if (get_sha1(start, sha1) ||
 227            (commit = lookup_commit_reference(sha1)) == NULL)
 228                die("Not a valid branch point: '%s'.", start);
 229        hashcpy(sha1, commit->object.sha1);
 230
 231        lock = lock_any_ref_for_update(ref, NULL);
 232        if (!lock)
 233                die("Failed to lock ref for update: %s.", strerror(errno));
 234
 235        if (reflog) {
 236                log_all_ref_updates = 1;
 237                snprintf(msg, sizeof msg, "branch: Created from %s", start);
 238        }
 239
 240        if (write_ref_sha1(lock, sha1, msg) < 0)
 241                die("Failed to write ref: %s.", strerror(errno));
 242}
 243
 244int cmd_branch(int argc, const char **argv, const char *prefix)
 245{
 246        int delete = 0, force_delete = 0, force_create = 0;
 247        int verbose = 0, abbrev = DEFAULT_ABBREV;
 248        int reflog = 0;
 249        int kinds = REF_LOCAL_BRANCH;
 250        int i;
 251
 252        git_config(git_default_config);
 253
 254        for (i = 1; i < argc; i++) {
 255                const char *arg = argv[i];
 256
 257                if (arg[0] != '-')
 258                        break;
 259                if (!strcmp(arg, "--")) {
 260                        i++;
 261                        break;
 262                }
 263                if (!strcmp(arg, "-d")) {
 264                        delete = 1;
 265                        continue;
 266                }
 267                if (!strcmp(arg, "-D")) {
 268                        delete = 1;
 269                        force_delete = 1;
 270                        continue;
 271                }
 272                if (!strcmp(arg, "-f")) {
 273                        force_create = 1;
 274                        continue;
 275                }
 276                if (!strcmp(arg, "-r")) {
 277                        kinds = REF_REMOTE_BRANCH;
 278                        continue;
 279                }
 280                if (!strcmp(arg, "-a")) {
 281                        kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
 282                        continue;
 283                }
 284                if (!strcmp(arg, "-l")) {
 285                        reflog = 1;
 286                        continue;
 287                }
 288                if (!strncmp(arg, "--abbrev=", 9)) {
 289                        abbrev = atoi(arg+9);
 290                        continue;
 291                }
 292                if (!strcmp(arg, "-v")) {
 293                        verbose = 1;
 294                        continue;
 295                }
 296                usage(builtin_branch_usage);
 297        }
 298
 299        head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
 300        if (!head)
 301                die("Failed to resolve HEAD as a valid ref.");
 302        if (strncmp(head, "refs/heads/", 11))
 303                die("HEAD not found below refs/heads!");
 304        head += 11;
 305
 306        if (delete)
 307                delete_branches(argc - i, argv + i, force_delete);
 308        else if (i == argc)
 309                print_ref_list(kinds, verbose, abbrev);
 310        else if (i == argc - 1)
 311                create_branch(argv[i], head, force_create, reflog);
 312        else if (i == argc - 2)
 313                create_branch(argv[i], argv[i + 1], force_create, reflog);
 314        else
 315                usage(builtin_branch_usage);
 316
 317        return 0;
 318}