git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Gregory David" <gregory.david@p1sec.com>,
	ptm-dev <ptm-dev@p1sec.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH v3 1/2] show-branch: refactor in preparation for next commit
Date: Thu, 21 Apr 2022 17:33:47 +0200	[thread overview]
Message-ID: <patch-v3-1.2-879930b7a66-20220421T152704Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-v3-0.2-00000000000-20220421T152704Z-avarab@gmail.com>

Move the code in cmd_show_branch() that formats a reflog message for
us into a function, and change the "flags" variable that we never
change into a "const", in addition to moving it up a scope in
preparation for the subsequent commit.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/show-branch.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 330b0553b9d..499ef76a508 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -618,6 +618,24 @@ static int parse_reflog_param(const struct option *opt, const char *arg,
 	return 0;
 }
 
+static char *fmt_reflog(char *const logmsg, const timestamp_t ts, const int tz,
+			const char *fmt)
+{
+	char *const end = strchr(logmsg, '\n');
+	const char *msg;
+	char *ret;
+
+	if (end)
+		*end = '\0';
+
+	msg = *logmsg ? logmsg : "(none)";
+
+	ret = xstrfmt(fmt, show_date(ts, tz, DATE_MODE(RELATIVE)), msg);
+	free(logmsg);
+
+	return ret;
+}
+
 int cmd_show_branch(int ac, const char **av, const char *prefix)
 {
 	struct commit *rev[MAX_REVS], *commit;
@@ -640,6 +658,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 	int topics = 0;
 	int dense = 1;
 	const char *reflog_base = NULL;
+	const unsigned int flags = 0;
 	struct option builtin_show_branch_options[] = {
 		OPT_BOOL('a', "all", &all_heads,
 			 N_("show remote-tracking and local branches")),
@@ -720,7 +739,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 		struct object_id oid;
 		char *ref;
 		int base = 0;
-		unsigned int flags = 0;
 
 		if (ac == 0) {
 			static const char *fake_av[2];
@@ -761,8 +779,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 		for (i = 0; i < reflog; i++) {
 			char *logmsg;
 			char *nth_desc;
-			const char *msg;
-			char *end;
 			timestamp_t timestamp;
 			int tz;
 
@@ -773,16 +789,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 				break;
 			}
 
-			end = strchr(logmsg, '\n');
-			if (end)
-				*end = '\0';
-
-			msg = (*logmsg == '\0') ? "(none)" : logmsg;
-			reflog_msg[i] = xstrfmt("(%s) %s",
-						show_date(timestamp, tz,
-							  DATE_MODE(RELATIVE)),
-						msg);
-			free(logmsg);
+			reflog_msg[i] = fmt_reflog(logmsg, timestamp, tz,
+						   "(%s) %s");
 
 			nth_desc = xstrfmt("%s@{%d}", *av, base+i);
 			append_ref(nth_desc, &oid, 1);
-- 
2.36.0.876.g7efc8a7728c


  reply	other threads:[~2022-04-21 15:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21 13:34 [PATCH] show-branch: fix SEGFAULT when `--current` and `--reflog` together Gregory David
2022-04-21 15:33 ` [PATCH v3 0/2] show-brach: segfault fix from Gregory David Ævar Arnfjörð Bjarmason
2022-04-21 15:33   ` Ævar Arnfjörð Bjarmason [this message]
2022-04-21 18:13     ` [PATCH v3 1/2] show-branch: refactor in preparation for next commit Junio C Hamano
2022-04-21 15:33   ` [PATCH v3 2/2] show-branch: fix SEGFAULT when `--current` and `--reflog` together Ævar Arnfjörð Bjarmason
2022-04-21 18:36     ` Junio C Hamano
2022-04-21 21:25       ` [PATCH] show-branch: -g and --current are incompatible Junio C Hamano

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

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

  git send-email \
    --in-reply-to=patch-v3-1.2-879930b7a66-20220421T152704Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=gregory.david@p1sec.com \
    --cc=ptm-dev@p1sec.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.
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 NNTP newsgroup(s).