#include "revision.h"
#include "log-tree.h"
#include "builtin.h"
-#include "path-list.h"
+#include "string-list.h"
-static int read_directory(const char *path, struct path_list *list)
+static int read_directory(const char *path, struct string_list *list)
{
DIR *dir;
struct dirent *e;
while ((e = readdir(dir)))
if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
- path_list_insert(e->d_name, list);
+ string_list_insert(e->d_name, list);
closedir(dir);
return 0;
if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
char buffer1[PATH_MAX], buffer2[PATH_MAX];
- struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
+ struct string_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
int len1 = 0, len2 = 0, i1, i2, ret = 0;
if (name1 && read_directory(name1, &p1))
return -1;
if (name2 && read_directory(name2, &p2)) {
- path_list_clear(&p1, 0);
+ string_list_clear(&p1, 0);
return -1;
}
else if (i2 == p2.nr)
comp = -1;
else
- comp = strcmp(p1.items[i1].path,
- p2.items[i2].path);
+ comp = strcmp(p1.items[i1].string,
+ p2.items[i2].string);
if (comp > 0)
n1 = NULL;
else {
n1 = buffer1;
- strncpy(buffer1 + len1, p1.items[i1++].path,
+ strncpy(buffer1 + len1, p1.items[i1++].string,
PATH_MAX - len1);
}
n2 = NULL;
else {
n2 = buffer2;
- strncpy(buffer2 + len2, p2.items[i2++].path,
+ strncpy(buffer2 + len2, p2.items[i2++].string,
PATH_MAX - len2);
}
ret = queue_diff(o, n1, n2);
}
- path_list_clear(&p1, 0);
- path_list_clear(&p2, 0);
+ string_list_clear(&p1, 0);
+ string_list_clear(&p2, 0);
return ret;
} else {
}
}
+static int path_outside_repo(const char *path)
+{
+ /*
+ * We have already done setup_git_directory_gently() so we
+ * know we are inside a git work tree already.
+ */
+ const char *work_tree;
+ size_t len;
+
+ if (!is_absolute_path(path))
+ return 0;
+ work_tree = get_git_work_tree();
+ 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)
break;
}
- /*
- * No explicit --no-index, but "git diff --opts A B" outside
- * a git repository is a cute hack to support.
- */
- if (!no_index && !nongit)
- return;
-
+ if (!no_index && !nongit) {
+ /*
+ * Inside a git repository, without --no-index. Only
+ * when a path outside the repository is given,
+ * e.g. "git diff /var/tmp/[12]", or "git diff
+ * Makefile /var/tmp/Makefile", allow it to be used as
+ * a colourful "diff" replacement.
+ */
+ if ((argc != i + 2) ||
+ (!path_outside_repo(argv[i]) &&
+ !path_outside_repo(argv[i+1])))
+ return;
+ }
if (argc != i + 2)
die("git diff %s takes two paths",
no_index ? "--no-index" : "[--no-index]");
+ /*
+ * If the user asked for our exit code then don't start a
+ * pager or we would end up reporting its exit code instead.
+ */
+ if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
+ setup_pager();
+
diff_setup(&revs->diffopt);
if (!revs->diffopt.output_format)
revs->diffopt.output_format = DIFF_FORMAT_PATCH;