branch.con commit Merge branch 'cn/maint-lf-to-crlf-filter' (a96a89f)
   1#include "cache.h"
   2#include "branch.h"
   3#include "refs.h"
   4#include "remote.h"
   5#include "commit.h"
   6#include "sequencer.h"
   7
   8struct tracking {
   9        struct refspec spec;
  10        char *src;
  11        const char *remote;
  12        int matches;
  13};
  14
  15static int find_tracked_branch(struct remote *remote, void *priv)
  16{
  17        struct tracking *tracking = priv;
  18
  19        if (!remote_find_tracking(remote, &tracking->spec)) {
  20                if (++tracking->matches == 1) {
  21                        tracking->src = tracking->spec.src;
  22                        tracking->remote = remote->name;
  23                } else {
  24                        free(tracking->spec.src);
  25                        if (tracking->src) {
  26                                free(tracking->src);
  27                                tracking->src = NULL;
  28                        }
  29                }
  30                tracking->spec.src = NULL;
  31        }
  32
  33        return 0;
  34}
  35
  36static int should_setup_rebase(const char *origin)
  37{
  38        switch (autorebase) {
  39        case AUTOREBASE_NEVER:
  40                return 0;
  41        case AUTOREBASE_LOCAL:
  42                return origin == NULL;
  43        case AUTOREBASE_REMOTE:
  44                return origin != NULL;
  45        case AUTOREBASE_ALWAYS:
  46                return 1;
  47        }
  48        return 0;
  49}
  50
  51void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
  52{
  53        const char *shortname = remote + 11;
  54        int remote_is_branch = !prefixcmp(remote, "refs/heads/");
  55        struct strbuf key = STRBUF_INIT;
  56        int rebasing = should_setup_rebase(origin);
  57
  58        if (remote_is_branch
  59            && !strcmp(local, shortname)
  60            && !origin) {
  61                warning("Not setting branch %s as its own upstream.",
  62                        local);
  63                return;
  64        }
  65
  66        strbuf_addf(&key, "branch.%s.remote", local);
  67        git_config_set(key.buf, origin ? origin : ".");
  68
  69        strbuf_reset(&key);
  70        strbuf_addf(&key, "branch.%s.merge", local);
  71        git_config_set(key.buf, remote);
  72
  73        if (rebasing) {
  74                strbuf_reset(&key);
  75                strbuf_addf(&key, "branch.%s.rebase", local);
  76                git_config_set(key.buf, "true");
  77        }
  78
  79        if (flag & BRANCH_CONFIG_VERBOSE) {
  80                strbuf_reset(&key);
  81
  82                strbuf_addstr(&key, origin ? "remote" : "local");
  83
  84                /* Are we tracking a proper "branch"? */
  85                if (remote_is_branch) {
  86                        strbuf_addf(&key, " branch %s", shortname);
  87                        if (origin)
  88                                strbuf_addf(&key, " from %s", origin);
  89                }
  90                else
  91                        strbuf_addf(&key, " ref %s", remote);
  92                printf("Branch %s set up to track %s%s.\n",
  93                       local, key.buf,
  94                       rebasing ? " by rebasing" : "");
  95        }
  96        strbuf_release(&key);
  97}
  98
  99/*
 100 * This is called when new_ref is branched off of orig_ref, and tries
 101 * to infer the settings for branch.<new_ref>.{remote,merge} from the
 102 * config.
 103 */
 104static int setup_tracking(const char *new_ref, const char *orig_ref,
 105                          enum branch_track track)
 106{
 107        struct tracking tracking;
 108
 109        if (strlen(new_ref) > 1024 - 7 - 7 - 1)
 110                return error("Tracking not set up: name too long: %s",
 111                                new_ref);
 112
 113        memset(&tracking, 0, sizeof(tracking));
 114        tracking.spec.dst = (char *)orig_ref;
 115        if (for_each_remote(find_tracked_branch, &tracking))
 116                return 1;
 117
 118        if (!tracking.matches)
 119                switch (track) {
 120                case BRANCH_TRACK_ALWAYS:
 121                case BRANCH_TRACK_EXPLICIT:
 122                case BRANCH_TRACK_OVERRIDE:
 123                        break;
 124                default:
 125                        return 1;
 126                }
 127
 128        if (tracking.matches > 1)
 129                return error("Not tracking: ambiguous information for ref %s",
 130                                orig_ref);
 131
 132        install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
 133                              tracking.src ? tracking.src : orig_ref);
 134
 135        free(tracking.src);
 136        return 0;
 137}
 138
 139struct branch_desc_cb {
 140        const char *config_name;
 141        const char *value;
 142};
 143
 144static int read_branch_desc_cb(const char *var, const char *value, void *cb)
 145{
 146        struct branch_desc_cb *desc = cb;
 147        if (strcmp(desc->config_name, var))
 148                return 0;
 149        free((char *)desc->value);
 150        return git_config_string(&desc->value, var, value);
 151}
 152
 153int read_branch_desc(struct strbuf *buf, const char *branch_name)
 154{
 155        struct branch_desc_cb cb;
 156        struct strbuf name = STRBUF_INIT;
 157        strbuf_addf(&name, "branch.%s.description", branch_name);
 158        cb.config_name = name.buf;
 159        cb.value = NULL;
 160        if (git_config(read_branch_desc_cb, &cb) < 0) {
 161                strbuf_release(&name);
 162                return -1;
 163        }
 164        if (cb.value)
 165                strbuf_addstr(buf, cb.value);
 166        strbuf_release(&name);
 167        return 0;
 168}
 169
 170int validate_new_branchname(const char *name, struct strbuf *ref,
 171                            int force, int attr_only)
 172{
 173        if (strbuf_check_branch_ref(ref, name))
 174                die("'%s' is not a valid branch name.", name);
 175
 176        if (!ref_exists(ref->buf))
 177                return 0;
 178        else if (!force && !attr_only)
 179                die("A branch named '%s' already exists.", ref->buf + strlen("refs/heads/"));
 180
 181        if (!attr_only) {
 182                const char *head;
 183                unsigned char sha1[20];
 184
 185                head = resolve_ref("HEAD", sha1, 0, NULL);
 186                if (!is_bare_repository() && head && !strcmp(head, ref->buf))
 187                        die("Cannot force update the current branch.");
 188        }
 189        return 1;
 190}
 191
 192void create_branch(const char *head,
 193                   const char *name, const char *start_name,
 194                   int force, int reflog, enum branch_track track)
 195{
 196        struct ref_lock *lock = NULL;
 197        struct commit *commit;
 198        unsigned char sha1[20];
 199        char *real_ref, msg[PATH_MAX + 20];
 200        struct strbuf ref = STRBUF_INIT;
 201        int forcing = 0;
 202        int dont_change_ref = 0;
 203        int explicit_tracking = 0;
 204
 205        if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
 206                explicit_tracking = 1;
 207
 208        if (validate_new_branchname(name, &ref, force,
 209                                    track == BRANCH_TRACK_OVERRIDE)) {
 210                if (!force)
 211                        dont_change_ref = 1;
 212                else
 213                        forcing = 1;
 214        }
 215
 216        real_ref = NULL;
 217        if (get_sha1(start_name, sha1))
 218                die("Not a valid object name: '%s'.", start_name);
 219
 220        switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
 221        case 0:
 222                /* Not branching from any existing branch */
 223                if (explicit_tracking)
 224                        die("Cannot setup tracking information; starting point is not a branch.");
 225                break;
 226        case 1:
 227                /* Unique completion -- good, only if it is a real branch */
 228                if (prefixcmp(real_ref, "refs/heads/") &&
 229                    prefixcmp(real_ref, "refs/remotes/")) {
 230                        if (explicit_tracking)
 231                                die("Cannot setup tracking information; starting point is not a branch.");
 232                        else
 233                                real_ref = NULL;
 234                }
 235                break;
 236        default:
 237                die("Ambiguous object name: '%s'.", start_name);
 238                break;
 239        }
 240
 241        if ((commit = lookup_commit_reference(sha1)) == NULL)
 242                die("Not a valid branch point: '%s'.", start_name);
 243        hashcpy(sha1, commit->object.sha1);
 244
 245        if (!dont_change_ref) {
 246                lock = lock_any_ref_for_update(ref.buf, NULL, 0);
 247                if (!lock)
 248                        die_errno("Failed to lock ref for update");
 249        }
 250
 251        if (reflog)
 252                log_all_ref_updates = 1;
 253
 254        if (forcing)
 255                snprintf(msg, sizeof msg, "branch: Reset to %s",
 256                         start_name);
 257        else if (!dont_change_ref)
 258                snprintf(msg, sizeof msg, "branch: Created from %s",
 259                         start_name);
 260
 261        if (real_ref && track)
 262                setup_tracking(ref.buf+11, real_ref, track);
 263
 264        if (!dont_change_ref)
 265                if (write_ref_sha1(lock, sha1, msg) < 0)
 266                        die_errno("Failed to write ref");
 267
 268        strbuf_release(&ref);
 269        free(real_ref);
 270}
 271
 272void remove_branch_state(void)
 273{
 274        unlink(git_path("CHERRY_PICK_HEAD"));
 275        unlink(git_path("REVERT_HEAD"));
 276        unlink(git_path("MERGE_HEAD"));
 277        unlink(git_path("MERGE_RR"));
 278        unlink(git_path("MERGE_MSG"));
 279        unlink(git_path("MERGE_MODE"));
 280        unlink(git_path("SQUASH_MSG"));
 281        remove_sequencer_state(0);
 282}