git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v2 1/6] builtin/am: introduce write_state_*() helper functions
Date: Mon, 24 Aug 2015 13:58:05 -0700	[thread overview]
Message-ID: <1440449890-29490-2-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1440449890-29490-1-git-send-email-gitster@pobox.com>

There are many calls to write_file() that repeat the same pattern in
the implementation of the builtin version of "am".  They all share
the same traits, i.e they

 - produce a text file with a single string in it;

 - have enough information to produce the entire contents of that
   file;

 - generate the pathname of the file by making a call to am_path(); and

 - they ask write_file() to die() upon failure.

The slight differences among the call sites throw them into roughly
three categories:

 - many write either "t" or "f" based on a boolean value to a file;

 - some write the integer value in decimal text;

 - some others write more general string, e.g. an object name in
   hex, an empty string (i.e. the presense of the file itself serves
   as a flag), etc.

Introduce three helpers, write_state_bool(), write_state_count() and
write_state_text(), to reduce direct calls to write_file().

This is a preparatory step for the next step to ensure that no
"state" file this command leaves in $GIT_DIR is with an incomplete
line at the end.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/am.c | 68 ++++++++++++++++++++++++++++++++++++------------------------
 1 file changed, 41 insertions(+), 27 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 634f7a7..4d34dc5 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -194,6 +194,27 @@ static inline const char *am_path(const struct am_state *state, const char *path
 }
 
 /**
+ * For convenience to call write_file()
+ */
+static int write_state_text(const struct am_state *state,
+			    const char *name, const char *string)
+{
+	return write_file(am_path(state, name), 1, "%s", string);
+}
+
+static int write_state_count(const struct am_state *state,
+			     const char *name, int value)
+{
+	return write_file(am_path(state, name), 1, "%d", value);
+}
+
+static int write_state_bool(const struct am_state *state,
+			    const char *name, int value)
+{
+	return write_state_text(state, name, value ? "t" : "f");
+}
+
+/**
  * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
  * at the end.
  */
@@ -362,7 +383,7 @@ static void write_author_script(const struct am_state *state)
 	sq_quote_buf(&sb, state->author_date);
 	strbuf_addch(&sb, '\n');
 
-	write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
+	write_state_text(state, "author-script", sb.buf);
 
 	strbuf_release(&sb);
 }
@@ -1000,13 +1021,10 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
 	if (state->rebasing)
 		state->threeway = 1;
 
-	write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
-
-	write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
-
-	write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
-
-	write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
+	write_state_bool(state, "threeway", state->threeway);
+	write_state_bool(state, "quiet", state->quiet);
+	write_state_bool(state, "sign", state->signoff);
+	write_state_bool(state, "utf8", state->utf8);
 
 	switch (state->keep) {
 	case KEEP_FALSE:
@@ -1022,9 +1040,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
 		die("BUG: invalid value for state->keep");
 	}
 
-	write_file(am_path(state, "keep"), 1, "%s", str);
-
-	write_file(am_path(state, "messageid"), 1, state->message_id ? "t" : "f");
+	write_state_text(state, "keep", str);
+	write_state_bool(state, "messageid", state->message_id);
 
 	switch (state->scissors) {
 	case SCISSORS_UNSET:
@@ -1039,24 +1056,23 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
 	default:
 		die("BUG: invalid value for state->scissors");
 	}
-
-	write_file(am_path(state, "scissors"), 1, "%s", str);
+	write_state_text(state, "scissors", str);
 
 	sq_quote_argv(&sb, state->git_apply_opts.argv, 0);
-	write_file(am_path(state, "apply-opt"), 1, "%s", sb.buf);
+	write_state_text(state, "apply-opt", sb.buf);
 
 	if (state->rebasing)
-		write_file(am_path(state, "rebasing"), 1, "%s", "");
+		write_state_text(state, "rebasing", "");
 	else
-		write_file(am_path(state, "applying"), 1, "%s", "");
+		write_state_text(state, "applying", "");
 
 	if (!get_sha1("HEAD", curr_head)) {
-		write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
+		write_state_text(state, "abort-safety", sha1_to_hex(curr_head));
 		if (!state->rebasing)
 			update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
 					UPDATE_REFS_DIE_ON_ERR);
 	} else {
-		write_file(am_path(state, "abort-safety"), 1, "%s", "");
+		write_state_text(state, "abort-safety", "");
 		if (!state->rebasing)
 			delete_ref("ORIG_HEAD", NULL, 0);
 	}
@@ -1066,9 +1082,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
 	 * session is in progress, they should be written last.
 	 */
 
-	write_file(am_path(state, "next"), 1, "%d", state->cur);
-
-	write_file(am_path(state, "last"), 1, "%d", state->last);
+	write_state_count(state, "next", state->cur);
+	write_state_count(state, "last", state->last);
 
 	strbuf_release(&sb);
 }
