Skip to content

Commit 75f3ff2

Browse files
sbeyergitster
authored andcommitted
Generalize and libify index_is_dirty() to index_differs_from(...)
index_is_dirty() in builtin-revert.c checks if the index is dirty. This patch generalizes this function to check if the index differs from a revision, i.e. the former index_is_dirty() behavior can now be achieved by index_differs_from("HEAD", 0). The second argument "diff_flags" allows to set further diff option flags like DIFF_OPT_IGNORE_SUBMODULES. See DIFF_OPT_* macros in diff.h for a list. index_differs_from() seems to be useful for more than builtin-revert.c, so it is moved into diff-lib.c and also used in builtin-commit.c. Yet to mention: - "rev.abbrev = 0;" can be safely removed. This has no impact on performance or functioning of neither setup_revisions() nor run_diff_index(). - rev.pending.objects is free()d because this fixes a leak. (Also see 295dd2a "Fix memory leak in traverse_commit_list") Mentored-by: Daniel Barkalow <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Stephan Beyer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fcb6c07 commit 75f3ff2

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

builtin-commit.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
561561
commitable = run_status(fp, index_file, prefix, 1);
562562
wt_status_use_color = saved_color_setting;
563563
} else {
564-
struct rev_info rev;
565564
unsigned char sha1[20];
566565
const char *parent = "HEAD";
567566

@@ -573,16 +572,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
573572

574573
if (get_sha1(parent, sha1))
575574
commitable = !!active_nr;
576-
else {
577-
init_revisions(&rev, "");
578-
rev.abbrev = 0;
579-
setup_revisions(0, NULL, &rev, parent);
580-
DIFF_OPT_SET(&rev.diffopt, QUIET);
581-
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
582-
run_diff_index(&rev, 1 /* cached */);
583-
584-
commitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
585-
}
575+
else
576+
commitable = index_differs_from(parent, 0);
586577
}
587578

588579
fclose(fp);

builtin-revert.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,6 @@ static char *help_msg(const unsigned char *sha1)
223223
return helpbuf;
224224
}
225225

226-
static int index_is_dirty(void)
227-
{
228-
struct rev_info rev;
229-
init_revisions(&rev, NULL);
230-
setup_revisions(0, NULL, &rev, "HEAD");
231-
DIFF_OPT_SET(&rev.diffopt, QUIET);
232-
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
233-
run_diff_index(&rev, 1);
234-
return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
235-
}
236-
237226
static struct tree *empty_tree(void)
238227
{
239228
struct tree *tree = xcalloc(1, sizeof(struct tree));
@@ -279,7 +268,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
279268
} else {
280269
if (get_sha1("HEAD", head))
281270
die ("You do not have a valid HEAD");
282-
if (index_is_dirty())
271+
if (index_differs_from("HEAD", 0))
283272
die ("Dirty index: cannot %s", me);
284273
}
285274
discard_cache();

diff-lib.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,18 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
513513
exit(128);
514514
return 0;
515515
}
516+
517+
int index_differs_from(const char *def, int diff_flags)
518+
{
519+
struct rev_info rev;
520+
521+
init_revisions(&rev, NULL);
522+
setup_revisions(0, NULL, &rev, def);
523+
DIFF_OPT_SET(&rev.diffopt, QUIET);
524+
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
525+
rev.diffopt.flags |= diff_flags;
526+
run_diff_index(&rev, 1);
527+
if (rev.pending.alloc)
528+
free(rev.pending.objects);
529+
return (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);
530+
}

diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,6 @@ extern int diff_result_code(struct diff_options *, int);
265265

266266
extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
267267

268+
extern int index_differs_from(const char *def, int diff_flags);
269+
268270
#endif /* DIFF_H */

0 commit comments

Comments
 (0)