use xfopen() in more places

xfopen() - provides error details - explains error on reading, or writing, or whatever operation - has l10n support - prints file name in the error Some of these are missing in the places that are replaced with xfopen(), which is a clear win. In some other places, it's just less code (not as clearly a win as the previous case but still is). The only slight regresssion is in remote-testsvn, where we don't report the file class (marks files) in the error messages anymore. But since this is a _test_ svn remote transport, I'm not too concerned. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 3, 2017 at 17:16 UTC 23a9e0712d76a63c5af1caeb816943f466f0300e
10 files changed +13 -39
bisect.c
+1 -4
@@ -438,10 +438,7 @@ static void read_bisect_paths(struct argv_array *array)
438 {
439 struct strbuf str = STRBUF_INIT;
440 const char *filename = git_path_bisect_names();
441 - FILE *fp = fopen(filename, "r");
442 -
443 - if (!fp)
444 - die_errno(_("Could not open file '%s'"), filename);
441 + FILE *fp = xfopen(filename, "r");
442
443 while (strbuf_getline_lf(&str, fp) != EOF) {
444 strbuf_trim(&str);
builtin/am.c
+2 -6
@@ -1275,12 +1275,8 @@ static int parse_mail(struct am_state *state, const char *mail)
1275 die("BUG: invalid value for state->scissors");
1276 }
1277
1278 - mi.input = fopen(mail, "r");
1279 - if (!mi.input)
1280 - die("could not open input");
1281 - mi.output = fopen(am_path(state, "info"), "w");
1282 - if (!mi.output)
1283 - die("could not open output 'info'");
1278 + mi.input = xfopen(mail, "r");
1279 + mi.output = xfopen(am_path(state, "info"), "w");
1280 if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch")))
1281 die("could not parse patch");
1282
builtin/commit.c
+1 -4
@@ -1695,10 +1695,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1695 if (!reflog_msg)
1696 reflog_msg = "commit (merge)";
1697 pptr = commit_list_append(current_head, pptr);
1698 - fp = fopen(git_path_merge_head(), "r");
1699 - if (fp == NULL)
1700 - die_errno(_("could not open '%s' for reading"),
1701 - git_path_merge_head());
1698 + fp = xfopen(git_path_merge_head(), "r");
1699 while (strbuf_getline_lf(&m, fp) != EOF) {
1700 struct commit *parent;
1701
builtin/fast-export.c
+1 -3
@@ -905,9 +905,7 @@ static void export_marks(char *file)
905 static void import_marks(char *input_file)
906 {
907 char line[512];
908 - FILE *f = fopen(input_file, "r");
909 - if (!f)
910 - die_errno("cannot read '%s'", input_file);
908 + FILE *f = xfopen(input_file, "r");
909
910 while (fgets(line, sizeof(line), f)) {
911 uint32_t mark;
builtin/fsck.c
+1 -2
@@ -280,8 +280,7 @@ static void check_unreachable_object(struct object *obj)
280 free(filename);
281 return;
282 }
283 - if (!(f = fopen(filename, "w")))
284 - die_errno("Could not open '%s'", filename);
283 + f = xfopen(filename, "w");
284 if (obj->type == OBJ_BLOB) {
285 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
286 die_errno("Could not write '%s'", filename);
builtin/merge.c
+1 -3
@@ -839,9 +839,7 @@ static int suggest_conflicts(void)
839 struct strbuf msgbuf = STRBUF_INIT;
840
841 filename = git_path_merge_msg();
842 - fp = fopen(filename, "a");
843 - if (!fp)
844 - die_errno(_("Could not open '%s' for writing"), filename);
842 + fp = xfopen(filename, "a");
843
844 append_conflicts_hint(&msgbuf);
845 fputs(msgbuf.buf, fp);
builtin/pull.c
+1 -2
@@ -337,8 +337,7 @@ static void get_merge_heads(struct oid_array *merge_heads)
337 struct strbuf sb = STRBUF_INIT;
338 struct object_id oid;
339
340 - if (!(fp = fopen(filename, "r")))
341 - die_errno(_("could not open '%s' for reading"), filename);
340 + fp = xfopen(filename, "r");
341 while (strbuf_getline_lf(&sb, fp) != EOF) {
342 if (get_oid_hex(sb.buf, &oid))
343 continue; /* invalid line: does not start with SHA1 */
diff.c
+2 -6
@@ -4071,9 +4071,7 @@ int diff_opt_parse(struct diff_options *options,
4071 DIFF_OPT_CLR(options, FUNCCONTEXT);
4072 else if ((argcount = parse_long_opt("output", av, &optarg))) {
4073 char *path = prefix_filename(prefix, optarg);
4074 - options->file = fopen(path, "w");
4075 - if (!options->file)
4076 - die_errno("Could not open '%s'", path);
4074 + options->file = xfopen(path, "w");
4075 options->close_file = 1;
4076 if (options->use_color != GIT_COLOR_ALWAYS)
4077 options->use_color = GIT_COLOR_NEVER;
@@ -4807,9 +4805,7 @@ void diff_flush(struct diff_options *options)
4805 */
4806 if (options->close_file)
4807 fclose(options->file);
4810 - options->file = fopen("/dev/null", "w");
4811 - if (!options->file)
4812 - die_errno("Could not open /dev/null");
4808 + options->file = xfopen("/dev/null", "w");
4809 options->close_file = 1;
4810 for (i = 0; i < q->nr; i++) {
4811 struct diff_filepair *p = q->queue[i];
fast-import.c
+1 -3
@@ -3274,9 +3274,7 @@ static void option_export_pack_edges(const char *edges)
3274 {
3275 if (pack_edges)
3276 fclose(pack_edges);
3277 - pack_edges = fopen(edges, "a");
3278 - if (!pack_edges)
3279 - die_errno("Cannot open '%s'", edges);
3277 + pack_edges = xfopen(edges, "a");
3278 }
3279
3280 static int parse_one_option(const char *option)
remote-testsvn.c
+2 -6
@@ -124,10 +124,8 @@ static int note2mark_cb(const unsigned char *object_sha1,
124 static void regenerate_marks(void)
125 {
126 int ret;
127 - FILE *marksfile = fopen(marksfilename, "w+");
127 + FILE *marksfile = xfopen(marksfilename, "w+");
128
129 - if (!marksfile)
130 - die_errno("Couldn't create mark file %s.", marksfilename);
129 ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
130 if (ret)
131 die("Regeneration of marks failed, returned %d.", ret);
@@ -148,9 +146,7 @@ static void check_or_regenerate_marks(int latestrev)
146 marksfile = fopen(marksfilename, "r");
147 if (!marksfile) {
148 regenerate_marks();
151 - marksfile = fopen(marksfilename, "r");
152 - if (!marksfile)
153 - die_errno("cannot read marks file %s!", marksfilename);
149 + marksfile = xfopen(marksfilename, "r");
150 fclose(marksfile);
151 } else {
152 strbuf_addf(&sb, ":%d ", latestrev);