Skip to content

Commit b8ae42e

Browse files
quanta-ktgitster
authored andcommitted
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 <[email protected]> Suggested-by: Junio C Hamano <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Phillip Wood <[email protected]> Signed-off-by: Abhijeet Sonar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 786a3e4 commit b8ae42e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

builtin/describe.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ static const char *diff_index_args[] = {
5454
"diff-index", "--quiet", "HEAD", "--", NULL
5555
};
5656

57+
static const char *update_index_args[] = {
58+
"update-index", "--unmerged", "-q", "--refresh", NULL
59+
};
60+
5761
struct commit_name {
5862
struct hashmap_entry entry;
5963
struct object_id peeled;
@@ -645,6 +649,14 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
645649
if (argc == 0) {
646650
if (broken) {
647651
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);
648660
strvec_pushv(&cp.args, diff_index_args);
649661
cp.git_cmd = 1;
650662
cp.no_stdin = 1;

t/t6120-describe.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,40 @@ test_expect_success 'setup misleading taggerdates' '
671671

672672
check_describe newer-tag-older-commit~1 --contains unique-file~2
673673

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+
674710
test_done

0 commit comments

Comments
 (0)