git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Kenneth Cochran <kenneth.cochran101@gmail.com>
To: Kenneth Cochran <kenneth.cochran101@gmail.com>
Cc: git@vger.kernel.org, Sahil Dua <sahildua2305@gmail.com>,
	Duy Nguyen <pclouds@gmail.com>, Jeff King <peff@peff.net>
Subject: Re: [RFC PATCH 1/4] Add alias option to git branch
Date: Mon, 4 Mar 2019 09:47:22 -0600	[thread overview]
Message-ID: <7D21A788-5E38-466F-B3CC-F6A5CBEB2E2E@gmail.com> (raw)
In-Reply-To: <47CA9077-E8C1-4093-8B36-2D5D3DE2D886@gmail.com>

From 2f6409a39b24db826a22d2813ec8d5de46723500 Mon Sep 17 00:00:00 2001
From: Kenneth Cochran <kenneth.cochran101@gmail.com>
Date: Tue, 26 Feb 2019 04:41:22 -0600
Subject: [RFC PATCH 1/4] branch: add "--alias" option to create an alias
Cc: Sahil Dua <sahildua2305@gmail.com>,
    Duy Nguyen <pclouds@gmail.com>,
    Jeff King <peff@peff.net>

Often, people have to use long or unweildly branch names
e.g. `feature/<bug_number>`. When working locally, it's nice to be
able to refer to that by something more friendly. It's already possible
to do this, with `git symbolic-ref refs/heads/alias_name refs/heads/branch_name`
I see three problems with this current approach:
  1. Typing out "refs/heads/" is tedious and error prone
  2. git will willingly overwrite existing branch names
  3. Deleting a checked out symref leaves head in an invalid state.

This commit solves the first two of the above issues.
I've implemented this as a new option for git branch, since this seemed like the
best place for it. We'll still need additional work to improve how git handles
deleting checked out symrefs.

These changes were originally proposed by Phil Sainty, but it doesn't seem
like there was ever any discussion about it:
https://www.mail-archive.com/git@vger.kernel.org/msg161274.html

Reported-by: Phil Sainty <psainty@orcon.net.nz>
Signed-off-by: Kenneth Cochran <kenneth.cochran101@gmail.com>
---
 Documentation/git-branch.txt |  8 +++++
 builtin/branch.c             | 29 ++++++++++++++++--
 t/t3207-branch-alias.sh      | 58 ++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 2 deletions(-)
 create mode 100755 t/t3207-branch-alias.sh

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 3bd83a7cbd..0476c8567b 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -19,6 +19,7 @@ SYNOPSIS
 'git branch' --unset-upstream [<branchname>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
 'git branch' (-c | -C) [<oldbranch>] <newbranch>
+'git branch' --alias <aliasname> [<branchname>]
 'git branch' (-d | -D) [-r] <branchname>...
 'git branch' --edit-description [<branchname>]
 
@@ -69,6 +70,10 @@ The `-c` and `-C` options have the exact same semantics as `-m` and
 `-M`, except instead of the branch being renamed it along with its
 config and reflog will be copied to a new name.
 
+With a `--alias` option, a symbolic ref with `<aliasname>` will be
+created. You may specify a branch to create the alias for. If one is
+not specified, the currently checked out branch is assumed.
+
 With a `-d` or `-D` option, `<branchname>` will be deleted.  You may
 specify more than one branch for deletion.  If the branch currently
 has a reflog then the reflog will also be deleted.
@@ -124,6 +129,9 @@ OPTIONS
 -C::
 	Shortcut for `--copy --force`.
 
+--alias::
+	Create an alias for a branch.
+
 --color[=<when>]::
 	Color branches to highlight current, local, and
 	remote-tracking branches.
diff --git a/builtin/branch.c b/builtin/branch.c
index 1be727209b..4b8b8fc08f 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -547,6 +547,20 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
 	strbuf_release(&newsection);
 }
 
+static void create_branch_alias(const char* branch_name, const char* alias_name) {
+	struct strbuf branch_ref = STRBUF_INIT;
+	struct strbuf alias_ref = STRBUF_INIT;
+
+	if(!validate_branchname(branch_name, &branch_ref))
+		die(_("%s is not a valid branch"), branch_name);
+	validate_new_branchname(alias_name, &alias_ref, 0);
+	create_symref(alias_ref.buf, branch_ref.buf, "");
+
+	strbuf_release(&branch_ref);
+	strbuf_release(&alias_ref);
+	printf(_("%s created as an alias for %s\n"), alias_name, branch_name);
+}
+
 static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
 
 static int edit_branch_description(const char *branch_name)
