builtin-branch.con commit refs outside refs/{heads,tags} match less strongly. (29561ad)
   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]";
  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
  82static int ref_index, ref_alloc;
  83static char **ref_list;
  84
  85static int append_ref(const char *refname, const unsigned char *sha1, int flags,
  86                void *cb_data)
  87{
  88        if (ref_index >= ref_alloc) {
  89                ref_alloc = alloc_nr(ref_alloc);
  90                ref_list = xrealloc(ref_list, ref_alloc * sizeof(char *));
  91        }
  92
  93        ref_list[ref_index++] = xstrdup(refname);
  94
  95        return 0;
  96}
  97
  98static int ref_cmp(const void *r1, const void *r2)
  99{
 100        return strcmp(*(char **)r1, *(char **)r2);
 101}
 102
 103static void print_ref_list(int remote_only)
 104{
 105        int i;
 106        char c;
 107
 108        if (remote_only)
 109                for_each_remote_ref(append_ref, NULL);
 110        else
 111                for_each_branch_ref(append_ref, NULL);
 112
 113        qsort(ref_list, ref_index, sizeof(char *), ref_cmp);
 114
 115        for (i = 0; i < ref_index; i++) {
 116                c = ' ';
 117                if (!strcmp(ref_list[i], head))
 118                        c = '*';
 119
 120                printf("%c %s\n", c, ref_list[i]);
 121        }
 122}
 123
 124static void create_branch(const char *name, const char *start,
 125                          int force, int reflog)
 126{
 127        struct ref_lock *lock;
 128        struct commit *commit;
 129        unsigned char sha1[20];
 130        char ref[PATH_MAX], msg[PATH_MAX + 20];
 131
 132        snprintf(ref, sizeof ref, "refs/heads/%s", name);
 133        if (check_ref_format(ref))
 134                die("'%s' is not a valid branch name.", name);
 135
 136        if (resolve_ref(ref, sha1, 1, NULL)) {
 137                if (!force)
 138                        die("A branch named '%s' already exists.", name);
 139                else if (!strcmp(head, name))
 140                        die("Cannot force update the current branch.");
 141        }
 142
 143        if (get_sha1(start, sha1) ||
 144            (commit = lookup_commit_reference(sha1)) == NULL)
 145                die("Not a valid branch point: '%s'.", start);
 146        hashcpy(sha1, commit->object.sha1);
 147
 148        lock = lock_any_ref_for_update(ref, NULL);
 149        if (!lock)
 150                die("Failed to lock ref for update: %s.", strerror(errno));
 151
 152        if (reflog) {
 153                log_all_ref_updates = 1;
 154                snprintf(msg, sizeof msg, "branch: Created from %s", start);
 155        }
 156
 157        if (write_ref_sha1(lock, sha1, msg) < 0)
 158                die("Failed to write ref: %s.", strerror(errno));
 159}
 160
 161int cmd_branch(int argc, const char **argv, const char *prefix)
 162{
 163        int delete = 0, force_delete = 0, force_create = 0, remote_only = 0;
 164        int reflog = 0;
 165        int i;
 166
 167        git_config(git_default_config);
 168
 169        for (i = 1; i < argc; i++) {
 170                const char *arg = argv[i];
 171
 172                if (arg[0] != '-')
 173                        break;
 174                if (!strcmp(arg, "--")) {
 175                        i++;
 176                        break;
 177                }
 178                if (!strcmp(arg, "-d")) {
 179                        delete = 1;
 180                        continue;
 181                }
 182                if (!strcmp(arg, "-D")) {
 183                        delete = 1;
 184                        force_delete = 1;
 185                        continue;
 186                }
 187                if (!strcmp(arg, "-f")) {
 188                        force_create = 1;
 189                        continue;
 190                }
 191                if (!strcmp(arg, "-r")) {
 192                        remote_only = 1;
 193                        continue;
 194                }
 195                if (!strcmp(arg, "-l")) {
 196                        reflog = 1;
 197                        continue;
 198                }
 199                usage(builtin_branch_usage);
 200        }
 201
 202        head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
 203        if (!head)
 204                die("Failed to resolve HEAD as a valid ref.");
 205        if (strncmp(head, "refs/heads/", 11))
 206                die("HEAD not found below refs/heads!");
 207        head += 11;
 208
 209        if (delete)
 210                delete_branches(argc - i, argv + i, force_delete);
 211        else if (i == argc)
 212                print_ref_list(remote_only);
 213        else if (i == argc - 1)
 214                create_branch(argv[i], head, force_create, reflog);
 215        else if (i == argc - 2)
 216                create_branch(argv[i], argv[i + 1], force_create, reflog);
 217        else
 218                usage(builtin_branch_usage);
 219
 220        return 0;
 221}