From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH/WIP 1/8] strbuf: add and use strbuf_read_file_or_die()
Date: Wed, 18 Mar 2015 16:55:40 +0700 [thread overview]
Message-ID: <1426672547-11369-2-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1426672547-11369-1-git-send-email-pclouds@gmail.com>
---
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
next prev parent reply other threads:[~2015-03-18 9:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-18 9:55 [PATCH/WIP 0/8] Convert git-rebase.sh to C Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` Nguyễn Thái Ngọc Duy [this message]
2015-03-18 9:55 ` [PATCH/WIP 2/8] Move reset_tree from builtin/checkout.c to unpack-trees.c Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` [PATCH/WIP 3/8] rebase: turn rebase--am into a separate program Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` [PATCH/WIP 4/8] rebase: turn rebase--merge " Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` [PATCH/WIP 5/8] rebase: turn rebase--interactive " Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` [PATCH/WIP 6/8] rebase: remove unused function Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` [PATCH/WIP 7/8] rebase: move resolvemsg to rebase--* scripts Nguyễn Thái Ngọc Duy
2015-03-18 9:55 ` [PATCH/WIP 8/8] Build in rebase Nguyễn Thái Ngọc Duy
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=1426672547-11369-2-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.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).