Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/branch.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Builtin "git branch"
3
 *
4
 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5
 * Based on git-branch.sh by Junio C Hamano.
6
 */
7
8
#include "builtin.h"
9
#include "config.h"
10
#include "color.h"
11
#include "editor.h"
12
#include "environment.h"
13
#include "refs.h"
14
#include "commit.h"
15
#include "gettext.h"
16
#include "object-name.h"
17
#include "remote.h"
18
#include "parse-options.h"
19
#include "branch.h"
20
#include "path.h"
21
#include "string-list.h"
22
#include "column.h"
23
#include "utf8.h"
24
#include "ref-filter.h"
25
#include "worktree.h"
26
#include "help.h"
27
#include "advice.h"
28
#include "commit-reach.h"
29
30
static const char * const builtin_branch_usage[] = {
31
  N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
32
  N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"),
33
  N_("git branch [<options>] [-l] [<pattern>...]"),
34
  N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
35
  N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
36
  N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
37
  N_("git branch [<options>] [-r | -a] [--points-at]"),
38
  N_("git branch [<options>] [-r | -a] [--format]"),
39
  NULL
40
};
41
42
static const char *head;
43
static struct object_id head_oid;
44
static int recurse_submodules = 0;
45
static int submodule_propagate_branches = 0;
46
47
static int branch_use_color = -1;
48
static char branch_colors[][COLOR_MAXLEN] = {
49
  GIT_COLOR_RESET,
50
  GIT_COLOR_NORMAL,       /* PLAIN */
51
  GIT_COLOR_RED,          /* REMOTE */
52
  GIT_COLOR_NORMAL,       /* LOCAL */
53
  GIT_COLOR_GREEN,        /* CURRENT */
54
  GIT_COLOR_BLUE,         /* UPSTREAM */
55
  GIT_COLOR_CYAN,         /* WORKTREE */
56
};
57
enum color_branch {
58
  BRANCH_COLOR_RESET = 0,
59
  BRANCH_COLOR_PLAIN = 1,
60
  BRANCH_COLOR_REMOTE = 2,
61
  BRANCH_COLOR_LOCAL = 3,
62
  BRANCH_COLOR_CURRENT = 4,
63
  BRANCH_COLOR_UPSTREAM = 5,
64
  BRANCH_COLOR_WORKTREE = 6
65
};
66
67
static const char *color_branch_slots[] = {
68
  [BRANCH_COLOR_RESET]  = "reset",
69
  [BRANCH_COLOR_PLAIN]  = "plain",
70
  [BRANCH_COLOR_REMOTE] = "remote",
71
  [BRANCH_COLOR_LOCAL]  = "local",
72
  [BRANCH_COLOR_CURRENT]  = "current",
73
  [BRANCH_COLOR_UPSTREAM] = "upstream",
74
  [BRANCH_COLOR_WORKTREE] = "worktree",
75
};
76
77
static struct string_list output = STRING_LIST_INIT_DUP;
78
static unsigned int colopts;
79
80
define_list_config_array(color_branch_slots);
81
82
static int git_branch_config(const char *var, const char *value,
83
           const struct config_context *ctx, void *cb)
84
0
{
85
0
  const char *slot_name;
86
87
0
  if (!strcmp(var, "branch.sort")) {
88
0
    if (!value)
89
0
      return config_error_nonbool(var);
90
0
    string_list_append(cb, value);
91
0
    return 0;
92
0
  }
93
94
0
  if (starts_with(var, "column."))
95
0
    return git_column_config(var, value, "branch", &colopts);
96
0
  if (!strcmp(var, "color.branch")) {
97
0
    branch_use_color = git_config_colorbool(var, value);
98
0
    return 0;
99
0
  }
100
0
  if (skip_prefix(var, "color.branch.", &slot_name)) {
101
0
    int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
102
0
    if (slot < 0)
103
0
      return 0;
104
0
    if (!value)
105
0
      return config_error_nonbool(var);
106
0
    return color_parse(value, branch_colors[slot]);
107
0
  }
108
0
  if (!strcmp(var, "submodule.recurse")) {
109
0
    recurse_submodules = git_config_bool(var, value);
110
0
    return 0;
111
0
  }
112
0
  if (!strcasecmp(var, "submodule.propagateBranches")) {
113
0
    submodule_propagate_branches = git_config_bool(var, value);
114
0
    return 0;
115
0
  }
116
117
0
  if (git_color_config(var, value, cb) < 0)
118
0
    return -1;
119
120
0
  return git_default_config(var, value, ctx, cb);
121
0
}
122
123
static const char *branch_get_color(enum color_branch ix)
124
0
{
125
0
  if (want_color(branch_use_color))
126
0
    return branch_colors[ix];
127
0
  return "";
128
0
}
129
130
static int branch_merged(int kind, const char *name,
131
       struct commit *rev, struct commit *head_rev)
