@matheusbsbs / matheus-bagliano / commits / 75f3ff2eea
Fork 0

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 295dd2ad "Fix memory leak in traverse_commit_list") Mentored-by: Daniel Barkalow <barkalow@iabervon.org> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Stephan Beyer <s-beyer@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephan Beyer committed Feb 10, 2009 at 15:30 UTC 75f3ff2eeaba820b37016f464b6d1078cb6260e2
4 files changed +20 -23
builtin-commit.c
+2 -11
@@ -561,7 +561,6 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
561 commitable = run_status(fp, index_file, prefix, 1);
562 wt_status_use_color = saved_color_setting;
563 } else {
564 - struct rev_info rev;
564 unsigned char sha1[20];
565 const char *parent = "HEAD";
566
@@ -573,16 +572,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
572
573 if (get_sha1(parent, sha1))
574 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);
577 }
578
579 fclose(fp);
builtin-revert.c
+1 -12
@@ -223,17 +223,6 @@ static char *help_msg(const unsigned char *sha1)
223 return helpbuf;
224 }
225
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 -
226 static struct tree *empty_tree(void)
227 {
228 struct tree *tree = xcalloc(1, sizeof(struct tree));
@@ -279,7 +268,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
268 } else {
269 if (get_sha1("HEAD", head))
270 die ("You do not have a valid HEAD");
282 - if (index_is_dirty())
271 + if (index_differs_from("HEAD", 0))
272 die ("Dirty index: cannot %s", me);
273 }
274 discard_cache();
diff-lib.c
+15
@@ -513,3 +513,18 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
513 exit(128);
514 return 0;
515 }
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
+2
@@ -265,4 +265,6 @@ extern int diff_result_code(struct diff_options *, int);
265
266 extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
267
268 +extern int index_differs_from(const char *def, int diff_flags);
269 +
270 #endif /* DIFF_H */