10
#include "refs.h"
11
#include "commit.h"
12
#include "builtin.h"
13
+#include "remote.h"
14
15
static const char builtin_branch_usage[] =
16
"git-branch [-r] (-d | -D) <branchname> | [--track | --no-track] [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
23
static const char *head;
24
static unsigned char head_sha1[20];
25
25
-static int branch_track = 1; /* 0 = none, 1 = remotes, 2 = all */
26
+static int branch_track = 1;
27
28
static int branch_use_color;
29
static char branch_colors[][COLOR_MAXLEN] = {
67
color_parse(value, var, branch_colors[slot]);
68
return 0;
69
}
69
- if (!strcmp(var, "branch.autosetupmerge")) {
70
- if (!strcmp(value, "all"))
71
- branch_track = 2;
72
- else
70
+ if (!strcmp(var, "branch.autosetupmerge"))
71
branch_track = git_config_bool(var, value);
74
- }
72
73
return git_default_config(var, value);
74
}
325
free_ref_list(&ref_list);
326
}
327
331
-static char *config_repo;
332
-static char *config_remote;
333
-static const char *start_ref;
328
+struct tracking {
329
+ struct refspec spec;
330
+ char *src;
331
+ const char *remote;
332
+ int matches;
333
+};
334
335
-static int get_remote_branch_name(const char *value)
335
+static int find_tracked_branch(struct remote *remote, void *priv)
336
{
337
- const char *colon;
338
- const char *end;
337
+ struct tracking *tracking = priv;
338
340
- if (*value == '+')
341
- value++;
342
-
343
- colon = strchr(value, ':');
344
- if (!colon)
345
- return 0;
346
-
347
- end = value + strlen(value);
348
-
349
- /*
350
- * Try an exact match first. I.e. handle the case where the
351
- * value is "$anything:refs/foo/bar/baz" and start_ref is exactly
352
- * "refs/foo/bar/baz". Then the name at the remote is $anything.
353
- */
354
- if (!strcmp(colon + 1, start_ref)) {
355
- /* Truncate the value before the colon. */
356
- nfasprintf(&config_repo, "%.*s", colon - value, value);
357
- return 1;
339
+ if (!remote_find_tracking(remote, &tracking->spec)) {
340
+ if (++tracking->matches == 1) {
341
+ tracking->src = tracking->spec.src;
342
+ tracking->remote = remote->name;
343
+ } else {
344
+ free(tracking->spec.src);
345
+ if (tracking->src) {
346
+ free(tracking->src);
347
+ tracking->src = NULL;
348
+ }
349
+ }
350
+ tracking->spec.src = NULL;
351
}
352
360
- /*
361
- * Is this a wildcard match?
362
- */
363
- if ((end - 2 <= value) || end[-2] != '/' || end[-1] != '*' ||
364
- (colon - 2 <= value) || colon[-2] != '/' || colon[-1] != '*')
365
- return 0;
366
-
367
- /*
368
- * Value is "refs/foo/bar/<asterisk>:refs/baz/boa/<asterisk>"
369
- * and start_ref begins with "refs/baz/boa/"; the name at the
370
- * remote is refs/foo/bar/ with the remaining part of the
371
- * start_ref. The length of the prefix on the RHS is (end -
372
- * colon - 2), including the slash immediately before the
373
- * asterisk.
374
- */
375
- if ((strlen(start_ref) < end - colon - 2) ||
376
- memcmp(start_ref, colon + 1, end - colon - 2))
377
- return 0; /* does not match prefix */
378
-
379
- /* Replace the asterisk with the remote branch name. */
380
- nfasprintf(&config_repo, "%.*s%s",
381
- (colon - 1) - value, value,
382
- start_ref + (end - colon - 2));
383
- return 1;
384
-}
385
-
386
-static int get_remote_config(const char *key, const char *value)
387
-{
388
- const char *var;
389
- if (prefixcmp(key, "remote."))
390
- return 0;
391
-
392
- var = strrchr(key, '.');
393
- if (var == key + 6 || strcmp(var, ".fetch"))
394
- return 0;
395
- /*
396
- * Ok, we are looking at key == "remote.$foo.fetch";
397
- */
398
- if (get_remote_branch_name(value))
399
- nfasprintf(&config_remote, "%.*s", var - (key + 7), key + 7);
400
-
353
return 0;
354
}
355
404
-static void set_branch_merge(const char *name, const char *config_remote,
405
- const char *config_repo)
356
+
357
+/*
358
+ * This is called when new_ref is branched off of orig_ref, and tries
359
+ * to infer the settings for branch.<new_ref>.{remote,merge} from the
360
+ * config.
361
+ */
362
+static int setup_tracking(const char *new_ref, const char *orig_ref)
363
{
364
char key[1024];
408
- if (sizeof(key) <=
409
- snprintf(key, sizeof(key), "branch.%s.remote", name))
410
- die("what a long branch name you have!");
411
- git_config_set(key, config_remote);
412
-
413
- /*
414
- * We do not have to check if we have enough space for
415
- * the 'merge' key, since it's shorter than the
416
- * previous 'remote' key, which we already checked.
417
- */
418
- snprintf(key, sizeof(key), "branch.%s.merge", name);
419
- git_config_set(key, config_repo);
420
-}
365
+ struct tracking tracking;
366
422
-static void set_branch_defaults(const char *name, const char *real_ref)
423
-{
424
- /*
425
- * name is the name of new branch under refs/heads;
426
- * real_ref is typically refs/remotes/$foo/$bar, where
427
- * $foo is the remote name (there typically are no slashes)
428
- * and $bar is the branch name we map from the remote
429
- * (it could have slashes).
430
- */
431
- start_ref = real_ref;
432
- git_config(get_remote_config);
433
- if (!config_repo && !config_remote &&
434
- !prefixcmp(real_ref, "refs/heads/")) {
435
- set_branch_merge(name, ".", real_ref);
436
- printf("Branch %s set up to track local branch %s.\n",
437
- name, real_ref);
438
- }
367
+ if (strlen(new_ref) > 1024 - 7 - 7 - 1)
368
+ return error("Tracking not set up: name too long: %s",
369
+ new_ref);
370
440
- if (config_repo && config_remote) {
441
- set_branch_merge(name, config_remote, config_repo);
371
+ memset(&tracking, 0, sizeof(tracking));
372
+ tracking.spec.dst = (char *)orig_ref;
373
+ if (for_each_remote(find_tracked_branch, &tracking) ||
374
+ !tracking.matches)
375
+ return 1;
376
+
377
+ if (tracking.matches > 1)
378
+ return error("Not tracking: ambiguous information for ref %s",
379
+ orig_ref);
380
+
381
+ if (tracking.matches == 1) {
382
+ sprintf(key, "branch.%s.remote", new_ref);
383
+ git_config_set(key, tracking.remote ? tracking.remote : ".");
384
+ sprintf(key, "branch.%s.merge", new_ref);
385
+ git_config_set(key, tracking.src);
386
+ free(tracking.src);
387
printf("Branch %s set up to track remote branch %s.\n",
443
- name, real_ref);
388
+ new_ref, orig_ref);
389
}
390
446
- if (config_repo)
447
- free(config_repo);
448
- if (config_remote)
449
- free(config_remote);
391
+ return 0;
392
}
393
394
static void create_branch(const char *name, const char *start_name,
450
/* When branching off a remote branch, set up so that git-pull
451
automatically merges from there. So far, this is only done for
452
remotes registered via .git/config. */
511
- if (real_ref && (track == 2 ||
512
- (track == 1 &&
513
- !prefixcmp(real_ref, "refs/remotes/"))))
514
- set_branch_defaults(name, real_ref);
453
+ if (real_ref && track)
454
+ setup_tracking(name, real_ref);
455
456
if (write_ref_sha1(lock, sha1, msg) < 0)
457
die("Failed to write ref: %s.", strerror(errno));
522
break;
523
}
524
if (!strcmp(arg, "--track")) {
585
- track = 2;
525
+ track = 1;
526
continue;
527
}
528
if (!strcmp(arg, "--no-track")) {