132
0
{
133
  /*
134
   * This checks whether the merge bases of branch and HEAD (or
135
   * the other branch this branch builds upon) contains the
136
   * branch, which means that the branch has already been merged
137
   * safely to HEAD (or the other branch).
138
   */
139
0
  struct commit *reference_rev = NULL;
140
0
  const char *reference_name = NULL;
141
0
  void *reference_name_to_free = NULL;
142
0
  int merged;
143
144
0
  if (kind == FILTER_REFS_BRANCHES) {
145
0
    struct branch *branch = branch_get(name);
146
0
    const char *upstream = branch_get_upstream(branch, NULL);
147
0
    struct object_id oid;
148
149
0
    if (upstream &&
150
0
        (reference_name = reference_name_to_free =
151
0
         refs_resolve_refdup(get_main_ref_store(the_repository), upstream, RESOLVE_REF_READING,
152
0
           &oid, NULL)) != NULL)
153
0
      reference_rev = lookup_commit_reference(the_repository,
154
0
                &oid);
155
0
  }
156
0
  if (!reference_rev)
157
0
    reference_rev = head_rev;
158
159
0
  merged = reference_rev ? repo_in_merge_bases(the_repository, rev,
160
0
                 reference_rev) : 0;
161
0
  if (merged < 0)
162
0
    exit(128);
163
164
  /*
165
   * After the safety valve is fully redefined to "check with
166
   * upstream, if any, otherwise with HEAD", we should just
167
   * return the result of the repo_in_merge_bases() above without
168
   * any of the following code, but during the transition period,
169
   * a gentle reminder is in order.
170
   */
171
0
  if (head_rev != reference_rev) {
172
0
    int expect = head_rev ? repo_in_merge_bases(the_repository, rev, head_rev) : 0;
173
0
    if (expect < 0)
174
0
      exit(128);
175
0
    if (expect == merged)
176
0
      ; /* okay */
177
0
    else if (merged)
178
0
      warning(_("deleting branch '%s' that has been merged to\n"
179
0
        "         '%s', but not yet merged to HEAD"),
180
0
        name, reference_name);
181
0
    else
182
0
      warning(_("not deleting branch '%s' that is not yet merged to\n"
183
0
        "         '%s', even though it is merged to HEAD"),
184
0
        name, reference_name);
185
0
  }
186
0
  free(reference_name_to_free);
187
0
  return merged;
188
0
}
189
190
static int check_branch_commit(const char *branchname, const char *refname,
191
             const struct object_id *oid, struct commit *head_rev,
192
             int kinds, int force)
193
0
{
194
0
  struct commit *rev = lookup_commit_reference(the_repository, oid);
195
0
  if (!force && !rev) {
196
0
    error(_("couldn't look up commit object for '%s'"), refname);
197
0
    return -1;
198
0
  }
199
0
  if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
200
0
    error(_("the branch '%s' is not fully merged"), branchname);
201
0
    advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH,
202
0
          _("If you are sure you want to delete it, "
203
0
          "run 'git branch -D %s'"), branchname);
204
0
    return -1;
205
0
  }
206
0
  return 0;
207
0
}
208
209
static void delete_branch_config(const char *branchname)
210
0
{
211
0
  struct strbuf buf = STRBUF_INIT;
212
0
  strbuf_addf(&buf, "branch.%s", branchname);
213
0
  if (repo_config_rename_section(the_repository, buf.buf, NULL) < 0)
214
0
    warning(_("update of config-file failed"));
215
0
  strbuf_release(&buf);
216
0
}
217
218
static int delete_branches(int argc, const char **argv, int force, int kinds,
219
         int quiet)
