From patchwork Tue Oct 9 18:31:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniels Umanovskis X-Patchwork-Id: 10633121 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DAB6D174A for ; Tue, 9 Oct 2018 18:31:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D4924295B1 for ; Tue, 9 Oct 2018 18:31:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D2BDA295A6; Tue, 9 Oct 2018 18:31:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 09EB92959F for ; Tue, 9 Oct 2018 18:31:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726492AbeJJBtn (ORCPT ); Tue, 9 Oct 2018 21:49:43 -0400 Received: from mail.weplayciv.com ([162.221.200.53]:60522 "EHLO mail.weplayciv.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726460AbeJJBtn (ORCPT ); Tue, 9 Oct 2018 21:49:43 -0400 Received: from localhost.localdomain (c-3985e555.02-149-6c6b7013.bbcust.telenor.se [85.229.133.57]) by mail.weplayciv.com (Postfix) with ESMTPSA id EE42794E081; Tue, 9 Oct 2018 11:31:25 -0700 (PDT) From: Daniels Umanovskis To: git@vger.kernel.org Cc: Daniels Umanovskis Subject: [PATCH 1/2] branch: introduce --current display option Date: Tue, 9 Oct 2018 20:31:13 +0200 Message-Id: <20181009183114.16477-1-daniels@umanovskis.se> X-Mailer: git-send-email 2.19.1.274.g059d67db4.dirty MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When called with --current, git branch will print the current branch name and terminate. It will print HEAD in detached-head state. Rationale: finding out the current branch is useful interactively, but especially in scripting. git branch --list prints many branches, and prepends the current one with an asterisk, meaning sed or other filtering is necessary to just get the current branch. git rev-parse --abbrev-ref HEAD is the current way to achieve this output, but that is not intuitive or easy to understand. Signed-off-by: Daniels Umanovskis --- builtin/branch.c | 17 +++++++++++++++++ t/t3203-branch-output.sh | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/builtin/branch.c b/builtin/branch.c index c396c4153..e4c6b0490 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -443,6 +443,18 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin free(to_free); } +static void print_current_branch_name() +{ + struct strbuf out = STRBUF_INIT; + const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); + char *shortname = shorten_unambiguous_ref(refname, 0); + strbuf_addf(&out, _("%s"), shortname); + fwrite(out.buf, 1, out.len, stdout); + putchar('\n'); + free(shortname); + strbuf_release(&out); +} + static void reject_rebase_or_bisect_branch(const char *target) { struct worktree **worktrees = get_worktrees(0); @@ -581,6 +593,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 current = 0; int reflog = 0, edit_description = 0; int quiet = 0, unset_upstream = 0; const char *new_upstream = NULL; @@ -620,6 +633,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) OPT_BIT('c', "copy", ©, N_("copy a branch and its reflog"), 1), OPT_BIT('C', NULL, ©, N_("copy a branch, even if target exists"), 2), OPT_BOOL('l', "list", &list, N_("list branch names")), + OPT_BOOL(0, "current", ¤t, N_("show current branch name")), OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")), OPT_BOOL(0, "edit-description", &edit_description, N_("edit the description for the branch")), @@ -697,6 +711,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (!argc) die(_("branch name required")); return delete_branches(argc, argv, delete > 1, filter.kind, quiet); + } else if (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) diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh index ee6787614..396d81568 100755 --- a/t/t3203-branch-output.sh +++ b/t/t3203-branch-output.sh @@ -100,6 +100,24 @@ test_expect_success 'git branch -v pattern does not show branch summaries' ' test_must_fail git branch -v branch* ' +test_expect_success 'git branch `--current` shows current branch' ' + cat >expect <<-\EOF && + branch-two + EOF + git checkout branch-two && + git branch --current >actual && + test_cmp expect actual +' + +test_expect_success 'git branch `--current` shows detached HEAD properly' ' + cat >expect <<-\EOF && + HEAD + EOF + git checkout HEAD^0 && + git branch --current >actual && + test_cmp expect actual +' + test_expect_success 'git branch shows detached HEAD properly' ' cat >expect <