branch.con commit ident: use a dynamic strbuf in fmt_ident (c96f0c8)
   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, int quiet)
 105{
 106        struct tracking tracking;
 107        int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
 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(config_flags, 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_unsafe("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, int clobber_head,
 195                   int quiet, enum branch_track track)
 196{
 197        struct ref_lock *lock = NULL;
 198        struct commit *commit;
 199        unsigned char sha1[20];
 200        char *real_ref, msg[PATH_MAX + 20];
 201        struct strbuf ref = STRBUF_INIT;
 202        int forcing = 0;
 203        int dont_change_ref = 0;
 204        int explicit_tracking = 0;
 205
 206        if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
 207                explicit_tracking = 1;
 208
 209        if (validate_new_branchname(name, &ref, force,
 210                                    track == BRANCH_TRACK_OVERRIDE ||
 211                                    clobber_head)) {
 212                if (!force)
 213                        dont_change_ref = 1;
 214                else
 215                        forcing = 1;
 216        }
 217
 218        real_ref = NULL;
 219        if (get_sha1(start_name, sha1))
 220                die("Not a valid object name: '%s'.", start_name);
 221
 222        switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
 223        case 0:
 224                /* Not branching from any existing branch */
 225                if (explicit_tracking)
 226                        die("Cannot setup tracking information; starting point is not a branch.");
 227                break;
 228        case 1:
 229                /* Unique completion -- good, only if it is a real branch */
 230                if (prefixcmp(real_ref, "refs/heads/") &&
 231                    prefixcmp(real_ref, "refs/remotes/")) {
 232                        if (explicit_tracking)
 233                                die("Cannot setup tracking information; starting point is not a branch.");
 234                        else
 235                                real_ref = NULL;
 236                }
 237                break;
 238        default:
 239                die("Ambiguous object name: '%s'.", start_name);
 240                break;
 241        }
 242
 243        if ((commit = lookup_commit_reference(sha1)) == NULL)
 244                die("Not a valid branch point: '%s'.", start_name);
 245        hashcpy(sha1, commit->object.sha1);
 246
 247        if (!dont_change_ref) {
 248                lock = lock_any_ref_for_update(ref.buf, NULL, 0);
 249                if (!lock)
 250                        die_errno("Failed to lock ref for update");
 251        }
 252
 253        if (reflog)
 254                log_all_ref_updates = 1;
 255
 256        if (forcing)
 257                snprintf(msg, sizeof msg, "branch: Reset to %s",
 258                         start_name);
 259        else if (!dont_change_ref)
 260                snprintf(msg, sizeof msg, "branch: Created from %s",
 261                         start_name);
 262
 263        if (real_ref && track)
 264                setup_tracking(ref.buf+11, real_ref, track, quiet);
 265
 266        if (!dont_change_ref)
 267                if (write_ref_sha1(lock, sha1, msg) < 0)
 268                        die_errno("Failed to write ref");
 269
 270        strbuf_release(&ref);
 271        free(real_ref);
 272}
 273
 274void remove_branch_state(void)
 275{
 276        unlink(git_path("CHERRY_PICK_HEAD"));
 277        unlink(git_path("REVERT_HEAD"));
 278        unlink(git_path("MERGE_HEAD"));
 279        unlink(git_path("MERGE_RR"));
 280        unlink(git_path("MERGE_MSG"));
 281        unlink(git_path("MERGE_MODE"));
 282        unlink(git_path("SQUASH_MSG"));
 283}