220
0
{
221
0
  struct commit *head_rev = NULL;
222
0
  struct object_id oid;
223
0
  char *name = NULL;
224
0
  const char *fmt;
225
0
  int i;
226
0
  int ret = 0;
227
0
  int remote_branch = 0;
228
0
  struct strbuf bname = STRBUF_INIT;
229
0
  unsigned allowed_interpret;
230
0
  struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
231
0
  struct string_list_item *item;
232
0
  int branch_name_pos;
233
0
  const char *fmt_remotes = "refs/remotes/%s";
234
235
0
  switch (kinds) {
236
0
  case FILTER_REFS_REMOTES:
237
0
    fmt = fmt_remotes;
238
    /* For subsequent UI messages */
239
0
    remote_branch = 1;
240
0
    allowed_interpret = INTERPRET_BRANCH_REMOTE;
241
242
0
    force = 1;
243
0
    break;
244
0
  case FILTER_REFS_BRANCHES:
245
0
    fmt = "refs/heads/%s";
246
0
    allowed_interpret = INTERPRET_BRANCH_LOCAL;
247
0
    break;
248
0
  default:
249
0
    die(_("cannot use -a with -d"));
250
0
  }
251
0
  branch_name_pos = strcspn(fmt, "%");
252
253
0
  if (!force)
254
0
    head_rev = lookup_commit_reference(the_repository, &head_oid);
255
256
0
  for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
257
0
    char *target = NULL;
258
0
    int flags = 0;
259
260
0
    strbuf_branchname(&bname, argv[i], allowed_interpret);
261
0
    free(name);
262
0
    name = mkpathdup(fmt, bname.buf);
263
264
0
    if (kinds == FILTER_REFS_BRANCHES) {
265
0
      const char *path;
266
0
      if ((path = branch_checked_out(name))) {
267
0
        error(_("cannot delete branch '%s' "
268
0
          "used by worktree at '%s'"),
269
0
              bname.buf, path);
270
0
        ret = 1;
271
0
        continue;
272
0
      }
273
0
    }
274
275
0
    target = refs_resolve_refdup(get_main_ref_store(the_repository),
276
0
               name,
277
0
               RESOLVE_REF_READING
278
0
               | RESOLVE_REF_NO_RECURSE
279
0
               | RESOLVE_REF_ALLOW_BAD_NAME,
280
0
               &oid, &flags);
281
0
    if (!target) {
282
0
      if (remote_branch) {
283
0
        error(_("remote-tracking branch '%s' not found"), bname.buf);
284
0
      } else {
285
0
        char *virtual_name = mkpathdup(fmt_remotes, bname.buf);
286
0
        char *virtual_target = refs_resolve_refdup(get_main_ref_store(the_repository),
287
0
                     virtual_name,
288
0
                     RESOLVE_REF_READING
289
0
                     | RESOLVE_REF_NO_RECURSE
290
0
                     | RESOLVE_REF_ALLOW_BAD_NAME,
291
0
                     &oid,
292
0
                     &flags);
293
0
        FREE_AND_NULL(virtual_name);
294
295
0
        if (virtual_target)
296
0
          error(_("branch '%s' not found.\n"
297
0
            "Did you forget --remote?"),
298
0
            bname.buf);
299
0
        else
300
0
          error(_("branch '%s' not found"), bname.buf);
301
0
        FREE_AND_NULL(virtual_target);
302
0
      }
303
0
      ret = 1;
304
0
      continue;
305
0
    }
306
307
0
    if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
308
0
        check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
309
0
          force)) {
310
0
      ret = 1;
311
0
      goto next;
312
0
    }
313
314
0
    item = string_list_append(&refs_to_delete, name);
315
0
    item->util = xstrdup((flags & REF_ISBROKEN) ? "broken"
316
0
            : (flags & REF_ISSYMREF) ? target
317
0
            : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV));
318
319
0
  next:
320
0
    free(target);
321
0
  }
322
323
0
  if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
324
0
    ret = 1;
325
326
0
  for_each_string_list_item(item, &refs_to_delete) {
327
0
    char *describe_ref = item->util;
328
0
    char *name = item->string;
329
0
    if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
330
0
      char *refname = name + branch_name_pos;
331
0
      if (!quiet)
332
0
        printf(remote_branch
333
0
          ? _("Deleted remote-tracking branch %s (was %s).\n")
334
0
          : _("Deleted branch %s (was %s).\n"),
335
0
          name + branch_name_pos, describe_ref);
336
337
0
      delete_branch_config(refname);
338
0
    }
339
0
    free(describe_ref);
340
0
  }
341
0
  string_list_clear(&refs_to_delete, 0);
342
343
0
  free(name);
344
0
  strbuf_release(&bname);
345
346
0
  return ret;
347
0
}
348
349
static int calc_maxwidth(struct ref_array *refs, int remote_bonus)
350
0
{
351
0
  int i, max = 0;
352
0
  for (i = 0; i < refs->nr; i++) {
353
0
    struct ref_array_item *it = refs->items[i];
354
0
    const char *desc = it->refname;
355
0
    int w;
356
357
0
    skip_prefix(it->refname, "refs/heads/", &desc);
358
0
    skip_prefix(it->refname, "refs/remotes/", &desc);
359
0
    if (it->kind == FILTER_REFS_DETACHED_HEAD) {
360
0
      char *head_desc = get_head_description();
361
0
      w = utf8_strwidth(head_desc);
362
0
      free(head_desc);
363
0
    } else
364
0
      w = utf8_strwidth(desc);
365
366
0
    if (it->kind == FILTER_REFS_REMOTES)
367
0
      w += remote_bonus;
368
0
    if (w > max)
369
0
      max = w;
370
0
  }
371
0
  return max;
372
0
}
373
374
static const char *quote_literal_for_format(const char *s)
375
0
{
376
0
  static struct strbuf buf = STRBUF_INIT;
377
378
0
  strbuf_reset(&buf);
379
0
  while (strbuf_expand_step(&buf, &s))
380
0
    strbuf_addstr(&buf, "%%");
381
0
  return buf.buf;
382
0
}
383
384
static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix)
385
0
{
386
0
  struct strbuf fmt = STRBUF_INIT;
387
0
  struct strbuf local = STRBUF_INIT;
388
0
  struct strbuf remote = STRBUF_INIT;
389
390
0
  strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else)  %s%%(end)%%(end)",
