Merge branch 'tg/range-diff-output-update'
[gitweb.git] / builtin / branch.c
index bbd006aab4b4798b7e86530fe3562c753e879bc1..2ef214632f025b0da7bf5f118aebe3c68d5af2f8 100644 (file)
@@ -23,6 +23,7 @@
 #include "ref-filter.h"
 #include "worktree.h"
 #include "help.h"
+#include "commit-reach.h"
 
 static const char * const builtin_branch_usage[] = {
        N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@ -37,7 +38,6 @@ static const char * const builtin_branch_usage[] = {
 
 static const char *head;
 static struct object_id head_oid;
-static int used_deprecated_reflog_option;
 
 static int branch_use_color = -1;
 static char branch_colors[][COLOR_MAXLEN] = {
@@ -47,6 +47,7 @@ static char branch_colors[][COLOR_MAXLEN] = {
        GIT_COLOR_NORMAL,       /* LOCAL */
        GIT_COLOR_GREEN,        /* CURRENT */
        GIT_COLOR_BLUE,         /* UPSTREAM */
+       GIT_COLOR_CYAN,         /* WORKTREE */
 };
 enum color_branch {
        BRANCH_COLOR_RESET = 0,
@@ -54,7 +55,8 @@ enum color_branch {
        BRANCH_COLOR_REMOTE = 2,
        BRANCH_COLOR_LOCAL = 3,
        BRANCH_COLOR_CURRENT = 4,
-       BRANCH_COLOR_UPSTREAM = 5
+       BRANCH_COLOR_UPSTREAM = 5,
+       BRANCH_COLOR_WORKTREE = 6
 };
 
 static const char *color_branch_slots[] = {
@@ -64,6 +66,7 @@ static const char *color_branch_slots[] = {
        [BRANCH_COLOR_LOCAL]    = "local",
        [BRANCH_COLOR_CURRENT]  = "current",
        [BRANCH_COLOR_UPSTREAM] = "upstream",
+       [BRANCH_COLOR_WORKTREE] = "worktree",
 };
 
 static struct string_list output = STRING_LIST_INIT_DUP;
@@ -342,9 +345,10 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
        struct strbuf local = STRBUF_INIT;
        struct strbuf remote = STRBUF_INIT;
 
-       strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)  %s%%(end)",
-                   branch_get_color(BRANCH_COLOR_CURRENT),
-                   branch_get_color(BRANCH_COLOR_LOCAL));
+       strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else)  %s%%(end)%%(end)",
+                       branch_get_color(BRANCH_COLOR_CURRENT),
+                       branch_get_color(BRANCH_COLOR_WORKTREE),
+                       branch_get_color(BRANCH_COLOR_LOCAL));
        strbuf_addf(&remote, "  %s",
                    branch_get_color(BRANCH_COLOR_REMOTE));
 
@@ -363,9 +367,13 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
                strbuf_addf(&local, " %s ", obname.buf);
 
                if (filter->verbose > 1)
+               {
+                       strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)",
+                                   branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET));
                        strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
                                    "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
                                    branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
+               }
                else
                        strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
 
@@ -443,6 +451,21 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
        free(to_free);
 }
 
+static void print_current_branch_name(void)
+{
+       int flags;
+       const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
+       const char *shortname;
+       if (!refname)
+               die(_("could not resolve HEAD"));
+       else if (!(flags & REF_ISSYMREF))
+               return;
+       else if (skip_prefix(refname, "refs/heads/", &shortname))
+               puts(shortname);
+       else
+               die(_("HEAD (%s) points outside of refs/heads/"), refname);
+}
+
 static void reject_rebase_or_bisect_branch(const char *target)
 {
        struct worktree **worktrees = get_worktrees(0);
@@ -578,17 +601,10 @@ static int edit_branch_description(const char *branch_name)
        return 0;
 }
 
-static int deprecated_reflog_option_cb(const struct option *opt,
-                                      const char *arg, int unset)
-{
-       used_deprecated_reflog_option = 1;
-       *(int *)opt->value = !unset;
-       return 0;
-}
-
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
        int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
+       int show_current = 0;
        int reflog = 0, edit_description = 0;
        int quiet = 0, unset_upstream = 0;
        const char *new_upstream = NULL;
@@ -627,22 +643,16 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
                OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
                OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
-               OPT_BOOL(0, "list", &list, N_("list branch names")),
+               OPT_BOOL('l', "list", &list, N_("list branch names")),
+               OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")),
                OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
-               {
-                       OPTION_CALLBACK, 'l', NULL, &reflog, NULL,
-                       N_("deprecated synonym for --create-reflog"),
-                       PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
-                       deprecated_reflog_option_cb
-               },
                OPT_BOOL(0, "edit-description", &edit_description,
                         N_("edit the description for the branch")),
                OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
                OPT_MERGED(&filter, N_("print only branches that are merged")),
                OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
                OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
-               OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
-                            N_("field name to sort on"), &parse_opt_ref_sorting),
+               OPT_REF_SORT(sorting_tail),
                {
                        OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
                        N_("print only branches of the object"), 0, parse_opt_object_name
@@ -676,14 +686,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
                             0);
 
-       if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0)
+       if (!delete && !rename && !copy && !edit_description && !new_upstream &&
+           !show_current && !unset_upstream && argc == 0)
                list = 1;
 
        if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
            filter.no_commit)
                list = 1;
 
-       if (!!delete + !!rename + !!copy + !!new_upstream +
+       if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
            list + unset_upstream > 1)
                usage_with_options(builtin_branch_usage, options);
 
@@ -707,15 +718,13 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        if (list)
                setup_auto_pager("branch", 1);
 
-       if (used_deprecated_reflog_option && !list) {
-               warning("the '-l' alias for '--create-reflog' is deprecated;");
-               warning("it will be removed in a future version of Git");
-       }
-
        if (delete) {
                if (!argc)
                        die(_("branch name required"));
                return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
+       } else if (show_current) {
+               print_current_branch_name();
+               return 0;
        } else if (list) {
                /*  git branch --local also shows HEAD when it is detached */
                if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
@@ -735,8 +744,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                print_columns(&output, colopts, NULL);
                string_list_clear(&output, 0);
                return 0;
-       }
-       else if (edit_description) {
+       } else if (edit_description) {
                const char *branch_name;
                struct strbuf branch_ref = STRBUF_INIT;
 
@@ -803,7 +811,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                 * create_branch takes care of setting up the tracking
                 * info and making sure new_upstream is correct
                 */
-               create_branch(branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
+               create_branch(the_repository, branch->name, new_upstream,
+                             0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
        } else if (unset_upstream) {
                struct branch *branch = branch_get(argv[0]);
                struct strbuf buf = STRBUF_INIT;
@@ -828,18 +837,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                git_config_set_multivar(buf.buf, NULL, NULL, 1);
                strbuf_release(&buf);
        } else if (argc > 0 && argc <= 2) {
-               struct branch *branch = branch_get(argv[0]);
-
-               if (!branch)
-                       die(_("no such branch '%s'"), argv[0]);
-
                if (filter.kind != FILTER_REFS_BRANCHES)
-                       die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
+                       die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n"
+                                 "Did you mean to use: -a|-r --list <pattern>?"));
 
                if (track == BRANCH_TRACK_OVERRIDE)
                        die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
 
-               create_branch(argv[0], (argc == 2) ? argv[1] : head,
+               create_branch(the_repository,
+                             argv[0], (argc == 2) ? argv[1] : head,
                              force, 0, reflog, quiet, track);
 
        } else