Skip to content

Commit

Permalink
Merge branch 'js/diff-ni'
Browse files Browse the repository at this point in the history
* js/diff-ni:
  Get rid of the dependency to GNU diff in the tests
  diff --no-index: support /dev/null as filename
  diff-ni: fix the diff with standard input
  diff: support reading a file from stdin via "-"
  • Loading branch information
Junio C Hamano committed Mar 11, 2007
2 parents 8509fed + 5bd7450 commit cf6981d
Show file tree
Hide file tree
Showing 32 changed files with 176 additions and 131 deletions.
42 changes: 27 additions & 15 deletions diff-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@ static int read_directory(const char *path, struct path_list *list)
return 0;
}

static int get_mode(const char *path, int *mode)
{
struct stat st;

if (!path || !strcmp(path, "/dev/null"))
*mode = 0;
else if (!strcmp(path, "-"))
*mode = ntohl(create_ce_mode(0666));
else if (stat(path, &st))
return error("Could not access '%s'", path);
else
*mode = st.st_mode;
return 0;
}

static int queue_diff(struct diff_options *o,
const char *name1, const char *name2)
{
struct stat st;
int mode1 = 0, mode2 = 0;

if (name1) {
if (stat(name1, &st))
return error("Could not access '%s'", name1);
mode1 = st.st_mode;
}
if (name2) {
if (stat(name2, &st))
return error("Could not access '%s'", name2);
mode2 = st.st_mode;
}
if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
return -1;

if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
return error("file/directory conflict: %s, %s", name1, name2);
Expand Down Expand Up @@ -224,7 +230,7 @@ int setup_diff_no_index(struct rev_info *revs,
{
int i;
for (i = 1; i < argc; i++)
if (argv[i][0] != '-')
if (argv[i][0] != '-' || argv[i][1] == '\0')
break;
else if (!strcmp(argv[i], "--")) {
i++;
Expand Down Expand Up @@ -254,9 +260,15 @@ int setup_diff_no_index(struct rev_info *revs,

revs->diffopt.paths = xcalloc(2, sizeof(char*));
for (i = 0; i < 2; i++) {
const char *p;
p = prefix_filename(prefix, len, argv[argc - 2 + i]);
revs->diffopt.paths[i] = xstrdup(p);
const char *p = argv[argc - 2 + i];
/*
* stdin should be spelled as '-'; if you have
* path that is '-', spell it as ./-.
*/
p = (strcmp(p, "-")
? xstrdup(prefix_filename(prefix, len, p))
: p);
revs->diffopt.paths[i] = p;
}
}
else
Expand Down
33 changes: 33 additions & 0 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,32 @@ static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
return e;
}

static int populate_from_stdin(struct diff_filespec *s)
{
#define INCREMENT 1024
char *buf;
unsigned long size;
int got;

size = 0;
buf = NULL;
while (1) {
buf = xrealloc(buf, size + INCREMENT);
got = xread(0, buf + size, INCREMENT);
if (!got)
break; /* EOF */
if (got < 0)
return error("error while reading from stdin %s",
strerror(errno));
size += got;
}
s->should_munmap = 0;
s->data = buf;
s->size = size;
s->should_free = 1;
return 0;
}

/*
* While doing rename detection and pickaxe operation, we may need to
* grab the data for the blob (or file) for our own in-core comparison.
Expand All @@ -1389,6 +1415,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
char *buf;
unsigned long size;

if (!strcmp(s->path, "-"))
return populate_from_stdin(s);

if (lstat(s->path, &st) < 0) {
if (errno == ENOENT) {
err_empty:
Expand Down Expand Up @@ -1690,6 +1719,10 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
if (DIFF_FILE_VALID(one)) {
if (!one->sha1_valid) {
struct stat st;
if (!strcmp(one->path, "-")) {
hashcpy(one->sha1, null_sha1);
return;
}
if (lstat(one->path, &st) < 0)
die("stat %s", one->path);
if (index_path(one->sha1, one->path, &st, 0))
Expand Down
6 changes: 3 additions & 3 deletions t/diff-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ compare_diff_raw () {

sed -e "$sanitize_diff_raw" <"$1" >.tmp-1
sed -e "$sanitize_diff_raw" <"$2" >.tmp-2
diff -u .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
git diff .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
}

sanitize_diff_raw_z='/^:/s/ '"$_x40"' '"$_x40"' \([A-Z]\)[0-9]*$/ X X \1#/'
Expand All @@ -23,7 +23,7 @@ compare_diff_raw_z () {

tr '\0' '\012' <"$1" | sed -e "$sanitize_diff_raw_z" >.tmp-1
tr '\0' '\012' <"$2" | sed -e "$sanitize_diff_raw_z" >.tmp-2
diff -u .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
git diff .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
}

compare_diff_patch () {
Expand All @@ -37,5 +37,5 @@ compare_diff_patch () {
/^[dis]*imilarity index [0-9]*%$/d
/^index [0-9a-f]*\.\.[0-9a-f]/d
' <"$2" >.tmp-2
diff -u .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
git diff .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
}
2 changes: 1 addition & 1 deletion t/t1000-read-tree-m-3way.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"

check_result () {
git-ls-files --stage | sed -e 's/ '"$_x40"' / X /' >current &&
diff -u expected current
git diff expected current
}

# This is done on an empty work directory, which is the normal
Expand Down
26 changes: 13 additions & 13 deletions t/t1001-read-tree-m-2way.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ compare_change () {
-e '/^--- /d; /^+++ /d; /^@@ /d;' \
-e 's/^\([-+][0-7][0-7][0-7][0-7][0-7][0-7]\) '"$_x40"' /\1 X /p' \
"$1"
diff -u expected current
git diff expected current
}

check_cache_at () {
Expand Down Expand Up @@ -86,7 +86,7 @@ test_expect_success \
'rm -f .git/index &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >1-3.out &&
diff -u M.out 1-3.out &&
git diff M.out 1-3.out &&
check_cache_at bozbar dirty &&
check_cache_at frotz dirty &&
check_cache_at nitfol dirty'
Expand All @@ -101,7 +101,7 @@ test_expect_success \
git-update-index --add yomin &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >4.out || return 1
diff -u M.out 4.out >4diff.out
git diff M.out 4.out >4diff.out
compare_change 4diff.out expected &&
check_cache_at yomin clean'

Expand All @@ -115,7 +115,7 @@ test_expect_success \
echo yomin yomin >yomin &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >5.out || return 1
diff -u M.out 5.out >5diff.out
git diff M.out 5.out >5diff.out
compare_change 5diff.out expected &&
check_cache_at yomin dirty'

Expand All @@ -127,7 +127,7 @@ test_expect_success \
git-update-index --add frotz &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >6.out &&
diff -u M.out 6.out &&
git diff M.out 6.out &&
check_cache_at frotz clean'

test_expect_success \
Expand All @@ -140,7 +140,7 @@ test_expect_success \
echo frotz frotz >frotz &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >7.out &&
diff -u M.out 7.out &&
git diff M.out 7.out &&
check_cache_at frotz dirty'

test_expect_success \
Expand Down Expand Up @@ -171,7 +171,7 @@ test_expect_success \
git-update-index --add rezrov &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >10.out &&
diff -u M.out 10.out'
git diff M.out 10.out'

test_expect_success \
'11 - dirty path removed.' \
Expand Down Expand Up @@ -216,7 +216,7 @@ test_expect_success \
git-update-index --add nitfol &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >14.out || return 1
diff -u M.out 14.out >14diff.out
git diff M.out 14.out >14diff.out
compare_change 14diff.out expected &&
check_cache_at nitfol clean'

Expand All @@ -230,7 +230,7 @@ test_expect_success \
echo nitfol nitfol nitfol >nitfol &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >15.out || return 1
diff -u M.out 15.out >15diff.out
git diff M.out 15.out >15diff.out
compare_change 15diff.out expected &&
check_cache_at nitfol dirty'

Expand Down Expand Up @@ -262,7 +262,7 @@ test_expect_success \
git-update-index --add bozbar &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >18.out &&
diff -u M.out 18.out &&
git diff M.out 18.out &&
check_cache_at bozbar clean'

test_expect_success \
Expand All @@ -275,7 +275,7 @@ test_expect_success \
echo gnusto gnusto >bozbar &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >19.out &&
diff -u M.out 19.out &&
git diff M.out 19.out &&
check_cache_at bozbar dirty'

test_expect_success \
Expand All @@ -287,7 +287,7 @@ test_expect_success \
git-update-index --add bozbar &&
read_tree_twoway $treeH $treeM &&
git-ls-files --stage >20.out &&
diff -u M.out 20.out &&
git diff M.out 20.out &&
check_cache_at bozbar dirty'

test_expect_success \
Expand Down Expand Up @@ -337,7 +337,7 @@ test_expect_success \
git-update-index --add DF &&
read_tree_twoway $treeDF $treeDFDF &&
git-ls-files --stage >DFDFcheck.out &&
diff -u DFDF.out DFDFcheck.out &&
git diff DFDF.out DFDFcheck.out &&
check_cache_at DF/DF dirty &&
:'

Expand Down
2 changes: 1 addition & 1 deletion t/t1002-read-tree-m-u-2way.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ compare_change () {
sed >current \
-e '/^--- /d; /^+++ /d; /^@@ /d;' \
-e 's/^\(.[0-7][0-7][0-7][0-7][0-7][0-7]\) '"$_x40"' /\1 X /' "$1"
diff -u expected current
git diff expected current
}

check_cache_at () {
Expand Down
8 changes: 4 additions & 4 deletions t/t1300-repo-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,12 @@ cat > expect << EOF
weird
EOF

test_expect_success "rename succeeded" "diff -u expect .git/config"
test_expect_success "rename succeeded" "git diff expect .git/config"

test_expect_failure "rename non-existing section" \
'git-config --rename-section branch."world domination" branch.drei'

test_expect_success "rename succeeded" "diff -u expect .git/config"
test_expect_success "rename succeeded" "git diff expect .git/config"

test_expect_success "rename another section" \
'git-config --rename-section branch."1 234 blabl/a" branch.drei'
Expand All @@ -389,7 +389,7 @@ cat > expect << EOF
weird
EOF

test_expect_success "rename succeeded" "diff -u expect .git/config"
test_expect_success "rename succeeded" "git diff expect .git/config"

cat >> .git/config << EOF
[branch "zwei"] a = 1 [branch "vier"]
Expand All @@ -405,7 +405,7 @@ weird
EOF

test_expect_success "section was removed properly" \
"diff -u expect .git/config"
"git diff -u expect .git/config"

test_expect_success numbers '
Expand Down
4 changes: 2 additions & 2 deletions t/t3001-ls-files-others-exclude.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test_expect_success \
--exclude-per-directory=.gitignore \
--exclude-from=.git/ignore \
>output &&
diff -u expect output'
git diff expect output'

# Test \r\n (MSDOS-like systems)
printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
Expand All @@ -77,6 +77,6 @@ test_expect_success \
--exclude-per-directory=.gitignore \
--exclude-from=.git/ignore \
>output &&
diff -u expect output'
git diff expect output'

test_done
10 changes: 5 additions & 5 deletions t/t3002-ls-files-dashpath.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_expect_success \
test_expect_success \
'git-ls-files without path restriction.' \
'git-ls-files --others >output &&
diff -u output - <<EOF
git diff output - <<EOF
--
-foo
output
Expand All @@ -34,31 +34,31 @@ EOF
test_expect_success \
'git-ls-files with path restriction.' \
'git-ls-files --others path0 >output &&
diff -u output - <<EOF
git diff output - <<EOF
path0
EOF
'

test_expect_success \
'git-ls-files with path restriction with --.' \
'git-ls-files --others -- path0 >output &&
diff -u output - <<EOF
git diff output - <<EOF
path0
EOF
'

test_expect_success \
'git-ls-files with path restriction with -- --.' \
'git-ls-files --others -- -- >output &&
diff -u output - <<EOF
git diff output - <<EOF
--
EOF
'

test_expect_success \
'git-ls-files with no path restriction.' \
'git-ls-files --others -- >output &&
diff -u output - <<EOF
git diff output - <<EOF
--
-foo
output
Expand Down
2 changes: 1 addition & 1 deletion t/t3100-ls-tree-restrict.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
test_output () {
sed -e "s/ $_x40 / X /" <current >check
diff -u expected check
git diff expected check
}

test_expect_success \
Expand Down
2 changes: 1 addition & 1 deletion t/t3101-ls-tree-dirname.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
test_output () {
sed -e "s/ $_x40 / X /" <current >check
diff -u expected check
git diff expected check
}

test_expect_success \
Expand Down
Loading

0 comments on commit cf6981d

Please sign in to comment.