391
0
      branch_get_color(BRANCH_COLOR_CURRENT),
392
0
      branch_get_color(BRANCH_COLOR_WORKTREE),
393
0
      branch_get_color(BRANCH_COLOR_LOCAL));
394
0
  strbuf_addf(&remote, "  %s",
395
0
        branch_get_color(BRANCH_COLOR_REMOTE));
396
397
0
  if (filter->verbose) {
398
0
    struct strbuf obname = STRBUF_INIT;
399
400
0
    if (filter->abbrev < 0)
401
0
      strbuf_addf(&obname, "%%(objectname:short)");
402
0
    else if (!filter->abbrev)
403
0
      strbuf_addf(&obname, "%%(objectname)");
404
0
    else
405
0
      strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
406
407
0
    strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
408
0
    strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
409
0
    strbuf_addf(&local, " %s ", obname.buf);
410
411
0
    if (filter->verbose > 1)
412
0
    {
413
0
      strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)",
414
0
            branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET));
415
0
      strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
416
0
            "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
417
0
            branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
418
0
    }
419
0
    else
420
0
      strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
421
422
0
    strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
423
0
          "%%(if)%%(symref)%%(then) -> %%(symref:short)"
424
0
          "%%(else) %s %%(contents:subject)%%(end)",
425
0
          maxwidth, quote_literal_for_format(remote_prefix),
426
0
          branch_get_color(BRANCH_COLOR_RESET), obname.buf);
427
0
    strbuf_release(&obname);
428
0
  } else {
429
0
    strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
430
0
          branch_get_color(BRANCH_COLOR_RESET));
431
0
    strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
432
0
          quote_literal_for_format(remote_prefix),
433
0
          branch_get_color(BRANCH_COLOR_RESET));
434
0
  }
435
436
0
  strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf);
437
438
0
  strbuf_release(&local);
439
0
  strbuf_release(&remote);
440
0
  return strbuf_detach(&fmt, NULL);
441
0
}
442
443
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
444
         struct ref_format *format, struct string_list *output)
445
0
{
446
0
  int i;
447
0
  struct ref_array array;
448
0
  int maxwidth = 0;
449
0
  const char *remote_prefix = "";
450
0
  char *to_free = NULL;
451
452
  /*
453
   * If we are listing more than just remote branches,
454
   * then remote branches will have a "remotes/" prefix.
455
   * We need to account for this in the width.
456
   */
457
0
  if (filter->kind != FILTER_REFS_REMOTES)
458
0
    remote_prefix = "remotes/";
459
460
0
  memset(&array, 0, sizeof(array));
461
462
0
  filter_refs(&array, filter, filter->kind);
463
464
0
  if (filter->verbose)
465
0
    maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
466
467
0
  if (!format->format)
468
0
    format->format = to_free = build_format(filter, maxwidth, remote_prefix);
469
0
  format->use_color = branch_use_color;
470
471
0
  if (verify_ref_format(format))
472
0
    die(_("unable to parse format string"));
473
474
0
  filter_ahead_behind(the_repository, format, &array);
475
0
  ref_array_sort(sorting, &array);
476
477
0
  if (column_active(colopts)) {
478
0
    struct strbuf out = STRBUF_INIT, err = STRBUF_INIT;
479
480
0
    assert(!filter->verbose && "--column and --verbose are incompatible");
481
482
0
    for (i = 0; i < array.nr; i++) {
483
0
      strbuf_reset(&err);
484
0
      strbuf_reset(&out);
485
0
      if (format_ref_array_item(array.items[i], format, &out, &err))
486
0
        die("%s", err.buf);
487
488
      /* format to a string_list to let print_columns() do its job */
489
0
      string_list_append(output, out.buf);
490
0
    }
491
492
0
    strbuf_release(&err);
493
0
    strbuf_release(&out);
494
0
  } else {
495
0
    print_formatted_ref_array(&array, format);
496
0
  }
497
498
0
  ref_array_clear(&array);
499
0
  free(to_free);
500
0
}
501
502
static void print_current_branch_name(void)
503
0
{
504
0
  int flags;
505
0
  const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
506
0
                  "HEAD", 0, NULL, &flags);
507
0
  const char *shortname;
508
0
  if (!refname)
509
0
    die(_("could not resolve HEAD"));
510
0
  else if (!(flags & REF_ISSYMREF))
511
0
    return;
512
0
  else if (skip_prefix(refname, "refs/heads/", &shortname))
513
0
    puts(shortname);
514
0
  else
515
0
    die(_("HEAD (%s) points outside of refs/heads/"), refname);
516
0
}
517
518
static void reject_rebase_or_bisect_branch(struct worktree **worktrees,
519
             const char *target)
