Skip to content

Commit

Permalink
Merge branch 'th/diff-no-index-fixes' into maint
Browse files Browse the repository at this point in the history
"git diff --no-index" did not correctly handle relative paths and
did not correctly give exit codes when run under "--quiet" option.

* th/diff-no-index-fixes:
  diff-no-index: exit(1) if 'diff --quiet <repo file> <external file>' finds changes
  diff: handle relative paths in no-index
  • Loading branch information
Junio C Hamano committed Jul 11, 2012
2 parents 4ac01b0 + 304970d commit 36c5109
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 24 deletions.
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ extern void verify_filename(const char *prefix,
const char *name,
int diagnose_misspelt_rev);
extern void verify_non_filename(const char *prefix, const char *name);
extern int path_inside_repo(const char *prefix, const char *path);

#define INIT_DB_QUIET 0x0001

Expand Down
23 changes: 3 additions & 20 deletions diff-no-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,6 @@ static int queue_diff(struct diff_options *o,
}
}

static int path_outside_repo(const char *path)
{
const char *work_tree;
size_t len;

if (!is_absolute_path(path))
return 0;
work_tree = get_git_work_tree();
if (!work_tree)
return 1;
len = strlen(work_tree);
if (strncmp(path, work_tree, len) ||
(path[len] != '\0' && path[len] != '/'))
return 1;
return 0;
}

void diff_no_index(struct rev_info *revs,
int argc, const char **argv,
int nongit, const char *prefix)
Expand Down Expand Up @@ -197,8 +180,8 @@ void diff_no_index(struct rev_info *revs,
* a colourful "diff" replacement.
*/
if ((argc != i + 2) ||
(!path_outside_repo(argv[i]) &&
!path_outside_repo(argv[i+1])))
(path_inside_repo(prefix, argv[i]) &&
path_inside_repo(prefix, argv[i+1])))
return;
}
if (argc != i + 2)
Expand Down Expand Up @@ -268,5 +251,5 @@ void diff_no_index(struct rev_info *revs,
* The return code for --no-index imitates diff(1):
* 0 = no changes, 1 = changes, else error
*/
exit(revs->diffopt.found_changes);
exit(diff_result_code(&revs->diffopt, 0));
}
24 changes: 22 additions & 2 deletions setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
static int inside_git_dir = -1;
static int inside_work_tree = -1;

char *prefix_path(const char *prefix, int len, const char *path)
static char *prefix_path_gently(const char *prefix, int len, const char *path)
{
const char *orig = path;
char *sanitized;
Expand All @@ -31,7 +31,8 @@ char *prefix_path(const char *prefix, int len, const char *path)
if (strncmp(sanitized, work_tree, len) ||
(len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) {
error_out:
die("'%s' is outside repository", orig);
free(sanitized);
return NULL;
}
if (sanitized[len] == '/')
len++;
Expand All @@ -40,6 +41,25 @@ char *prefix_path(const char *prefix, int len, const char *path)
return sanitized;
}

char *prefix_path(const char *prefix, int len, const char *path)
{
char *r = prefix_path_gently(prefix, len, path);
if (!r)
die("'%s' is outside repository", path);
return r;
}

int path_inside_repo(const char *prefix, const char *path)
{
int len = prefix ? strlen(prefix) : 0;
char *r = prefix_path_gently(prefix, len, path);
if (r) {
free(r);
return 1;
}
return 0;
}

int check_filename(const char *prefix, const char *arg)
{
const char *name;
Expand Down
73 changes: 72 additions & 1 deletion t/t4035-diff-quiet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ test_expect_success 'setup' '
git commit -m first &&
echo 2 >b &&
git add . &&
git commit -a -m second
git commit -a -m second &&
mkdir -p test-outside/repo && (
cd test-outside/repo &&
git init &&
echo "1 1" >a &&
git add . &&
git commit -m 1
) &&
mkdir -p test-outside/non/git && (
cd test-outside/non/git &&
echo "1 1" >a &&
echo "1 1" >matching-file &&
echo "1 1 " >trailing-space &&
echo "1 1" >extra-space &&
echo "2" >never-match
)
'

test_expect_success 'git diff-tree HEAD^ HEAD' '
Expand Down Expand Up @@ -77,4 +92,60 @@ test_expect_success 'git diff-index --cached HEAD' '
}
'

test_expect_success 'git diff, one file outside repo' '
(
cd test-outside/repo &&
test_expect_code 0 git diff --quiet a ../non/git/matching-file &&
test_expect_code 1 git diff --quiet a ../non/git/extra-space
)
'

test_expect_success 'git diff, both files outside repo' '
(
GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
export GIT_CEILING_DIRECTORIES &&
cd test-outside/non/git &&
test_expect_code 0 git diff --quiet a matching-file &&
test_expect_code 1 git diff --quiet a extra-space
)
'

test_expect_success 'git diff --ignore-space-at-eol, one file outside repo' '
(
cd test-outside/repo &&
test_expect_code 0 git diff --quiet --ignore-space-at-eol a ../non/git/trailing-space &&
test_expect_code 1 git diff --quiet --ignore-space-at-eol a ../non/git/extra-space
)
'

test_expect_success 'git diff --ignore-space-at-eol, both files outside repo' '
(
GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
export GIT_CEILING_DIRECTORIES &&
cd test-outside/non/git &&
test_expect_code 0 git diff --quiet --ignore-space-at-eol a trailing-space &&
test_expect_code 1 git diff --quiet --ignore-space-at-eol a extra-space
)
'

test_expect_success 'git diff --ignore-all-space, one file outside repo' '
(
cd test-outside/repo &&
test_expect_code 0 git diff --quiet --ignore-all-space a ../non/git/trailing-space &&
test_expect_code 0 git diff --quiet --ignore-all-space a ../non/git/extra-space &&
test_expect_code 1 git diff --quiet --ignore-all-space a ../non/git/never-match
)
'

test_expect_success 'git diff --ignore-all-space, both files outside repo' '
(
GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
export GIT_CEILING_DIRECTORIES &&
cd test-outside/non/git &&
test_expect_code 0 git diff --quiet --ignore-all-space a trailing-space &&
test_expect_code 0 git diff --quiet --ignore-all-space a extra-space &&
test_expect_code 1 git diff --quiet --ignore-all-space a never-match
)
'

test_done
15 changes: 14 additions & 1 deletion t/t4053-diff-no-index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ test_expect_success 'setup' '
mkdir a &&
mkdir b &&
echo 1 >a/1 &&
echo 2 >a/2
echo 2 >a/2 &&
git init repo &&
echo 1 >repo/a &&
mkdir -p non/git &&
echo 1 >non/git/a &&
echo 1 >non/git/b
'

test_expect_success 'git diff --no-index directories' '
git diff --no-index a b >cnt
test $? = 1 && test_line_count = 14 cnt
'

test_expect_success 'git diff --no-index relative path outside repo' '
(
cd repo &&
test_expect_code 0 git diff --no-index a ../non/git/a &&
test_expect_code 0 git diff --no-index ../non/git/a ../non/git/b
)
'

test_done

0 comments on commit 36c5109

Please sign in to comment.