Skip to content

Commit 25bd3ac

Browse files
peffgitster
authored andcommitted
diff: drop useless return from run_diff_{files,index} functions
Neither of these functions ever returns a value other than zero. Instead, they expect unrecoverable errors to exit immediately, and things like "--exit-code" are stored inside the diff_options struct to be handled later via diff_result_code(). Some callers do check the return values, but many don't bother. Let's drop the useless return values, which are misleading callers about how the functions work. This could be seen as a step in the wrong direction, as we might want to eventually "lib-ify" these to more cleanly return errors up the stack, in which case we'd have to add the return values back in. But there are some benefits to doing this now: 1. In the current code, somebody could accidentally add a "return -1" to one of the functions, which would be erroneously ignored by many callers. By removing the return code, the compiler can notice the mismatch and force the developer to decide what to do. Obviously the other option here is that we could start consistently checking the error code in every caller. But it would be dead code, and we wouldn't get any compile-time help in catching new cases. 2. It communicates the situation to callers, who may want to choose a different function. These functions are really thin wrappers for doing git-diff-files and git-diff-index within the process. But callers who care about recovering from an error here are probably better off using the underlying library functions, many of which do return errors. If somebody eventually wants to teach these functions to propagate errors, they'll have to switch back to returning a value, effectively reverting this patch. But at least then they will be starting with a level playing field: they know that they will need to inspect each caller to see how it should handle the error. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3755077 commit 25bd3ac

File tree

10 files changed

+27
-33
lines changed

10 files changed

+27
-33
lines changed

builtin/add.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
194194
out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
195195
rev.diffopt.file = xfdopen(out, "w");
196196
rev.diffopt.close_file = 1;
197-
if (run_diff_files(&rev, 0))
198-
die(_("Could not write patch"));
197+
run_diff_files(&rev, 0);
199198

200199
if (launch_editor(file, NULL, NULL))
201200
die(_("editing patch failed"));

builtin/describe.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
668668
struct lock_file index_lock = LOCK_INIT;
669669
struct rev_info revs;
670670
struct strvec args = STRVEC_INIT;
671-
int fd, result;
671+
int fd;
672672

673673
setup_work_tree();
674674
prepare_repo_settings(the_repository);
@@ -685,9 +685,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
685685
strvec_pushv(&args, diff_index_args);
686686
if (setup_revisions(args.nr, args.v, &revs, NULL) != 1)
687687
BUG("malformed internal diff-index command line");
688-
result = run_diff_index(&revs, 0);
688+
run_diff_index(&revs, 0);
689689

690-
if (!diff_result_code(&revs.diffopt, result))
690+
if (!diff_result_code(&revs.diffopt, 0))
691691
suffix = NULL;
692692
else
693693
suffix = dirty;

builtin/diff-files.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
8282

8383
if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0)
8484
die_errno("repo_read_index_preload");
85-
result = run_diff_files(&rev, options);
86-
result = diff_result_code(&rev.diffopt, result);
85+
run_diff_files(&rev, options);
86+
result = diff_result_code(&rev.diffopt, 0);
8787
release_revisions(&rev);
8888
return result;
8989
}

builtin/diff-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
7272
perror("repo_read_index");
7373
return -1;
7474
}
75-
result = run_diff_index(&rev, option);
76-
result = diff_result_code(&rev.diffopt, result);
75+
run_diff_index(&rev, option);
76+
result = diff_result_code(&rev.diffopt, 0);
7777
release_revisions(&rev);
7878
return result;
7979
}

builtin/diff.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ static int builtin_diff_index(struct rev_info *revs,
168168
} else if (repo_read_index(the_repository) < 0) {
169169
die_errno("repo_read_cache");
170170
}
171-
return run_diff_index(revs, option);
171+
run_diff_index(revs, option);
172+
return 0;
172173
}
173174

174175
static int builtin_diff_tree(struct rev_info *revs,
@@ -289,7 +290,8 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
289290
0) < 0) {
290291
die_errno("repo_read_index_preload");
291292
}
292-
return run_diff_files(revs, options);
293+
run_diff_files(revs, options);
294+
return 0;
293295
}
294296