520
0
{
521
0
  int i;
522
523
0
  for (i = 0; worktrees[i]; i++) {
524
0
    struct worktree *wt = worktrees[i];
525
526
0
    if (!wt->is_detached)
527
0
      continue;
528
529
0
    if (is_worktree_being_rebased(wt, target))
530
0
      die(_("branch %s is being rebased at %s"),
531
0
          target, wt->path);
532
533
0
    if (is_worktree_being_bisected(wt, target))
534
0
      die(_("branch %s is being bisected at %s"),
535
0
          target, wt->path);
536
0
  }
537
0
}
538
539
/*
540
 * Update all per-worktree HEADs pointing at the old ref to point the new ref.
541
 * This will be used when renaming a branch. Returns 0 if successful, non-zero
542
 * otherwise.
543
 */
544
static int replace_each_worktree_head_symref(struct worktree **worktrees,
545
               const char *oldref, const char *newref,
546
               const char *logmsg)
547
0
{
548
0
  int ret = 0;
549
0
  int i;
550
551
0
  for (i = 0; worktrees[i]; i++) {
552
0
    struct ref_store *refs;
553
554
0
    if (worktrees[i]->is_detached)
555
0
      continue;
556
0
    if (!worktrees[i]->head_ref)
557
0
      continue;
558
0
    if (strcmp(oldref, worktrees[i]->head_ref))
559
0
      continue;
560
561
0
    refs = get_worktree_ref_store(worktrees[i]);
562
0
    if (refs_update_symref(refs, "HEAD", newref, logmsg))
563
0
      ret = error(_("HEAD of working tree %s is not updated"),
564
0
            worktrees[i]->path);
565
0
  }
566
567
0
  return ret;
568
0
}
569
570
0
#define IS_HEAD 1
571
0
#define IS_ORPHAN 2
572
573
static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
574
0
{
575
0
  struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
576
0
  struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
577
0
  const char *interpreted_oldname = NULL;
578
0
  const char *interpreted_newname = NULL;
579
0
  int recovery = 0, oldref_usage = 0;
580
0
  struct worktree **worktrees = get_worktrees();
581
582
0
  if (strbuf_check_branch_ref(&oldref, oldname)) {
583
    /*
584
     * Bad name --- this could be an attempt to rename a
585
     * ref that we used to allow to be created by accident.
586
     */
587
0
    if (refs_ref_exists(get_main_ref_store(the_repository), oldref.buf))
588
0
      recovery = 1;
589
0
    else {
590
0
      int code = die_message(_("invalid branch name: '%s'"), oldname);
591
0
      advise_if_enabled(ADVICE_REF_SYNTAX,
592
0
            _("See `man git check-ref-format`"));
593
0
      exit(code);
594
0
    }
595
0
  }
596
597
0
  for (int i = 0; worktrees[i]; i++) {
598
0
    struct worktree *wt = worktrees[i];
599
600
0
    if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) {
601
0
      oldref_usage |= IS_HEAD;
602
0
      if (is_null_oid(&wt->head_oid))
603
0
        oldref_usage |= IS_ORPHAN;
604
0
      break;
605
0
    }
606
0
  }
607
608
0
  if ((copy || !(oldref_usage & IS_HEAD)) && !refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) {
609
0
    if (oldref_usage & IS_HEAD)
610
0
      die(_("no commit on branch '%s' yet"), oldname);
611
0
    else
612
0
      die(_("no branch named '%s'"), oldname);
613
0
  }
614
615
  /*
616
   * A command like "git branch -M currentbranch currentbranch" cannot
617
   * cause the worktree to become inconsistent with HEAD, so allow it.
618
   */
619
0
  if (!strcmp(oldname, newname))
620
0
    validate_branchname(newname, &newref);
621
0
  else
622
0
    validate_new_branchname(newname, &newref, force);
623
624
0
  reject_rebase_or_bisect_branch(worktrees, oldref.buf);
625
626
0
  if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
627
0
      !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
628
0
    BUG("expected prefix missing for refs");
629
0
  }
630
631
0
  if (copy)
632
0
    strbuf_addf(&logmsg, "Branch: copied %s to %s",
633
0
          oldref.buf, newref.buf);
634
0
  else
635
0
    strbuf_addf(&logmsg, "Branch: renamed %s to %s",
636
0
          oldref.buf, newref.buf);
637
638
0
  if (!copy && !(oldref_usage & IS_ORPHAN) &&
639
0
      refs_rename_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf))
640
0
    die(_("branch rename failed"));
641
0
  if (copy && refs_copy_existing_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf))
642
0
    die(_("branch copy failed"));
643
644
0
  if (recovery) {
645
0
    if (copy)
646
0
      warning(_("created a copy of a misnamed branch '%s'"),
647
0
        interpreted_oldname);
648
0
    else
649
0
      warning(_("renamed a misnamed branch '%s' away"),
650
0
        interpreted_oldname);
651
0
  }
652
653
0
  if (!copy && (oldref_usage & IS_HEAD) &&
654
0
      replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf,
655
0
                logmsg.buf))
