12
#include "builtin.h"
13
14
static const char builtin_branch_usage[] =
15
- "git-branch [-r] (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
15
+ "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]]";
16
17
#define REF_UNKNOWN_TYPE 0x00
18
#define REF_LOCAL_BRANCH 0x01
22
static const char *head;
23
static unsigned char head_sha1[20];
24
25
+static int branch_track_remotes;
26
+
27
static int branch_use_color;
28
static char branch_colors[][COLOR_MAXLEN] = {
29
"\033[m", /* reset */
66
color_parse(value, var, branch_colors[slot]);
67
return 0;
68
}
69
+ if (!strcmp(var, "branch.autosetupmerge"))
70
+ branch_track_remotes = git_config_bool(var, value);
71
+
72
return git_default_config(var, value);
73
}
74
314
free_ref_list(&ref_list);
315
}
316
317
+static char *config_repo;
318
+static char *config_remote;
319
+static const char *start_ref;
320
+static int start_len;
321
+static int base_len;
322
+
323
+static int get_remote_branch_name(const char *value)
324
+{
325
+ const char *colon;
326
+ const char *end;
327
+
328
+ if (*value == '+')
329
+ value++;
330
+
331
+ colon = strchr(value, ':');
332
+ if (!colon)
333
+ return 0;
334
+
335
+ end = value + strlen(value);
336
+
337
+ /* Try an exact match first. */
338
+ if (!strcmp(colon + 1, start_ref)) {
339
+ /* Truncate the value before the colon. */
340
+ nfasprintf(&config_repo, "%.*s", colon - value, value);
341
+ return 1;
342
+ }
343
+
344
+ /* Try with a wildcard match now. */
345
+ if (end - value > 2 && end[-2] == '/' && end[-1] == '*' &&
346
+ colon - value > 2 && colon[-2] == '/' && colon[-1] == '*' &&
347
+ (end - 2) - (colon + 1) == base_len &&
348
+ !strncmp(colon + 1, start_ref, base_len)) {
349
+ /* Replace the star with the remote branch name. */
350
+ nfasprintf(&config_repo, "%.*s%s",
351
+ (colon - 2) - value, value,
352
+ start_ref + base_len);
353
+ return 1;
354
+ }
355
+
356
+ return 0;
357
+}
358
+
359
+static int get_remote_config(const char *key, const char *value)
360
+{
361
+ const char *var;
362
+ if (prefixcmp(key, "remote."))
363
+ return 0;
364
+
365
+ var = strrchr(key, '.');
366
+ if (var == key + 6)
367
+ return 0;
368
+
369
+ if (!strcmp(var, ".fetch") && get_remote_branch_name(value))
370
+ nfasprintf(&config_remote, "%.*s", var - (key + 7), key + 7);
371
+
372
+ return 0;
373
+}
374
+
375
+static void set_branch_defaults(const char *name, const char *real_ref)
376
+{
377
+ char key[1024];
378
+ const char *slash = strrchr(real_ref, '/');
379
+
380
+ if (!slash)
381
+ return;
382
+
383
+ start_ref = real_ref;
384
+ start_len = strlen(real_ref);
385
+ base_len = slash - real_ref;
386
+ git_config(get_remote_config);
387
+
388
+ if (config_repo && config_remote) {
389
+ if (sizeof(key) <=
390
+ snprintf(key, sizeof(key), "branch.%s.remote", name))
391
+ die("what a long branch name you have!");
392
+ git_config_set(key, config_remote);
393
+
394
+ /*
395
+ * We do not have to check if we have enough space for
396
+ * the 'merge' key, since it's shorter than the
397
+ * previous 'remote' key, which we already checked.
398
+ */
399
+ snprintf(key, sizeof(key), "branch.%s.merge", name);
400
+ git_config_set(key, config_repo);
401
+
402
+ printf("Branch %s set up to track remote branch %s.\n",
403
+ name, real_ref);
404
+ }
405
+
406
+ if (config_repo)
407
+ free(config_repo);
408
+ if (config_remote)
409
+ free(config_remote);
410
+}
411
+
412
static void create_branch(const char *name, const char *start_name,
413
unsigned char *start_sha1,
314
- int force, int reflog)
414
+ int force, int reflog, int track)
415
{
416
struct ref_lock *lock;
417
struct commit *commit;
418
unsigned char sha1[20];
319
- char ref[PATH_MAX], msg[PATH_MAX + 20];
419
+ char *real_ref, ref[PATH_MAX], msg[PATH_MAX + 20];
420
int forcing = 0;
421
422
snprintf(ref, sizeof ref, "refs/heads/%s", name);
431
forcing = 1;
432
}
433
334
- if (start_sha1)
434
+ if (start_sha1) {
435
/* detached HEAD */
436
hashcpy(sha1, start_sha1);
337
- else if (get_sha1(start_name, sha1))
437
+ real_ref = NULL;
438
+ }
439
+ else if (dwim_ref(start_name, strlen(start_name), sha1, &real_ref) > 1)
440
+ die("Ambiguous object name: '%s'.", start_name);
441
+ else if (real_ref == NULL)
442
die("Not a valid object name: '%s'.", start_name);
443
444
if ((commit = lookup_commit_reference(sha1)) == NULL)
459
snprintf(msg, sizeof msg, "branch: Created from %s",
460
start_name);
461
462
+ /* When branching off a remote branch, set up so that git-pull
463
+ automatically merges from there. So far, this is only done for
464
+ remotes registered via .git/config. */
465
+ if (real_ref && track)
466
+ set_branch_defaults(name, real_ref);
467
+
468
if (write_ref_sha1(lock, sha1, msg) < 0)
469
die("Failed to write ref: %s.", strerror(errno));
470
+
471
+ if (real_ref)
472
+ free(real_ref);
473
}
474
475
static void rename_branch(const char *oldname, const char *newname, int force)
511
int delete = 0, force_delete = 0, force_create = 0;
512
int rename = 0, force_rename = 0;
513
int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
401
- int reflog = 0;
514
+ int reflog = 0, track;
515
int kinds = REF_LOCAL_BRANCH;
516
int i;
517
518
git_config(git_branch_config);
519
+ track = branch_track_remotes;
520
521
for (i = 1; i < argc; i++) {
522
const char *arg = argv[i];
527
i++;
528
break;
529
}
530
+ if (!strcmp(arg, "--track")) {
531
+ track = 1;
532
+ continue;
533
+ }
534
+ if (!strcmp(arg, "--no-track")) {
535
+ track = 0;
536
+ continue;
537
+ }
538
if (!strcmp(arg, "-d")) {
539
delete = 1;
540
continue;
621
else if (rename && (i == argc - 2))
622
rename_branch(argv[i], argv[i + 1], force_rename);
623
else if (i == argc - 1)
502
- create_branch(argv[i], head, head_sha1, force_create, reflog);
624
+ create_branch(argv[i], head, head_sha1, force_create, reflog,
625
+ track);
626
else if (i == argc - 2)
504
- create_branch(argv[i], argv[i+1], NULL, force_create, reflog);
627
+ create_branch(argv[i], argv[i+1], NULL, force_create, reflog,
628
+ track);
629
else
630
usage(builtin_branch_usage);
631