describe: refresh the index when 'broken' flag is used

When describe is run with 'dirty' flag, we refresh the index to make sure it is in sync with the filesystem before determining if the working tree is dirty. However, this is not done for the codepath where the 'broken' flag is used. This causes `git describe --broken --dirty` to false positively report the worktree being dirty if a file has different stat info than what is recorded in the index. Running `git update-index -q --refresh` to refresh the index before running diff-index fixes the problem. Also add tests to deliberately update stat info of a file before running describe to verify it behaves correctly. Reported-by: Paul Millar <paul.millar@desy.de> Suggested-by: Junio C Hamano <gitster@pobox.com> Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Abhijeet Sonar <abhijeet.nkt@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Abhijeet Sonar committed Jun 27, 2024 at 00:38 UTC b8ae42e292c51348b8d8730225396ac9ef72f550
2 files changed +48
builtin/describe.c
+12
@@ -54,6 +54,10 @@ static const char *diff_index_args[] = {
54 "diff-index", "--quiet", "HEAD", "--", NULL
55 };
56
57 +static const char *update_index_args[] = {
58 + "update-index", "--unmerged", "-q", "--refresh", NULL
59 +};
60 +
61 struct commit_name {
62 struct hashmap_entry entry;
63 struct object_id peeled;
@@ -645,6 +649,14 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
649 if (argc == 0) {
650 if (broken) {
651 struct child_process cp = CHILD_PROCESS_INIT;
652 +
653 + strvec_pushv(&cp.args, update_index_args);
654 + cp.git_cmd = 1;
655 + cp.no_stdin = 1;
656 + cp.no_stdout = 1;
657 + run_command(&cp);
658 +
659 + child_process_init(&cp);
660 strvec_pushv(&cp.args, diff_index_args);
661 cp.git_cmd = 1;
662 cp.no_stdin = 1;
t/t6120-describe.sh
+36
@@ -671,4 +671,40 @@ test_expect_success 'setup misleading taggerdates' '
671
672 check_describe newer-tag-older-commit~1 --contains unique-file~2
673
674 +test_expect_success 'describe --dirty with a file with changed stat' '
675 + test_when_finished "rm -fr stat-dirty" &&
676 + git init stat-dirty &&
677 + (
678 + cd stat-dirty &&
679 +
680 + echo A >file &&
681 + git add file &&
682 + git commit -m A &&
683 + git tag A -a -m A &&
684 + echo "A" >expect &&
685 +
686 + test-tool chmtime -10 file &&
687 + git describe --dirty >actual &&
688 + test_cmp expect actual
689 + )
690 +'
691 +
692 +test_expect_success 'describe --broken --dirty with a file with changed stat' '
693 + test_when_finished "rm -fr stat-dirty" &&
694 + git init stat-dirty &&
695 + (
696 + cd stat-dirty &&
697 +
698 + echo A >file &&
699 + git add file &&
700 + git commit -m A &&
701 + git tag A -a -m A &&
702 + echo "A" >expect &&
703 +
704 + test-tool chmtime -10 file &&
705 + git describe --dirty --broken >actual &&
706 + test_cmp expect actual
707 + )
708 +'
709 +
710 test_done