656
0
    die(_("branch renamed to %s, but HEAD is not updated"), newname);
657
658
0
  strbuf_release(&logmsg);
659
660
0
  strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
661
0
  strbuf_addf(&newsection, "branch.%s", interpreted_newname);
662
0
  if (!copy && repo_config_rename_section(the_repository, oldsection.buf, newsection.buf) < 0)
663
0
    die(_("branch is renamed, but update of config-file failed"));
664
0
  if (copy && strcmp(interpreted_oldname, interpreted_newname) &&
665
0
      repo_config_copy_section(the_repository, oldsection.buf, newsection.buf) < 0)
666
0
    die(_("branch is copied, but update of config-file failed"));
667
0
  strbuf_release(&oldref);
668
0
  strbuf_release(&newref);
669
0
  strbuf_release(&oldsection);
670
0
  strbuf_release(&newsection);
671
0
  free_worktrees(worktrees);
672
0
}
673
674
static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
675
676
static int edit_branch_description(const char *branch_name)
677
0
{
678
0
  int exists;
679
0
  struct strbuf buf = STRBUF_INIT;
680
0
  struct strbuf name = STRBUF_INIT;
681
682
0
  exists = !read_branch_desc(&buf, branch_name);
683
0
  if (!buf.len || buf.buf[buf.len-1] != '\n')
684
0
    strbuf_addch(&buf, '\n');
685
0
  strbuf_commented_addf(&buf, comment_line_str,
686
0
        _("Please edit the description for the branch\n"
687
0
          "  %s\n"
688
0
          "Lines starting with '%s' will be stripped.\n"),
689
0
        branch_name, comment_line_str);
690
0
  write_file_buf(edit_description(), buf.buf, buf.len);
691
0
  strbuf_reset(&buf);
692
0
  if (launch_editor(edit_description(), &buf, NULL)) {
693
0
    strbuf_release(&buf);
694
0
    return -1;
695
0
  }
696
0
  strbuf_stripspace(&buf, comment_line_str);
697
698
0
  strbuf_addf(&name, "branch.%s.description", branch_name);
699
0
  if (buf.len || exists)
700
0
    git_config_set(name.buf, buf.len ? buf.buf : NULL);
701
0
  strbuf_release(&name);
702
0
  strbuf_release(&buf);
703
704
0
  return 0;
705
0
}
706
707
int cmd_branch(int argc, const char **argv, const char *prefix)
708
0
{
709
  /* possible actions */
710
0
  int delete = 0, rename = 0, copy = 0, list = 0,
711
0
      unset_upstream = 0, show_current = 0, edit_description = 0;
712
0
  const char *new_upstream = NULL;
713
0
  int noncreate_actions = 0;
714
  /* possible options */
715
0
  int reflog = 0, quiet = 0, icase = 0, force = 0,
716
0
      recurse_submodules_explicit = 0;
717
0
  enum branch_track track;
718
0
  struct ref_filter filter = REF_FILTER_INIT;
719
0
  static struct ref_sorting *sorting;
720
0
  struct string_list sorting_options = STRING_LIST_INIT_DUP;
721
0
  struct ref_format format = REF_FORMAT_INIT;
722
723
0
  struct option options[] = {
724
0
    OPT_GROUP(N_("Generic options")),
725
0
    OPT__VERBOSE(&filter.verbose,
726
0
      N_("show hash and subject, give twice for upstream branch")),
727
0
    OPT__QUIET(&quiet, N_("suppress informational messages")),
728
0
    OPT_CALLBACK_F('t', "track",  &track, "(direct|inherit)",
729
0
      N_("set branch tracking configuration"),
730
0
      PARSE_OPT_OPTARG,
731
0
      parse_opt_tracking_mode),
732
0
    OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
733
0
      BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
734
0
    OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
735
0
    OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")),
736
0
    OPT__COLOR(&branch_use_color, N_("use colored output")),
737
0
    OPT_SET_INT_F('r', "remotes",     &filter.kind, N_("act on remote-tracking branches"),
738
0
            FILTER_REFS_REMOTES,
739
0
            PARSE_OPT_NONEG),
740
0
    OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")),
741
0
    OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")),
742
0
    OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")),
743
0
    OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")),
744
0
    OPT__ABBREV(&filter.abbrev),
745
746
0
    OPT_GROUP(N_("Specific git-branch actions:")),
747
0
    OPT_SET_INT_F('a', "all", &filter.kind, N_("list both remote-tracking and local branches"),
748
0
            FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES,
749
0
            PARSE_OPT_NONEG),
750
0
    OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
751
0
    OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
752
0
    OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
753
0
    OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
754
0
    OPT_BOOL(0, "omit-empty",  &format.array_opts.omit_empty,
755
0
      N_("do not output a newline after empty formatted refs")),
756
0
    OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
757
0
    OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
758
0
    OPT_BOOL('l', "list", &list, N_("list branch names")),
759
0
    OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")),
760
0
    OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
761
0
    OPT_BOOL(0, "edit-description", &edit_description,
762
0
       N_("edit the description for the branch")),
763
0
    OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
764
0
    OPT_MERGED(&filter, N_("print only branches that are merged")),
765
0
    OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
766
0
    OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
767
0
    OPT_REF_SORT(&sorting_options),
768
0
    OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"),
769
0
      N_("print only branches of the object"), parse_opt_object_name),
770
0
    OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
771
0
    OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")),
772
0
    OPT_STRING(  0 , "format", &format.format, N_("format"), N_("format to use for the output")),
773
0
    OPT_END(),
774
0
  };
