From mboxrd@z Thu Jan 1 00:00:00 1970 From: Karthik Nayak Subject: [PATCH 9/9] branch: implement '--format' option Date: Fri, 2 Oct 2015 23:09:06 +0530 Message-ID: <1443807546-5985-10-git-send-email-Karthik.188@gmail.com> References: <1443807546-5985-1-git-send-email-Karthik.188@gmail.com> Cc: christian.couder@gmail.com, Matthieu.Moy@grenoble-inp.fr, gitster@pobox.com, Karthik Nayak , Karthik Nayak To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Fri Oct 02 19:39:57 2015 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zi4JA-0004CR-KW for gcvg-git-2@plane.gmane.org; Fri, 02 Oct 2015 19:39:48 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754092AbbJBRjm (ORCPT ); Fri, 2 Oct 2015 13:39:42 -0400 Received: from mail-pa0-f67.google.com ([209.85.220.67]:32813 "EHLO mail-pa0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752949AbbJBRjk (ORCPT ); Fri, 2 Oct 2015 13:39:40 -0400 Received: by padew5 with SMTP id ew5so12561543pad.0 for ; Fri, 02 Oct 2015 10:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=T/Uhp6odm2SiuvTZEJ37ZSzgeF41OgcSMJu7ehfAZAg=; b=Mf1KpTssusU0VCEUxadWVsWBfVzifnu1ucWWTNoFzztKsa5eSC27b+uIB7c/OQJEjX t26VmRBU6wlWCMDFY2bqKBQ1wgHm8yRXdPFn5ypXi/CtOIf63j1EcN+14DNyOKCgJMMG D9ymE/92sWI6kXWPj3wEywUnC9dwSqVr2oJvS2C/MONIqLwNKsgJBgTXAb8Bn0g0gRt2 wG43/oe2jK45Q4qljQmNEs9QY4aax1OSTHr8acjefZ95NJk55xXbYEDHQD09F3eReV5V oA8wIfhgnG0yUvv5flqaz4XpJvnNPXuWerRpN8j7LZk9MOC2Gcj2M3QA2wUg5mYmUDmt PEHQ== X-Received: by 10.66.254.101 with SMTP id ah5mr21609281pad.10.1443807580198; Fri, 02 Oct 2015 10:39:40 -0700 (PDT) Received: from ashley.localdomain ([106.51.130.23]) by smtp.gmail.com with ESMTPSA id pq1sm12993384pbb.91.2015.10.02.10.39.36 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 02 Oct 2015 10:39:39 -0700 (PDT) X-Google-Original-From: Karthik Nayak X-Mailer: git-send-email 2.6.0 In-Reply-To: <1443807546-5985-1-git-send-email-Karthik.188@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Implement the '--format' option provided by 'ref-filter'. This lets the user list tags as per desired format similar to the implementation in 'git for-each-ref'. Add tests and documentation for the same. Mentored-by: Christian Couder Mentored-by: Matthieu Moy Signed-off-by: Karthik Nayak --- Documentation/git-branch.txt | 7 ++++++- builtin/branch.c | 14 +++++++++----- t/t3203-branch-output.sh | 11 +++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt index 03c7af1..5227206 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -12,7 +12,7 @@ SYNOPSIS [--list] [-v [--abbrev= | --no-abbrev]] [--column[=] | --no-column] [(--merged | --no-merged | --contains) []] [--sort=] - [--points-at ] [...] + [--points-at ] [--format=] [...] 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] [] 'git branch' (--set-upstream-to= | -u ) [] 'git branch' --unset-upstream [] @@ -244,6 +244,11 @@ start-point is either a local or remote-tracking branch. --points-at :: Only list branches of the given object. +--format :: + A string that interpolates `%(fieldname)` from the object + pointed at by a ref being shown. The format is the same as + that of linkgit:git-for-each-ref[1]. + Examples -------- diff --git a/builtin/branch.c b/builtin/branch.c index 48fbca1..ae3ecfb 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -27,6 +27,7 @@ static const char * const builtin_branch_usage[] = { N_("git branch [] [-r] (-d | -D) ..."), N_("git branch [] (-m | -M) [] "), N_("git branch [] [-r | -a] [--points-at]"), + N_("git branch [] [-r | -a] [--format]"), NULL }; @@ -331,14 +332,14 @@ static char *get_format(struct ref_filter *filter, int maxwidth, const char *rem return final; } -static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting) +static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, const char *format) { int i; struct ref_array array; int maxwidth = 0; const char *remote_prefix = ""; struct strbuf out = STRBUF_INIT; - char *format; + char *to_free = NULL; /* * If we are listing more than just remote branches, @@ -355,7 +356,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin if (filter->verbose) maxwidth = calc_maxwidth(&array, strlen(remote_prefix)); - format = get_format(filter, maxwidth, remote_prefix); + if (!format) + format = to_free = get_format(filter, maxwidth, remote_prefix); verify_ref_format(format); /* @@ -382,7 +384,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin } ref_array_clear(&array); - free(format); + free(to_free); } static void rename_branch(const char *oldname, const char *newname, int force) @@ -483,6 +485,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) enum branch_track track; struct ref_filter filter; static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting; + const char *format = NULL; struct option options[] = { OPT_GROUP(N_("Generic options")), @@ -523,6 +526,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"), N_("print only branches of the object"), 0, parse_opt_object_name }, + OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")), OPT_END(), }; @@ -581,7 +585,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached) filter.kind |= FILTER_REFS_DETACHED_HEAD; filter.name_patterns = argv; - print_ref_list(&filter, sorting); + print_ref_list(&filter, sorting, format); print_columns(&output, colopts, NULL); string_list_clear(&output, 0); return 0; diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh index f1ae5ff..a475ff1 100755 --- a/t/t3203-branch-output.sh +++ b/t/t3203-branch-output.sh @@ -163,4 +163,15 @@ test_expect_success 'git branch --points-at option' ' test_cmp expect actual ' +test_expect_success 'git branch --format option' ' + cat >expect <<-\EOF && + Refname is (HEAD detached from fromtag) + Refname is refs/heads/branch-one + Refname is refs/heads/branch-two + Refname is refs/heads/master + EOF + git branch --format="Refname is %(refname)" >actual && + test_cmp expect actual +' + test_done -- 2.6.0