builtin-branch.con commit branch -m/-M: remove undocumented RENAMED-REF (b0eab01)
   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>] | (-m | -M) [<oldbranch>] <newbranch> | [-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 = head_rev;
  42        unsigned char sha1[20];
  43        char *name;
  44        int i;
  45
  46        if (!force) {
  47                head_rev = lookup_commit_reference(head_sha1);
  48                if (!head_rev)
  49                        die("Couldn't look up commit object for HEAD");
  50        }
  51        for (i = 0; i < argc; i++) {
  52                if (!strcmp(head, argv[i]))
  53                        die("Cannot delete the branch you are currently on.");
  54
  55                name = xstrdup(mkpath("refs/heads/%s", argv[i]));
  56                if (!resolve_ref(name, sha1, 1, NULL))
  57                        die("Branch '%s' not found.", argv[i]);
  58
  59                rev = lookup_commit_reference(sha1);
  60                if (!rev)
  61                        die("Couldn't look up commit object for '%s'", name);
  62
  63                /* This checks whether the merge bases of branch and
  64                 * HEAD contains branch -- which means that the HEAD
  65                 * contains everything in both.
  66                 */
  67
  68                if (!force &&
  69                    !in_merge_bases(sha1, rev, head_rev)) {
  70                        fprintf(stderr,
  71                                "The branch '%s' is not a strict subset of your current HEAD.\n"
  72                                "If you are sure you want to delete it, run 'git branch -D %s'.\n",
  73                                argv[i], argv[i]);
  74                        exit(1);
  75                }
  76
  77                if (delete_ref(name, sha1))
  78                        printf("Error deleting branch '%s'\n", argv[i]);
  79                else
  80                        printf("Deleted branch %s.\n", argv[i]);
  81
  82                free(name);
  83        }
  84}
  85
  86#define REF_UNKNOWN_TYPE    0x00
  87#define REF_LOCAL_BRANCH    0x01
  88#define REF_REMOTE_BRANCH   0x02
  89#define REF_TAG             0x04
  90
  91struct ref_item {
  92        char *name;
  93        unsigned int kind;
  94        unsigned char sha1[20];
  95};
  96
  97struct ref_list {
  98        int index, alloc, maxwidth;
  99        struct ref_item *list;
 100        int kinds;
 101};
 102
 103static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 104{
 105        struct ref_list *ref_list = (struct ref_list*)(cb_data);
 106        struct ref_item *newitem;
 107        int kind = REF_UNKNOWN_TYPE;
 108        int len;
 109
 110        /* Detect kind */
 111        if (!strncmp(refname, "refs/heads/", 11)) {
 112                kind = REF_LOCAL_BRANCH;
 113                refname += 11;
 114        } else if (!strncmp(refname, "refs/remotes/", 13)) {
 115                kind = REF_REMOTE_BRANCH;
 116                refname += 13;
 117        } else if (!strncmp(refname, "refs/tags/", 10)) {
 118                kind = REF_TAG;
 119                refname += 10;
 120        }
 121
 122        /* Don't add types the caller doesn't want */
 123        if ((kind & ref_list->kinds) == 0)
 124                return 0;
 125
 126        /* Resize buffer */
 127        if (ref_list->index >= ref_list->alloc) {
 128                ref_list->alloc = alloc_nr(ref_list->alloc);
 129                ref_list->list = xrealloc(ref_list->list,
 130                                ref_list->alloc * sizeof(struct ref_item));
 131        }
 132
 133        /* Record the new item */
 134        newitem = &(ref_list->list[ref_list->index++]);
 135        newitem->name = xstrdup(refname);
 136        newitem->kind = kind;
 137        hashcpy(newitem->sha1, sha1);
 138        len = strlen(newitem->name);
 139        if (len > ref_list->maxwidth)
 140                ref_list->maxwidth = len;
 141
 142        return 0;
 143}
 144
 145static void free_ref_list(struct ref_list *ref_list)
 146{
 147        int i;
 148
 149        for (i = 0; i < ref_list->index; i++)
 150                free(ref_list->list[i].name);
 151        free(ref_list->list);
 152}
 153
 154static int ref_cmp(const void *r1, const void *r2)
 155{
 156        struct ref_item *c1 = (struct ref_item *)(r1);
 157        struct ref_item *c2 = (struct ref_item *)(r2);
 158
 159        if (c1->kind != c2->kind)
 160                return c1->kind - c2->kind;
 161        return strcmp(c1->name, c2->name);
 162}
 163
 164static void print_ref_info(const unsigned char *sha1, int abbrev)
 165{
 166        struct commit *commit;
 167        char subject[256];
 168
 169
 170        commit = lookup_commit(sha1);
 171        if (commit && !parse_commit(commit))
 172                pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
 173                                    subject, sizeof(subject), 0,
 174                                    NULL, NULL, 0);
 175        else
 176                strcpy(subject, " **** invalid ref ****");
 177
 178        printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
 179}
 180
 181static void print_ref_list(int kinds, int verbose, int abbrev)
 182{
 183        int i;
 184        char c;
 185        struct ref_list ref_list;
 186
 187        memset(&ref_list, 0, sizeof(ref_list));
 188        ref_list.kinds = kinds;
 189        for_each_ref(append_ref, &ref_list);
 190
 191        qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
 192
 193        for (i = 0; i < ref_list.index; i++) {
 194                c = ' ';
 195                if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
 196                                !strcmp(ref_list.list[i].name, head))
 197                        c = '*';
 198
 199                if (verbose) {
 200                        printf("%c %-*s", c, ref_list.maxwidth,
 201                               ref_list.list[i].name);
 202                        print_ref_info(ref_list.list[i].sha1, abbrev);
 203                }
 204                else
 205                        printf("%c %s\n", c, ref_list.list[i].name);
 206        }
 207
 208        free_ref_list(&ref_list);
 209}
 210
 211static void create_branch(const char *name, const char *start,
 212                          int force, int reflog)
 213{
 214        struct ref_lock *lock;
 215        struct commit *commit;
 216        unsigned char sha1[20];
 217        char ref[PATH_MAX], msg[PATH_MAX + 20];
 218
 219        snprintf(ref, sizeof ref, "refs/heads/%s", name);
 220        if (check_ref_format(ref))
 221                die("'%s' is not a valid branch name.", name);
 222
 223        if (resolve_ref(ref, sha1, 1, NULL)) {
 224                if (!force)
 225                        die("A branch named '%s' already exists.", name);
 226                else if (!strcmp(head, name))
 227                        die("Cannot force update the current branch.");
 228        }
 229
 230        if (get_sha1(start, sha1) ||
 231            (commit = lookup_commit_reference(sha1)) == NULL)
 232                die("Not a valid branch point: '%s'.", start);
 233        hashcpy(sha1, commit->object.sha1);
 234
 235        lock = lock_any_ref_for_update(ref, NULL);
 236        if (!lock)
 237                die("Failed to lock ref for update: %s.", strerror(errno));
 238
 239        if (reflog) {
 240                log_all_ref_updates = 1;
 241                snprintf(msg, sizeof msg, "branch: Created from %s", start);
 242        }
 243
 244        if (write_ref_sha1(lock, sha1, msg) < 0)
 245                die("Failed to write ref: %s.", strerror(errno));
 246}
 247
 248static void rename_branch(const char *oldname, const char *newname, int force)
 249{
 250        char oldref[PATH_MAX], newref[PATH_MAX];
 251        unsigned char sha1[20];
 252
 253        if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
 254                die("Old branchname too long");
 255
 256        if (check_ref_format(oldref))
 257                die("Invalid branch name: %s", oldref);
 258
 259        if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
 260                die("New branchname too long");
 261
 262        if (check_ref_format(newref))
 263                die("Invalid branch name: %s", newref);
 264
 265        if (resolve_ref(newref, sha1, 1, NULL) && !force)
 266                die("A branch named '%s' already exists.", newname);
 267
 268        if (rename_ref(oldref, newref))
 269                die("Branch rename failed");
 270
 271        if (!strcmp(oldname, head) && create_symref("HEAD", newref))
 272                die("Branch renamed to %s, but HEAD is not updated!", newname);
 273}
 274
 275int cmd_branch(int argc, const char **argv, const char *prefix)
 276{
 277        int delete = 0, force_delete = 0, force_create = 0;
 278        int rename = 0, force_rename = 0;
 279        int verbose = 0, abbrev = DEFAULT_ABBREV;
 280        int reflog = 0;
 281        int kinds = REF_LOCAL_BRANCH;
 282        int i;
 283
 284        git_config(git_default_config);
 285
 286        for (i = 1; i < argc; i++) {
 287                const char *arg = argv[i];
 288
 289                if (arg[0] != '-')
 290                        break;
 291                if (!strcmp(arg, "--")) {
 292                        i++;
 293                        break;
 294                }
 295                if (!strcmp(arg, "-d")) {
 296                        delete = 1;
 297                        continue;
 298                }
 299                if (!strcmp(arg, "-D")) {
 300                        delete = 1;
 301                        force_delete = 1;
 302                        continue;
 303                }
 304                if (!strcmp(arg, "-f")) {
 305                        force_create = 1;
 306                        continue;
 307                }
 308                if (!strcmp(arg, "-m")) {
 309                        rename = 1;
 310                        continue;
 311                }
 312                if (!strcmp(arg, "-M")) {
 313                        rename = 1;
 314                        force_rename = 1;
 315                        continue;
 316                }
 317                if (!strcmp(arg, "-r")) {
 318                        kinds = REF_REMOTE_BRANCH;
 319                        continue;
 320                }
 321                if (!strcmp(arg, "-a")) {
 322                        kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
 323                        continue;
 324                }
 325                if (!strcmp(arg, "-l")) {
 326                        reflog = 1;
 327                        continue;
 328                }
 329                if (!strncmp(arg, "--abbrev=", 9)) {
 330                        abbrev = atoi(arg+9);
 331                        continue;
 332                }
 333                if (!strcmp(arg, "-v")) {
 334                        verbose = 1;
 335                        continue;
 336                }
 337                usage(builtin_branch_usage);
 338        }
 339
 340        if ((delete && rename) || (delete && force_create) ||
 341            (rename && force_create))
 342                usage(builtin_branch_usage);
 343
 344        head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
 345        if (!head)
 346                die("Failed to resolve HEAD as a valid ref.");
 347        if (strncmp(head, "refs/heads/", 11))
 348                die("HEAD not found below refs/heads!");
 349        head += 11;
 350
 351        if (delete)
 352                delete_branches(argc - i, argv + i, force_delete);
 353        else if (i == argc)
 354                print_ref_list(kinds, verbose, abbrev);
 355        else if (rename && (i == argc - 1))
 356                rename_branch(head, argv[i], force_rename);
 357        else if (rename && (i == argc - 2))
 358                rename_branch(argv[i], argv[i + 1], force_rename);
 359        else if (i == argc - 1)
 360                create_branch(argv[i], head, force_create, reflog);
 361        else if (i == argc - 2)
 362                create_branch(argv[i], argv[i + 1], force_create, reflog);
 363        else
 364                usage(builtin_branch_usage);
 365
 366        return 0;
 367}