775
776
0
  setup_ref_filter_porcelain_msg();
777
778
0
  filter.kind = FILTER_REFS_BRANCHES;
779
0
  filter.abbrev = -1;
780
781
0
  if (argc == 2 && !strcmp(argv[1], "-h"))
782
0
    usage_with_options(builtin_branch_usage, options);
783
784
  /*
785
   * Try to set sort keys from config. If config does not set any,
786
   * fall back on default (refname) sorting.
787
   */
788
0
  git_config(git_branch_config, &sorting_options);
789
0
  if (!sorting_options.nr)
790
0
    string_list_append(&sorting_options, "refname");
791
792
0
  track = git_branch_track;
793
794
0
  head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
795
0
           0, &head_oid, NULL);
796
0
  if (!head)
797
0
    die(_("failed to resolve HEAD as a valid ref"));
798
0
  if (!strcmp(head, "HEAD"))
799
0
    filter.detached = 1;
800
0
  else if (!skip_prefix(head, "refs/heads/", &head))
801
0
    die(_("HEAD not found below refs/heads!"));
802
803
0
  argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
804
0
           0);
805
806
0
  if (!delete && !rename && !copy && !edit_description && !new_upstream &&
807
0
      !show_current && !unset_upstream && argc == 0)
808
0
    list = 1;
809
810
0
  if (filter.with_commit || filter.no_commit ||
811
0
      filter.reachable_from || filter.unreachable_from || filter.points_at.nr)
812
0
    list = 1;
813
814
0
  noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +
815
0
          !!show_current + !!list + !!edit_description +
816
0
          !!unset_upstream;
817
0
  if (noncreate_actions > 1)
818
0
    usage_with_options(builtin_branch_usage, options);
819
820
0
  if (recurse_submodules_explicit) {
821
0
    if (!submodule_propagate_branches)
822
0
      die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
823
0
    if (noncreate_actions)
824
0
      die(_("--recurse-submodules can only be used to create branches"));
825
0
  }
826
827
0
  recurse_submodules =
828
0
    (recurse_submodules || recurse_submodules_explicit) &&
829
0
    submodule_propagate_branches;
830
831
0
  if (filter.abbrev == -1)
832
0
    filter.abbrev = DEFAULT_ABBREV;
833
0
  filter.ignore_case = icase;
834
835
0
  finalize_colopts(&colopts, -1);
836
0
  if (filter.verbose) {
837
0
    if (explicitly_enable_column(colopts))
838
0
      die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose");
839
0
    colopts = 0;
840
0
  }
841
842
0
  if (force) {
843
0
    delete *= 2;
844
0
    rename *= 2;
845
0
    copy *= 2;
846
0
  }
847
848
0
  if (list)
849
0
    setup_auto_pager("branch", 1);
850
851
0
  UNLEAK(sorting_options);
852
853
0
  if (delete) {
854
0
    if (!argc)
855
0
      die(_("branch name required"));
856
0
    return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
857
0
  } else if (show_current) {
858
0
    print_current_branch_name();
859
0
    return 0;
860
0
  } else if (list) {
861
    /*  git branch --list also shows HEAD when it is detached */
862
0
    if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
863
0
      filter.kind |= FILTER_REFS_DETACHED_HEAD;
864
0
    filter.name_patterns = argv;
865
    /*
866
     * If no sorting parameter is given then we default to sorting
867
     * by 'refname'. This would give us an alphabetically sorted
868
     * array with the 'HEAD' ref at the beginning followed by
869
     * local branches 'refs/heads/...' and finally remote-tracking
870
     * branches 'refs/remotes/...'.
871
     */
872
0
    sorting = ref_sorting_options(&sorting_options);
873
0
    ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
874
0
    ref_sorting_set_sort_flags_all(
875
0
      sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
876
0
    print_ref_list(&filter, sorting, &format, &output);
877
0
    print_columns(&output, colopts, NULL);
878
0
    string_list_clear(&output, 0);
879
0
    ref_sorting_release(sorting);
880
0
    ref_filter_clear(&filter);
881
0
    return 0;
882
0
  } else if (edit_description) {
883
0
    const char *branch_name;
884
0
    struct strbuf branch_ref = STRBUF_INIT;
885
0
    struct strbuf buf = STRBUF_INIT;
886
0
    int ret = 1; /* assume failure */
887
888
0
    if (!argc) {
889
0
      if (filter.detached)
890
0
        die(_("cannot give description to detached HEAD"));
891
0
      branch_name = head;
892
0
    } else if (argc == 1) {
893
0
      strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
894
0
      branch_name = buf.buf;
895
0
    } else {
896
0
      die(_("cannot edit description of more than one branch"));
897
0
    }
898
899
0
    strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
900
0
    if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf))