@@ -1101,12 +1116,12 @@ static void am_next(struct am_state *state)
 	unlink(am_path(state, "original-commit"));
 
 	if (!get_sha1("HEAD", head))
-		write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
+		write_state_text(state, "abort-safety", sha1_to_hex(head));
 	else
-		write_file(am_path(state, "abort-safety"), 1, "%s", "");
+		write_state_text(state, "abort-safety", "");
 
 	state->cur++;
-	write_file(am_path(state, "next"), 1, "%d", state->cur);
+	write_state_count(state, "next", state->cur);
 }
 
 /**
@@ -1479,8 +1494,7 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
 	write_commit_patch(state, commit);
 
 	hashcpy(state->orig_commit, commit_sha1);
-	write_file(am_path(state, "original-commit"), 1, "%s",
-			sha1_to_hex(commit_sha1));
+	write_state_text(state, "original-commit", sha1_to_hex(commit_sha1));
 
 	return 0;
 }
@@ -1782,7 +1796,7 @@ static void am_run(struct am_state *state, int resume)
 	refresh_and_write_cache();
 
 	if (index_has_changes(&sb)) {
-		write_file(am_path(state, "dirtyindex"), 1, "t");
+		write_state_bool(state, "dirtyindex", 1);
 		die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
 	}
 
-- 
2.5.0-568-g53a3e28

  reply	other threads:[~2015-08-24 20:58 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-20 13:22 Minor builtin 'git am' side-effect SZEDER Gábor
2015-08-20 18:40 ` Junio C Hamano
2015-08-23  5:50   ` [PATCH] am: terminate state files with a newline Paul Tan
2015-08-23 12:30     ` SZEDER Gábor
2015-08-23 19:05     ` Junio C Hamano
2015-08-24  5:13       ` Jeff King
2015-08-24  6:48         ` Junio C Hamano
2015-08-24  6:50           ` Jeff King
2015-08-24 17:09             ` [PATCH 0/5] "am" state file fix with write_file() clean-up Junio C Hamano
2015-08-24 17:09               ` [PATCH 1/5] builtin/am: introduce write_state_*() helper functions Junio C Hamano
2015-08-24 17:09               ` [PATCH 2/5] builtin/am: make sure state files are text Junio C Hamano
2015-08-24 17:09               ` [PATCH 3/5] write_file(): introduce an explicit WRITE_FILE_GENTLY request Junio C Hamano
2015-08-24 18:41                 ` Junio C Hamano
2015-08-25 10:08                   ` Duy Nguyen
2015-08-25 10:30                     ` [PATCH] setup: update the right file in multiple checkouts Nguyễn Thái Ngọc Duy
2015-08-25 16:38                       ` Junio C Hamano
2015-08-31 10:29                         ` Duy Nguyen
2015-08-24 17:09               ` [PATCH 4/5] write_file(): do not leave incomplete line at the end Junio C Hamano
2015-08-24 17:09               ` [PATCH 5/5] write_file(): clean up transitional mess of flag words and terminating LF Junio C Hamano
2015-08-24 17:41               ` [PATCH 0/5] "am" state file fix with write_file() clean-up Jeff King
2015-08-24 18:15                 ` Junio C Hamano
2015-08-24 18:35                   ` Jeff King
2015-08-24 19:57                     ` Junio C Hamano
2015-08-24 20:58                       ` [PATCH v2 0/6] " Junio C Hamano
2015-08-24 20:58                         ` Junio C Hamano [this message]
2015-08-24 20:58                         ` [PATCH v2 2/6] builtin/am: make sure state files are text Junio C Hamano
2015-08-24 23:55                           ` Jeff King
2015-08-25 16:19                             ` Junio C Hamano
2015-08-25 16:47                               ` Jeff King
2015-08-25 18:41                                 ` Junio C Hamano
2015-08-24 20:58                         ` [PATCH v2 3/6] write_file(): drop "fatal" parameter Junio C Hamano
2015-08-24 20:58                         ` [PATCH v2 4/6] write_file_v(): do not leave incomplete line at the end Junio C Hamano
2015-08-24 20:58                         ` [PATCH v2 5/6] write_file(): drop caller-supplied LF from calls to create a one-liner file Junio C Hamano
2015-08-24 20:58                         ` [PATCH v2 6/6] write_file(): drop caller-supplied LF from multi-line file Junio C Hamano
2015-08-25  0:02                         ` [PATCH v2 0/6] "am" state file fix with write_file() clean-up Jeff King
2015-08-24 23:36     ` [PATCH] am: terminate state files with a newline brian m. carlson

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=1440449890-29490-2-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /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).