branch.con commit Make git-clone respect branch.autosetuprebase (a9f2c13)
   1#include "cache.h"
   2#include "branch.h"
   3#include "refs.h"
   4#include "remote.h"
   5#include "commit.h"
   6
   7struct tracking {
   8        struct refspec spec;
   9        char *src;
  10        const char *remote;
  11        int matches;
  12};
  13
  14static int find_tracked_branch(struct remote *remote, void *priv)
  15{
  16        struct tracking *tracking = priv;
  17
  18        if (!remote_find_tracking(remote, &tracking->spec)) {
  19                if (++tracking->matches == 1) {
  20                        tracking->src = tracking->spec.src;
  21                        tracking->remote = remote->name;
  22                } else {
  23                        free(tracking->spec.src);
  24                        if (tracking->src) {
  25                                free(tracking->src);
  26                                tracking->src = NULL;
  27                        }
  28                }
  29                tracking->spec.src = NULL;
  30        }
  31
  32        return 0;
  33}
  34
  35static int should_setup_rebase(const char *origin)
  36{
  37        switch (autorebase) {
  38        case AUTOREBASE_NEVER:
  39                return 0;
  40        case AUTOREBASE_LOCAL:
  41                return origin == NULL;
  42        case AUTOREBASE_REMOTE:
  43                return origin != NULL;
  44        case AUTOREBASE_ALWAYS:
  45                return 1;
  46        }
  47        return 0;
  48}
  49
  50void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
  51{
  52        struct strbuf key = STRBUF_INIT;
  53        int rebasing = should_setup_rebase(origin);
  54
  55        strbuf_addf(&key, "branch.%s.remote", local);
  56        git_config_set(key.buf, origin ? origin : ".");
  57
  58        strbuf_reset(&key);
  59        strbuf_addf(&key, "branch.%s.merge", local);
  60        git_config_set(key.buf, remote);
  61
  62        if (rebasing) {
  63                strbuf_reset(&key);
  64                strbuf_addf(&key, "branch.%s.rebase", local);
  65                git_config_set(key.buf, "true");
  66        }
  67
  68        if (flag & BRANCH_CONFIG_VERBOSE)
  69                printf("Branch %s set up to track %s branch %s %s.\n",
  70                       local,
  71                       origin ? "remote" : "local",
  72                       remote,
  73                       rebasing ? "by rebasing" : "by merging");
  74        strbuf_release(&key);
  75}
  76
  77/*
  78 * This is called when new_ref is branched off of orig_ref, and tries
  79 * to infer the settings for branch.<new_ref>.{remote,merge} from the
  80 * config.
  81 */
  82static int setup_tracking(const char *new_ref, const char *orig_ref,
  83                          enum branch_track track)
  84{
  85        struct tracking tracking;
  86
  87        if (strlen(new_ref) > 1024 - 7 - 7 - 1)
  88                return error("Tracking not set up: name too long: %s",
  89                                new_ref);
  90
  91        memset(&tracking, 0, sizeof(tracking));
  92        tracking.spec.dst = (char *)orig_ref;
  93        if (for_each_remote(find_tracked_branch, &tracking))
  94                return 1;
  95
  96        if (!tracking.matches)
  97                switch (track) {
  98                case BRANCH_TRACK_ALWAYS:
  99                case BRANCH_TRACK_EXPLICIT:
 100                        break;
 101                default:
 102                        return 1;
 103                }
 104
 105        if (tracking.matches > 1)
 106                return error("Not tracking: ambiguous information for ref %s",
 107                                orig_ref);
 108
 109        install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
 110                              tracking.src ? tracking.src : orig_ref);
 111
 112        free(tracking.src);
 113        return 0;
 114}
 115
 116void create_branch(const char *head,
 117                   const char *name, const char *start_name,
 118                   int force, int reflog, enum branch_track track)
 119{
 120        struct ref_lock *lock;
 121        struct commit *commit;
 122        unsigned char sha1[20];
 123        char *real_ref, msg[PATH_MAX + 20];
 124        struct strbuf ref = STRBUF_INIT;
 125        int forcing = 0;
 126        int len;
 127
 128        len = strlen(name);
 129        if (interpret_nth_last_branch(name, &ref) != len) {
 130                strbuf_reset(&ref);
 131                strbuf_add(&ref, name, len);
 132        }
 133        strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
 134
 135        if (check_ref_format(ref.buf))
 136                die("'%s' is not a valid branch name.", name);
 137
 138        if (resolve_ref(ref.buf, sha1, 1, NULL)) {
 139                if (!force)
 140                        die("A branch named '%s' already exists.", name);
 141                else if (!is_bare_repository() && !strcmp(head, name))
 142                        die("Cannot force update the current branch.");
 143                forcing = 1;
 144        }
 145
 146        real_ref = NULL;
 147        if (get_sha1(start_name, sha1))
 148                die("Not a valid object name: '%s'.", start_name);
 149
 150        switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
 151        case 0:
 152                /* Not branching from any existing branch */
 153                if (track == BRANCH_TRACK_EXPLICIT)
 154                        die("Cannot setup tracking information; starting point is not a branch.");
 155                break;
 156        case 1:
 157                /* Unique completion -- good, only if it is a real ref */
 158                if (track == BRANCH_TRACK_EXPLICIT && !strcmp(real_ref, "HEAD"))
 159                        die("Cannot setup tracking information; starting point is not a branch.");
 160                break;
 161        default:
 162                die("Ambiguous object name: '%s'.", start_name);
 163                break;
 164        }
 165
 166        if ((commit = lookup_commit_reference(sha1)) == NULL)
 167                die("Not a valid branch point: '%s'.", start_name);
 168        hashcpy(sha1, commit->object.sha1);
 169
 170        lock = lock_any_ref_for_update(ref.buf, NULL, 0);
 171        if (!lock)
 172                die("Failed to lock ref for update: %s.", strerror(errno));
 173
 174        if (reflog)
 175                log_all_ref_updates = 1;
 176
 177        if (forcing)
 178                snprintf(msg, sizeof msg, "branch: Reset from %s",
 179                         start_name);
 180        else
 181                snprintf(msg, sizeof msg, "branch: Created from %s",
 182                         start_name);
 183
 184        if (real_ref && track)
 185                setup_tracking(name, real_ref, track);
 186
 187        if (write_ref_sha1(lock, sha1, msg) < 0)
 188                die("Failed to write ref: %s.", strerror(errno));
 189
 190        strbuf_release(&ref);
 191        free(real_ref);
 192}
 193
 194void remove_branch_state(void)
 195{
 196        unlink(git_path("MERGE_HEAD"));
 197        unlink(git_path("MERGE_RR"));
 198        unlink(git_path("MERGE_MSG"));
 199        unlink(git_path("MERGE_MODE"));
 200        unlink(git_path("SQUASH_MSG"));
 201}