Browse Source
* js/diff-verbose-submodule: add tests for git diff --submodule Add the --submodule option to the diff option familymaint
Junio C Hamano
15 years ago
7 changed files with 411 additions and 0 deletions
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
#include "cache.h" |
||||
#include "submodule.h" |
||||
#include "dir.h" |
||||
#include "diff.h" |
||||
#include "commit.h" |
||||
#include "revision.h" |
||||
|
||||
int add_submodule_odb(const char *path) |
||||
{ |
||||
struct strbuf objects_directory = STRBUF_INIT; |
||||
struct alternate_object_database *alt_odb; |
||||
|
||||
strbuf_addf(&objects_directory, "%s/.git/objects/", path); |
||||
if (!is_directory(objects_directory.buf)) |
||||
return -1; |
||||
|
||||
/* avoid adding it twice */ |
||||
for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next) |
||||
if (alt_odb->name - alt_odb->base == objects_directory.len && |
||||
!strncmp(alt_odb->base, objects_directory.buf, |
||||
objects_directory.len)) |
||||
return 0; |
||||
|
||||
alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb)); |
||||
alt_odb->next = alt_odb_list; |
||||
strcpy(alt_odb->base, objects_directory.buf); |
||||
alt_odb->name = alt_odb->base + objects_directory.len; |
||||
alt_odb->name[2] = '/'; |
||||
alt_odb->name[40] = '\0'; |
||||
alt_odb->name[41] = '\0'; |
||||
alt_odb_list = alt_odb; |
||||
prepare_alt_odb(); |
||||
return 0; |
||||
} |
||||
|
||||
void show_submodule_summary(FILE *f, const char *path, |
||||
unsigned char one[20], unsigned char two[20], |
||||
const char *del, const char *add, const char *reset) |
||||
{ |
||||
struct rev_info rev; |
||||
struct commit *commit, *left = left, *right; |
||||
struct commit_list *merge_bases, *list; |
||||
const char *message = NULL; |
||||
struct strbuf sb = STRBUF_INIT; |
||||
static const char *format = " %m %s"; |
||||
int fast_forward = 0, fast_backward = 0; |
||||
|
||||
if (is_null_sha1(two)) |
||||
message = "(submodule deleted)"; |
||||
else if (add_submodule_odb(path)) |
||||
message = "(not checked out)"; |
||||
else if (is_null_sha1(one)) |
||||
message = "(new submodule)"; |
||||
else if (!(left = lookup_commit_reference(one)) || |
||||
!(right = lookup_commit_reference(two))) |
||||
message = "(commits not present)"; |
||||
|
||||
if (!message) { |
||||
init_revisions(&rev, NULL); |
||||
setup_revisions(0, NULL, &rev, NULL); |
||||
rev.left_right = 1; |
||||
rev.first_parent_only = 1; |
||||
left->object.flags |= SYMMETRIC_LEFT; |
||||
add_pending_object(&rev, &left->object, path); |
||||
add_pending_object(&rev, &right->object, path); |
||||
merge_bases = get_merge_bases(left, right, 1); |
||||
if (merge_bases) { |
||||
if (merge_bases->item == left) |
||||
fast_forward = 1; |
||||
else if (merge_bases->item == right) |
||||
fast_backward = 1; |
||||
} |
||||
for (list = merge_bases; list; list = list->next) { |
||||
list->item->object.flags |= UNINTERESTING; |
||||
add_pending_object(&rev, &list->item->object, |
||||
sha1_to_hex(list->item->object.sha1)); |
||||
} |
||||
if (prepare_revision_walk(&rev)) |
||||
message = "(revision walker failed)"; |
||||
} |
||||
|
||||
strbuf_addf(&sb, "Submodule %s %s..", path, |
||||
find_unique_abbrev(one, DEFAULT_ABBREV)); |
||||
if (!fast_backward && !fast_forward) |
||||
strbuf_addch(&sb, '.'); |
||||
strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV)); |
||||
if (message) |
||||
strbuf_addf(&sb, " %s\n", message); |
||||
else |
||||
strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); |
||||
fwrite(sb.buf, sb.len, 1, f); |
||||
|
||||
if (!message) { |
||||
while ((commit = get_revision(&rev))) { |
||||
strbuf_setlen(&sb, 0); |
||||
if (commit->object.flags & SYMMETRIC_LEFT) { |
||||
if (del) |
||||
strbuf_addstr(&sb, del); |
||||
} |
||||
else if (add) |
||||
strbuf_addstr(&sb, add); |
||||
format_commit_message(commit, format, &sb, |
||||
rev.date_mode); |
||||
if (reset) |
||||
strbuf_addstr(&sb, reset); |
||||
strbuf_addch(&sb, '\n'); |
||||
fprintf(f, "%s", sb.buf); |
||||
} |
||||
clear_commit_marks(left, ~0); |
||||
clear_commit_marks(right, ~0); |
||||
} |
||||
strbuf_release(&sb); |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
#ifndef SUBMODULE_H |
||||
#define SUBMODULE_H |
||||
|
||||
void show_submodule_summary(FILE *f, const char *path, |
||||
unsigned char one[20], unsigned char two[20], |
||||
const char *del, const char *add, const char *reset); |
||||
|
||||
#endif |
@ -0,0 +1,260 @@
@@ -0,0 +1,260 @@
|
||||
#!/bin/sh |
||||
# |
||||
# Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin |
||||
# |
||||
|
||||
test_description='Support for verbose submodule differences in git diff |
||||
|
||||
This test tries to verify the sanity of the --submodule option of git diff. |
||||
' |
||||
|
||||
. ./test-lib.sh |
||||
|
||||
add_file () { |
||||
sm=$1 |
||||
shift |
||||
owd=$(pwd) |
||||
cd "$sm" |
||||
for name; do |
||||
echo "$name" > "$name" && |
||||
git add "$name" && |
||||
test_tick && |
||||
git commit -m "Add $name" |
||||
done >/dev/null |
||||
git rev-parse --verify HEAD | cut -c1-7 |
||||
cd "$owd" |
||||
} |
||||
commit_file () { |
||||
test_tick && |
||||
git commit "$@" -m "Commit $*" >/dev/null |
||||
} |
||||
|
||||
test_create_repo sm1 && |
||||
add_file . foo >/dev/null |
||||
|
||||
head1=$(add_file sm1 foo1 foo2) |
||||
|
||||
test_expect_success 'added submodule' " |
||||
git add sm1 && |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 0000000...$head1 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
commit_file sm1 && |
||||
head2=$(add_file sm1 foo3) |
||||
|
||||
test_expect_success 'modified submodule(forward)' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head1..$head2: |
||||
> Add foo3 |
||||
EOF |
||||
" |
||||
|
||||
test_expect_success 'modified submodule(forward)' " |
||||
git diff --submodule=log >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head1..$head2: |
||||
> Add foo3 |
||||
EOF |
||||
" |
||||
|
||||
test_expect_success 'modified submodule(forward) --submodule' " |
||||
git diff --submodule >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head1..$head2: |
||||
> Add foo3 |
||||
EOF |
||||
" |
||||
|
||||
fullhead1=$(cd sm1; git rev-list --max-count=1 $head1) |
||||
fullhead2=$(cd sm1; git rev-list --max-count=1 $head2) |
||||
test_expect_success 'modified submodule(forward) --submodule=short' " |
||||
git diff --submodule=short >actual && |
||||
diff actual - <<-EOF |
||||
diff --git a/sm1 b/sm1 |
||||
index $head1..$head2 160000 |
||||
--- a/sm1 |
||||
+++ b/sm1 |
||||
@@ -1 +1 @@ |
||||
-Subproject commit $fullhead1 |
||||
+Subproject commit $fullhead2 |
||||
EOF |
||||
" |
||||
|
||||
commit_file sm1 && |
||||
cd sm1 && |
||||
git reset --hard HEAD~2 >/dev/null && |
||||
head3=$(git rev-parse --verify HEAD | cut -c1-7) && |
||||
cd .. |
||||
|
||||
test_expect_success 'modified submodule(backward)' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head2..$head3 (rewind): |
||||
< Add foo3 |
||||
< Add foo2 |
||||
EOF |
||||
" |
||||
|
||||
head4=$(add_file sm1 foo4 foo5) && |
||||
head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD) |
||||
test_expect_success 'modified submodule(backward and forward)' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head2...$head4: |
||||
> Add foo5 |
||||
> Add foo4 |
||||
< Add foo3 |
||||
< Add foo2 |
||||
EOF |
||||
" |
||||
|
||||
commit_file sm1 && |
||||
mv sm1 sm1-bak && |
||||
echo sm1 >sm1 && |
||||
head5=$(git hash-object sm1 | cut -c1-7) && |
||||
git add sm1 && |
||||
rm -f sm1 && |
||||
mv sm1-bak sm1 |
||||
|
||||
test_expect_success 'typechanged submodule(submodule->blob), --cached' " |
||||
git diff --submodule=log --cached >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 41fbea9...0000000 (submodule deleted) |
||||
diff --git a/sm1 b/sm1 |
||||
new file mode 100644 |
||||
index 0000000..9da5fb8 |
||||
--- /dev/null |
||||
+++ b/sm1 |
||||
@@ -0,0 +1 @@ |
||||
+sm1 |
||||
EOF |
||||
" |
||||
|
||||
test_expect_success 'typechanged submodule(submodule->blob)' " |
||||
git diff --submodule=log >actual && |
||||
diff actual - <<-EOF |
||||
diff --git a/sm1 b/sm1 |
||||
deleted file mode 100644 |
||||
index 9da5fb8..0000000 |
||||
--- a/sm1 |
||||
+++ /dev/null |
||||
@@ -1 +0,0 @@ |
||||
-sm1 |
||||
Submodule sm1 0000000...$head4 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
rm -rf sm1 && |
||||
git checkout-index sm1 |
||||
test_expect_success 'typechanged submodule(submodule->blob)' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head4...0000000 (submodule deleted) |
||||
diff --git a/sm1 b/sm1 |
||||
new file mode 100644 |
||||
index 0000000..$head5 |
||||
--- /dev/null |
||||
+++ b/sm1 |
||||
@@ -0,0 +1 @@ |
||||
+sm1 |
||||
EOF |
||||
" |
||||
|
||||
rm -f sm1 && |
||||
test_create_repo sm1 && |
||||
head6=$(add_file sm1 foo6 foo7) |
||||
fullhead6=$(cd sm1; git rev-list --max-count=1 $head6) |
||||
test_expect_success 'nonexistent commit' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head4...$head6 (commits not present) |
||||
EOF |
||||
" |
||||
|
||||
commit_file |
||||
test_expect_success 'typechanged submodule(blob->submodule)' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
diff --git a/sm1 b/sm1 |
||||
deleted file mode 100644 |
||||
index $head5..0000000 |
||||
--- a/sm1 |
||||
+++ /dev/null |
||||
@@ -1 +0,0 @@ |
||||
-sm1 |
||||
Submodule sm1 0000000...$head6 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
commit_file sm1 && |
||||
rm -rf sm1 |
||||
test_expect_success 'deleted submodule' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head6...0000000 (submodule deleted) |
||||
EOF |
||||
" |
||||
|
||||
test_create_repo sm2 && |
||||
head7=$(add_file sm2 foo8 foo9) && |
||||
git add sm2 |
||||
|
||||
test_expect_success 'multiple submodules' " |
||||
git diff-index -p --submodule=log HEAD >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head6...0000000 (submodule deleted) |
||||
Submodule sm2 0000000...$head7 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
test_expect_success 'path filter' " |
||||
git diff-index -p --submodule=log HEAD sm2 >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm2 0000000...$head7 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
commit_file sm2 |
||||
test_expect_success 'given commit' " |
||||
git diff-index -p --submodule=log HEAD^ >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head6...0000000 (submodule deleted) |
||||
Submodule sm2 0000000...$head7 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
test_expect_success 'given commit --submodule' " |
||||
git diff-index -p --submodule HEAD^ >actual && |
||||
diff actual - <<-EOF |
||||
Submodule sm1 $head6...0000000 (submodule deleted) |
||||
Submodule sm2 0000000...$head7 (new submodule) |
||||
EOF |
||||
" |
||||
|
||||
fullhead7=$(cd sm2; git rev-list --max-count=1 $head7) |
||||
|
||||
test_expect_success 'given commit --submodule=short' " |
||||
git diff-index -p --submodule=short HEAD^ >actual && |
||||
diff actual - <<-EOF |
||||
diff --git a/sm1 b/sm1 |
||||
deleted file mode 160000 |
||||
index $head6..0000000 |
||||
--- a/sm1 |
||||
+++ /dev/null |
||||
@@ -1 +0,0 @@ |
||||
-Subproject commit $fullhead6 |
||||
diff --git a/sm2 b/sm2 |
||||
new file mode 160000 |
||||
index 0000000..$head7 |
||||
--- /dev/null |
||||
+++ b/sm2 |
||||
@@ -0,0 +1 @@ |
||||
+Subproject commit $fullhead7 |
||||
EOF |
||||
" |
||||
|
||||
test_done |
Loading…
Reference in new issue