295297
struct symdiff {

builtin/stash.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,6 @@ static int get_untracked_files(const struct pathspec *ps, int include_untracked,
10891089
*/
10901090
static int check_changes_tracked_files(const struct pathspec *ps)
10911091
{
1092-
int result;
10931092
struct rev_info rev;
10941093
struct object_id dummy;
10951094
int ret = 0;
@@ -1111,14 +1110,14 @@ static int check_changes_tracked_files(const struct pathspec *ps)
11111110
add_head_to_pending(&rev);
11121111
diff_setup_done(&rev.diffopt);
11131112

1114-
result = run_diff_index(&rev, DIFF_INDEX_CACHED);
1115-
if (diff_result_code(&rev.diffopt, result)) {
1113+
run_diff_index(&rev, DIFF_INDEX_CACHED);
1114+
if (diff_result_code(&rev.diffopt, 0)) {
11161115
ret = 1;
11171116
goto done;
11181117
}
11191118

1120-
result = run_diff_files(&rev, 0);
1121-
if (diff_result_code(&rev.diffopt, result)) {
1119+
run_diff_files(&rev, 0);
1120+
if (diff_result_code(&rev.diffopt, 0)) {
11221121
ret = 1;
11231122
goto done;
11241123
}
@@ -1309,10 +1308,7 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
13091308

13101309
add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
13111310
"");
1312-
if (run_diff_index(&rev, 0)) {
1313-
ret = -1;
1314-
goto done;
1315-
}
1311+
run_diff_index(&rev, 0);
13161312

13171313
cp_upd_index.git_cmd = 1;
13181314
strvec_pushl(&cp_upd_index.args, "update-index",

builtin/submodule--helper.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
629629
char *displaypath;
630630
struct strvec diff_files_args = STRVEC_INIT;
631631
struct rev_info rev = REV_INFO_INIT;
632-
int diff_files_result;
633632
struct strbuf buf = STRBUF_INIT;
634633
const char *git_dir;
635634
struct setup_revision_opt opt = {
@@ -669,9 +668,9 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
669668
repo_init_revisions(the_repository, &rev, NULL);
670669
rev.abbrev = 0;
671670
setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
672-
diff_files_result = run_diff_files(&rev, 0);
671+
run_diff_files(&rev, 0);
673672

674-
if (!diff_result_code(&rev.diffopt, diff_files_result)) {
673+
if (!diff_result_code(&rev.diffopt, 0)) {
675674
print_status(flags, ' ', path, ce_oid,
676675
displaypath);
677676
} else if (!(flags & OPT_CACHED)) {

diff-lib.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static int match_stat_with_submodule(struct diff_options *diffopt,
9696
return changed;
9797
}
9898

99-
int run_diff_files(struct rev_info *revs, unsigned int option)
99+
void run_diff_files(struct rev_info *revs, unsigned int option)
100100
{
101101
int entries, i;
102102
int diff_unmerged_stage = revs->max_count;
@@ -272,7 +272,6 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
272272
diffcore_std(&revs->diffopt);
273273
diff_flush(&revs->diffopt);
274274
trace_performance_since(start, "diff-files");
275-
return 0;
276275
}
277276

278277
/*
@@ -606,7 +605,7 @@ void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb)
606605
free_commit_list(merge_bases);
607606
}
608607

609-
int run_diff_index(struct rev_info *revs, unsigned int option)
608+
void run_diff_index(struct rev_info *revs, unsigned int option)
610609
{
611610
struct object_array_entry *ent;
612611
int cached = !!(option & DIFF_INDEX_CACHED);
@@ -640,7 +639,6 @@ int run_diff_index(struct rev_info *revs, unsigned int option)
640639
diffcore_std(&revs->diffopt);
641640
diff_flush(&revs->diffopt);
642641
trace_performance_leave("diff-index");
643-
return 0;
644642
}
645643

646644
int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)

diff.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,11 @@ void diff_get_merge_base(const struct rev_info *revs, struct object_id *mb);
637637
#define DIFF_SILENT_ON_REMOVED 01
638638
/* report racily-clean paths as modified */
639639
#define DIFF_RACY_IS_MODIFIED 02
640-
int run_diff_files(struct rev_info *revs, unsigned int option);
640+
void run_diff_files(struct rev_info *revs, unsigned int option);
641641

642642
#define DIFF_INDEX_CACHED 01
643643
#define DIFF_INDEX_MERGE_BASE 02
644-
int run_diff_index(struct rev_info *revs, unsigned int option);
644+
void run_diff_index(struct rev_info *revs, unsigned int option);
645645

646646
int do_diff_cache(const struct object_id *, struct diff_options *);
647647
int diff_flush_patch_id(struct diff_options *, struct object_id *, int);

wt-status.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,8 +2580,8 @@ int has_unstaged_changes(struct repository *r, int ignore_submodules)
25802580
}
25812581
rev_info.diffopt.flags.quick = 1;
25822582
diff_setup_done(&rev_info.diffopt);
2583-
result = run_diff_files(&rev_info, 0);
2584-
result = diff_result_code(&rev_info.diffopt, result);
2583+
run_diff_files(&rev_info, 0);
2584+
result = diff_result_code(&rev_info.diffopt, 0);
25852585
release_revisions(&rev_info);
25862586
return result;
25872587
}
@@ -2614,8 +2614,8 @@ int has_uncommitted_changes(struct repository *r,
26142614
}
26152615

26162616
diff_setup_done(&rev_info.diffopt);
2617-
result = run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2618-
result = diff_result_code(&rev_info.diffopt, result);
2617+
run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2618+
result = diff_result_code(&rev_info.diffopt, 0);
26192619
release_revisions(&rev_info);
26202620
return result;
26212621
}

0 commit comments

Comments
 (0)