Skip to content

Commit

Permalink
Teach diff that modified submodule directory is dirty
Browse files Browse the repository at this point in the history
A diff run in superproject only compares the name of the commit object
bound at the submodule paths.  When we compare with a work tree and the
checked out submodule directory is dirty (e.g. has either staged or
unstaged changes, or has new files the user forgot to add to the index),
show the work tree side as "dirty".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Jan 17, 2010
1 parent ee6fc51 commit 8e08b41
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
9 changes: 7 additions & 2 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -2029,9 +2029,14 @@ static int populate_from_stdin(struct diff_filespec *s)
static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
{
int len;
char *data = xmalloc(100);
char *data = xmalloc(100), *dirty = "";

/* Are we looking at the work tree? */
if (!s->sha1_valid && is_submodule_modified(s->path))
dirty = "-dirty";

len = snprintf(data, 100,
"Subproject commit %s\n", sha1_to_hex(s->sha1));
"Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
s->data = data;
s->size = len;
s->should_free = 1;
Expand Down
84 changes: 83 additions & 1 deletion t/t4027-diff-submodule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ test_expect_success setup '
cd sub &&
git rev-list HEAD
) &&
echo ":160000 160000 $3 $_z40 M sub" >expect
echo ":160000 160000 $3 $_z40 M sub" >expect &&
subtip=$3 subprev=$2
'

test_expect_success 'git diff --raw HEAD' '
Expand All @@ -50,6 +51,87 @@ test_expect_success 'git diff-files --raw' '
test_cmp expect actual.files
'

expect_from_to () {
printf "%sSubproject commit %s\n+Subproject commit %s\n" \
"-" "$1" "$2"
}

test_expect_success 'git diff HEAD' '
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subtip $subprev &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff HEAD with dirty submodule (work tree)' '
echo >>sub/world &&
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subtip $subprev-dirty &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff HEAD with dirty submodule (index)' '
(
cd sub &&
git reset --hard &&
echo >>world &&
git add world
) &&
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subtip $subprev-dirty &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff HEAD with dirty submodule (untracked)' '
(
cd sub &&
git reset --hard &&
git clean -qfdx &&
>cruft
) &&
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subtip $subprev-dirty &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff HEAD with dirty submodule (work tree, refs match)' '
git commit -m "x" sub &&
echo >>sub/world &&
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subprev $subprev-dirty &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff HEAD with dirty submodule (index, refs match)' '
(
cd sub &&
git reset --hard &&
echo >>world &&
git add world
) &&
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subprev $subprev-dirty &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff HEAD with dirty submodule (untracked, refs match)' '
(
cd sub &&
git reset --hard &&
git clean -qfdx &&
>cruft
) &&
git diff HEAD >actual &&
sed -e "1,/^@@/d" actual >actual.body &&
expect_from_to >expect.body $subprev $subprev-dirty &&
test_cmp expect.body actual.body
'

test_expect_success 'git diff (empty submodule dir)' '
: >empty &&
rm -rf sub/* sub/.git &&
Expand Down

0 comments on commit 8e08b41

Please sign in to comment.