901
0
      error((!argc || branch_checked_out(branch_ref.buf))
902
0
            ? _("no commit on branch '%s' yet")
903
0
            : _("no branch named '%s'"),
904
0
            branch_name);
905
0
    else if (!edit_branch_description(branch_name))
906
0
      ret = 0; /* happy */
907
908
0
    strbuf_release(&branch_ref);
909
0
    strbuf_release(&buf);
910
911
0
    return ret;
912
0
  } else if (copy || rename) {
913
0
    if (!argc)
914
0
      die(_("branch name required"));
915
0
    else if ((argc == 1) && filter.detached)
916
0
      die(copy? _("cannot copy the current branch while not on any")
917
0
        : _("cannot rename the current branch while not on any"));
918
0
    else if (argc == 1)
919
0
      copy_or_rename_branch(head, argv[0], copy, copy + rename > 1);
920
0
    else if (argc == 2)
921
0
      copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1);
922
0
    else
923
0
      die(copy? _("too many branches for a copy operation")
924
0
        : _("too many arguments for a rename operation"));
925
0
  } else if (new_upstream) {
926
0
    struct branch *branch;
927
0
    struct strbuf buf = STRBUF_INIT;
928
929
0
    if (!argc)
930
0
      branch = branch_get(NULL);
931
0
    else if (argc == 1) {
932
0
      strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
933
0
      branch = branch_get(buf.buf);
934
0
    } else
935
0
      die(_("too many arguments to set new upstream"));
936
937
0
    if (!branch) {
938
0
      if (!argc || !strcmp(argv[0], "HEAD"))
939
0
        die(_("could not set upstream of HEAD to %s when "
940
0
              "it does not point to any branch"),
941
0
            new_upstream);
942
0
      die(_("no such branch '%s'"), argv[0]);
943
0
    }
944
945
0
    if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) {
946
0
      if (!argc || branch_checked_out(branch->refname))
947
0
        die(_("no commit on branch '%s' yet"), branch->name);
948
0
      die(_("branch '%s' does not exist"), branch->name);
949
0
    }
950
951
0
    dwim_and_setup_tracking(the_repository, branch->name,
952
0
          new_upstream, BRANCH_TRACK_OVERRIDE,
953
0
          quiet);
954
0
    strbuf_release(&buf);
955
0
  } else if (unset_upstream) {
956
0
    struct branch *branch;
957
0
    struct strbuf buf = STRBUF_INIT;
958
959
0
    if (!argc)
960
0
      branch = branch_get(NULL);
961
0
    else if (argc == 1) {
962
0
      strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
963
0
      branch = branch_get(buf.buf);
964
0
    } else
965
0
      die(_("too many arguments to unset upstream"));
966
967
0
    if (!branch) {
968
0
      if (!argc || !strcmp(argv[0], "HEAD"))
969
0
        die(_("could not unset upstream of HEAD when "
970
0
              "it does not point to any branch"));
971
0
      die(_("no such branch '%s'"), argv[0]);
972
0
    }
973
974
0
    if (!branch_has_merge_config(branch))
975
0
      die(_("branch '%s' has no upstream information"), branch->name);
976
977
0
    strbuf_reset(&buf);
978
0
    strbuf_addf(&buf, "branch.%s.remote", branch->name);
979
0
    git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
980
0
    strbuf_reset(&buf);
981
0
    strbuf_addf(&buf, "branch.%s.merge", branch->name);
982
0
    git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
983
0
    strbuf_release(&buf);
984
0
  } else if (!noncreate_actions && argc > 0 && argc <= 2) {
985
0
    const char *branch_name = argv[0];
986
0
    const char *start_name = argc == 2 ? argv[1] : head;
987
988
0
    if (filter.kind != FILTER_REFS_BRANCHES)
989
0
      die(_("the -a, and -r, options to 'git branch' do not take a branch name.\n"
990
0
          "Did you mean to use: -a|-r --list <pattern>?"));
991
992
0
    if (track == BRANCH_TRACK_OVERRIDE)
993
0
      die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead"));
994
995
0
    if (recurse_submodules) {
996
0
      create_branches_recursively(the_repository, branch_name,
997
0
                start_name, NULL, force,
998
0
                reflog, quiet, track, 0);
999
0
      return 0;
1000
0
    }
1001
0
    create_branch(the_repository, branch_name, start_name, force, 0,
1002
0
            reflog, quiet, track, 0);
1003
0
  } else
1004
0
    usage_with_options(builtin_branch_usage, options);
1005
1006
0
  return 0;
1007
0
}