branch.con commit completion: push --set-upstream (3623dc0)
   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        const char *shortname = remote + 11;
  53        int remote_is_branch = !prefixcmp(remote, "refs/heads/");
  54        struct strbuf key = STRBUF_INIT;
  55        int rebasing = should_setup_rebase(origin);
  56
  57        if (remote_is_branch
  58            && !strcmp(local, shortname)
  59            && !origin) {
  60                warning("Not setting branch %s as its own upstream.",
  61                        local);
  62                return;
  63        }
  64
  65        strbuf_addf(&key, "branch.%s.remote", local);
  66        git_config_set(key.buf, origin ? origin : ".");
  67
  68        strbuf_reset(&key);
  69        strbuf_addf(&key, "branch.%s.merge", local);
  70        git_config_set(key.buf, remote);
  71
  72        if (rebasing) {
  73                strbuf_reset(&key);
  74                strbuf_addf(&key, "branch.%s.rebase", local);
  75                git_config_set(key.buf, "true");
  76        }
  77
  78        if (flag & BRANCH_CONFIG_VERBOSE) {
  79                strbuf_reset(&key);
  80
  81                strbuf_addstr(&key, origin ? "remote" : "local");
  82
  83                /* Are we tracking a proper "branch"? */
  84                if (remote_is_branch) {
  85                        strbuf_addf(&key, " branch %s", shortname);
  86                        if (origin)
  87                                strbuf_addf(&key, " from %s", origin);
  88                }
  89                else
  90                        strbuf_addf(&key, " ref %s", remote);
  91                printf("Branch %s set up to track %s%s.\n",
  92                       local, key.buf,
  93                       rebasing ? " by rebasing" : "");
  94        }
  95        strbuf_release(&key);
  96}
  97
  98/*
  99 * This is called when new_ref is branched off of orig_ref, and tries
 100 * to infer the settings for branch.<new_ref>.{remote,merge} from the
 101 * config.
 102 */
 103static int setup_tracking(const char *new_ref, const char *orig_ref,
 104                          enum branch_track track)
 105{
 106        struct tracking tracking;
 107
 108        if (strlen(new_ref) > 1024 - 7 - 7 - 1)
 109                return error("Tracking not set up: name too long: %s",
 110                                new_ref);
 111
 112        memset(&tracking, 0, sizeof(tracking));
 113        tracking.spec.dst = (char *)orig_ref;
 114        if (for_each_remote(find_tracked_branch, &tracking))
 115                return 1;
 116
 117        if (!tracking.matches)
 118                switch (track) {
 119                case BRANCH_TRACK_ALWAYS:
 120                case BRANCH_TRACK_EXPLICIT:
 121                case BRANCH_TRACK_OVERRIDE:
 122                        break;
 123                default:
 124                        return 1;
 125                }
 126
 127        if (tracking.matches > 1)
 128                return error("Not tracking: ambiguous information for ref %s",
 129                                orig_ref);
 130
 131        install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
 132                              tracking.src ? tracking.src : orig_ref);
 133
 134        free(tracking.src);
 135        return 0;
 136}
 137
 138void create_branch(const char *head,
 139                   const char *name, const char *start_name,
 140                   int force, int reflog, enum branch_track track)
 141{
 142        struct ref_lock *lock = NULL;
 143        struct commit *commit;
 144        unsigned char sha1[20];
 145        char *real_ref, msg[PATH_MAX + 20];
 146        struct strbuf ref = STRBUF_INIT;
 147        int forcing = 0;
 148        int dont_change_ref = 0;
 149        int explicit_tracking = 0;
 150
 151        if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
 152                explicit_tracking = 1;
 153
 154        if (strbuf_check_branch_ref(&ref, name))
 155                die("'%s' is not a valid branch name.", name);
 156
 157        if (resolve_ref(ref.buf, sha1, 1, NULL)) {
 158                if (!force && track == BRANCH_TRACK_OVERRIDE)
 159                        dont_change_ref = 1;
 160                else if (!force)
 161                        die("A branch named '%s' already exists.", name);
 162                else if (!is_bare_repository() && head && !strcmp(head, name))
 163                        die("Cannot force update the current branch.");
 164                forcing = 1;
 165        }
 166
 167        real_ref = NULL;
 168        if (get_sha1(start_name, sha1))
 169                die("Not a valid object name: '%s'.", start_name);
 170
 171        switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
 172        case 0:
 173                /* Not branching from any existing branch */
 174                if (explicit_tracking)
 175                        die("Cannot setup tracking information; starting point is not a branch.");
 176                break;
 177        case 1:
 178                /* Unique completion -- good, only if it is a real branch */
 179                if (prefixcmp(real_ref, "refs/heads/") &&
 180                    prefixcmp(real_ref, "refs/remotes/")) {
 181                        if (explicit_tracking)
 182                                die("Cannot setup tracking information; starting point is not a branch.");
 183                        else
 184                                real_ref = NULL;
 185                }
 186                break;
 187        default:
 188                die("Ambiguous object name: '%s'.", start_name);
 189                break;
 190        }
 191
 192        if ((commit = lookup_commit_reference(sha1)) == NULL)
 193                die("Not a valid branch point: '%s'.", start_name);
 194        hashcpy(sha1, commit->object.sha1);
 195
 196        if (!dont_change_ref) {
 197                lock = lock_any_ref_for_update(ref.buf, NULL, 0);
 198                if (!lock)
 199                        die_errno("Failed to lock ref for update");
 200        }
 201
 202        if (reflog)
 203                log_all_ref_updates = 1;
 204
 205        if (forcing)
 206                snprintf(msg, sizeof msg, "branch: Reset to %s",
 207                         start_name);
 208        else if (!dont_change_ref)
 209                snprintf(msg, sizeof msg, "branch: Created from %s",
 210                         start_name);
 211
 212        if (real_ref && track)
 213                setup_tracking(name, real_ref, track);
 214
 215        if (!dont_change_ref)
 216                if (write_ref_sha1(lock, sha1, msg) < 0)
 217                        die_errno("Failed to write ref");
 218
 219        strbuf_release(&ref);
 220        free(real_ref);
 221}
 222
 223void remove_branch_state(void)
 224{
 225        unlink(git_path("CHERRY_PICK_HEAD"));
 226        unlink(git_path("MERGE_HEAD"));
 227        unlink(git_path("MERGE_RR"));
 228        unlink(git_path("MERGE_MSG"));
 229        unlink(git_path("MERGE_MODE"));
 230        unlink(git_path("SQUASH_MSG"));
 231}