Subject: [PATCH/WIP 1/8] strbuf: add and use strbuf_read_file_or_die()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 18 Mar 2015 16:55:40 +0700

---
 builtin/blame.c  |  4 ++--
 builtin/commit.c | 16 +++++-----------
 builtin/merge.c  |  3 +--
 builtin/notes.c  |  4 ++--
 builtin/tag.c    |  7 ++-----
 strbuf.c         |  8 ++++++++
 strbuf.h         |  1 +
 7 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/builtin/blame.c b/builtin/blame.c
index bc6c899..503595c 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2193,8 +2193,8 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
 			if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&
 			    textconv_object(read_from, mode, null_sha1, 0, &buf_ptr, &buf_len))
 				strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1);
-			else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size)
-				die_errno("cannot open or read '%s'", read_from);
+			else
+				strbuf_read_file_or_die(&buf, read_from, st.st_size);
 			break;
 		case S_IFLNK:
 			if (strbuf_readlink(&buf, read_from, st.st_size) < 0)
diff --git a/builtin/commit.c b/builtin/commit.c
index d6dd3df..dad9acf 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -612,9 +612,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 			die_errno(_("could not read log from standard input"));
 		hook_arg1 = "message";
 	} else if (logfile) {
-		if (strbuf_read_file(&sb, logfile, 0) < 0)
-			die_errno(_("could not read log file '%s'"),
-				  logfile);
+		strbuf_read_file_or_die(&sb, logfile, 0);
 		hook_arg1 = "message";
 	} else if (use_message) {
 		buffer = strstr(use_message_buffer, "\n\n");
@@ -634,16 +632,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 				      &sb, &ctx);
 		hook_arg1 = "message";
 	} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
-			die_errno(_("could not read MERGE_MSG"));
+		strbuf_read_file_or_die(&sb, git_path("MERGE_MSG"), 0);
 		hook_arg1 = "merge";
 	} else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
-		if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
-			die_errno(_("could not read SQUASH_MSG"));
+		strbuf_read_file_or_die(&sb, git_path("SQUASH_MSG"), 0);
 		hook_arg1 = "squash";
 	} else if (template_file) {
-		if (strbuf_read_file(&sb, template_file, 0) < 0)
-			die_errno(_("could not read '%s'"), template_file);
+		strbuf_read_file_or_die(&sb, template_file, 0);
 		hook_arg1 = "template";
 		clean_message_contents = 0;
 	}
@@ -1497,8 +1492,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		fclose(fp);
 		strbuf_release(&m);
 		if (!stat(git_path("MERGE_MODE"), &statbuf)) {
-			if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
-				die_errno(_("could not read MERGE_MODE"));
+			strbuf_read_file_or_die(&sb, git_path("MERGE_MODE"), 0);
 			if (!strcmp(sb.buf, "no-ff"))
 				allow_fast_forward = 0;
 		}
diff --git a/builtin/merge.c b/builtin/merge.c
index 9307e9c..6babf39 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -769,8 +769,7 @@ static void read_merge_msg(struct strbuf *msg)
 {
 	const char *filename = git_path("MERGE_MSG");
 	strbuf_reset(msg);
-	if (strbuf_read_file(msg, filename, 0) < 0)
-		die_errno(_("Could not read from '%s'"), filename);
+	strbuf_read_file_or_die(msg, filename, 0);
 }
 
 static void write_merge_state(struct commit_list *);
diff --git a/builtin/notes.c b/builtin/notes.c
index 453457a..3210c7f 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -252,8 +252,8 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
 	if (!strcmp(arg, "-")) {
 		if (strbuf_read(&(msg->buf), 0, 1024) < 0)
 			die_errno(_("cannot read '%s'"), arg);
-	} else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
-		die_errno(_("could not open or read '%s'"), arg);
+	} else
+		strbuf_read_file_or_die(&(msg->buf), arg, 0);
 	stripspace(&(msg->buf), 0);
 
 	msg->given = 1;
diff --git a/builtin/tag.c b/builtin/tag.c
index 9c3e067..69f4ea3 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -540,11 +540,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			if (!strcmp(msgfile, "-")) {
 				if (strbuf_read(&buf, 0, 1024) < 0)
 					die_errno(_("cannot read '%s'"), msgfile);
-			} else {
-				if (strbuf_read_file(&buf, msgfile, 1024) < 0)
-					die_errno(_("could not open or read '%s'"),
-						msgfile);
-			}
+			} else
+				strbuf_read_file_or_die(&buf, msgfile, 0);
 		}
 	}
 
diff --git a/strbuf.c b/strbuf.c
index 9a373be..9f50478 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -411,6 +411,14 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 	return len;
 }
 
+void strbuf_read_file_or_die(struct strbuf *sb, const char *path, size_t size)
+{
+	int ret;
+	ret = strbuf_read_file(sb, path, size);
+	if (ret < 0 || (size && ret != size))
+		die_errno(_("could not open or read '%s'"), path);
+}
+
 void strbuf_add_lines(struct strbuf *out, const char *prefix,
 		      const char *buf, size_t size)
 {
diff --git a/strbuf.h b/strbuf.h
index ecae4e2..c1f012d 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -152,6 +152,7 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 /* XXX: if read fails, any partial read is undone */
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
 extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
+extern void strbuf_read_file_or_die(struct strbuf *sb, const char *path, size_t size);
 extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
 
 extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
-- 
2.3.0.rc1.137.g477eb31