@@ -580,7 +594,7 @@ static int edit_branch_description(const char *branch_name)
 
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
-	int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
+	int delete = 0, rename = 0, copy = 0, force = 0, list = 0, alias = 0;
 	int reflog = 0, edit_description = 0;
 	int quiet = 0, unset_upstream = 0;
 	const char *new_upstream = NULL;
@@ -617,6 +631,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
 		OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
 		OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
+		OPT_BOOL(0, "alias", &alias, N_("create an alias for a branch")),
 		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('l', "list", &list, N_("list branch names")),
@@ -662,7 +677,8 @@ 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 && !unset_upstream &&
+	    !alias && argc == 0)
 		list = 1;
 
 	if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
@@ -762,6 +778,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
 		else
 			die(_("too many arguments for a rename operation"));
+	} else if (alias) {
+		if (!argc)
+			die(_("alias name required"));
+		else if (argc == 1)
+			create_branch_alias(head, argv[0]);
+		else if (argc == 2)
+			create_branch_alias(argv[1], argv[0]);
+		else
+			die(_("too many arguments for an alias operation"));
 	} else if (new_upstream) {
 		struct branch *branch = branch_get(argv[0]);
 
diff --git a/t/t3207-branch-alias.sh b/t/t3207-branch-alias.sh
new file mode 100755
index 0000000000..9d4c8c2914
--- /dev/null
+++ b/t/t3207-branch-alias.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Amos Waterland
+#
+
+test_description='git branch assorted tests'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'prepare a trivial repository' '
+	echo Hello >A &&
+	git update-index --add A &&
+	git commit -m "Initial commit." &&
+	echo World >>A &&
+	git update-index --add A &&
+	git commit -m "Second commit." &&
+	HEAD=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'git branch --alias' '
+	test_must_fail git branch --alias
+'
+
+test_expect_success 'git branch --alias sym' '
+	echo "sym created as an alias for master" >expect &&
+	git branch --alias sym >actual &&
+	test_i18ncmp expect actual &&
+	echo $HEAD >expect &&
+	git rev-parse --verify sym >actual &&
+	test_i18ncmp expect actual
+'
+
+test_expect_success 'git branch --alias sym1 brnch' '
+	git branch brnch &&
+	echo "sym1 created as an alias for brnch" >expect &&
+	git branch --alias sym1 brnch >actual &&
+	test_i18ncmp expect actual &&
+	git rev-parse --verify brnch >expect &&
+	git rev-parse --verify sym1 >actual &&
+	test_i18ncmp expect actual
+'
+
+test_expect_success 'git branch --alias sym2 brnch2 third_arg' '
+	test_must_fail git branch --alias sym2 brnch2 third_arg
+'
+
+test_expect_success 'git branch --alias refuses to overwrite existing branch' '
+	git branch bre &&
+	test_must_fail git branch --alias bre
+'
+
+test_expect_success 'git branch --alias refuses to overwrite existing symref' '
+	git branch --alias syme &&
+	test_must_fail git branch --alias syme
+'
+
+test_done
-- 
2.17.1



  reply	other threads:[~2019-03-04 15:47 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-04 15:45 [RFC PATCH 0/4] Add alias option to git branch Kenneth Cochran
2019-03-04 15:47 ` Kenneth Cochran [this message]
2019-03-04 15:48   ` [RFC PATCH 2/4] " Kenneth Cochran
2019-03-04 15:49     ` [RFC PATCH 3/4] " Kenneth Cochran
2019-03-04 15:49       ` [RFC PATCH 4/4] " Kenneth Cochran
2019-03-04 16:38         ` Kenneth Cochran
     [not found]         ` <CAJ145vUChd7+5QkmJsOK3bzZsudWfzZYp5wHZDzoq8SKSv0g0A@mail.gmail.com>
2019-03-04 19:33           ` Phil Sainty
2019-03-04 19:52             ` Kenneth Cochran
2019-03-05 13:21 ` [RFC PATCH 0/4] " Junio C Hamano
2019-03-05 17:36   ` Kenneth Cochran
2019-03-06 10:13     ` Phil Sainty

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7D21A788-5E38-466F-B3CC-F6A5CBEB2E2E@gmail.com \
    --to=kenneth.cochran101@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=sahildua2305@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).