From d288a70030eaa5c205a72b4548635e17f8e523c0 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 16 Aug 2005 18:06:34 -0700 Subject: [PATCH] [PATCH] Make "git diff" work inside relative subdirectories We always show the diff as an absolute path, but pathnames to diff are taken relative to the current working directory (and if no pathnames are given, the default ends up being all of the current working directory). Note that "../xyz" also works, so you can do cd linux/drivers/char git diff ../block and it will generate a diff of the linux/drivers/block changes. Signed-off-by: Linus Torvalds Signed-off-by: Junio C Hamano --- Makefile | 2 +- cache.h | 3 ++ diff-cache.c | 12 +++--- diff-files.c | 11 ++--- diff-tree.c | 17 ++++++-- git-diff-script | 4 +- rev-parse.c | 7 ++- setup.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 setup.c diff --git a/Makefile b/Makefile index 16ab0c7f0..db7596504 100644 --- a/Makefile +++ b/Makefile @@ -97,7 +97,7 @@ LIB_H=cache.h object.h blob.h tree.h commit.h tag.h delta.h epoch.h csum-file.h LIB_OBJS=read-cache.o sha1_file.o usage.o object.o commit.o tree.o blob.o \ tag.o date.o index.o diff-delta.o patch-delta.o entry.o path.o \ refs.o csum-file.o pack-check.o pkt-line.o connect.o ident.o \ - sha1_name.o + sha1_name.o setup.o LIB_H += rev-cache.h LIB_OBJS += rev-cache.o diff --git a/cache.h b/cache.h index f14a4ce91..6365381c1 100644 --- a/cache.h +++ b/cache.h @@ -140,6 +140,9 @@ extern char *get_graft_file(void); #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" +extern const char **get_pathspec(const char *prefix, char **pathspec); +extern const char *setup_git_directory(void); + #define alloc_nr(x) (((x)+16)*3/2) /* Initialize and use the cache information */ diff --git a/diff-cache.c b/diff-cache.c index 47a4e09ec..400a4cb2a 100644 --- a/diff-cache.c +++ b/diff-cache.c @@ -168,10 +168,11 @@ static const char diff_cache_usage[] = "[] [...]" COMMON_DIFF_OPTIONS_HELP; -int main(int argc, const char **argv) +int main(int argc, char **argv) { const char *tree_name = NULL; unsigned char sha1[20]; + const char *prefix = setup_git_directory(); const char **pathspec = NULL; void *tree; unsigned long size; @@ -179,15 +180,12 @@ int main(int argc, const char **argv) int allow_options = 1; int i; - read_cache(); for (i = 1; i < argc; i++) { const char *arg = argv[i]; if (!allow_options || *arg != '-') { - if (tree_name) { - pathspec = argv + i; + if (tree_name) break; - } tree_name = arg; continue; } @@ -265,12 +263,16 @@ int main(int argc, const char **argv) usage(diff_cache_usage); } + pathspec = get_pathspec(prefix, argv + i); + if (find_copies_harder && detect_rename != DIFF_DETECT_COPY) usage(diff_cache_usage); if (!tree_name || get_sha1(tree_name, sha1)) usage(diff_cache_usage); + read_cache(); + /* The rest is for paths restriction. */ diff_setup(diff_setup_opt); diff --git a/diff-files.c b/diff-files.c index 2e6416e38..89eb29b3e 100644 --- a/diff-files.c +++ b/diff-files.c @@ -41,12 +41,12 @@ static void show_modified(int oldmode, int mode, diff_change(oldmode, mode, old_sha1, sha1, path, NULL); } -int main(int argc, const char **argv) +int main(int argc, char **argv) { static const unsigned char null_sha1[20] = { 0, }; const char **pathspec; - int entries = read_cache(); - int i; + const char *prefix = setup_git_directory(); + int entries, i; while (1 < argc && argv[1][0] == '-') { if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u")) @@ -95,8 +95,9 @@ int main(int argc, const char **argv) argv++; argc--; } - /* Do we have a pathspec? */ - pathspec = (argc > 1) ? argv + 1 : NULL; + /* Find the directory, and set up the pathspec */ + pathspec = get_pathspec(prefix, argv + 1); + entries = read_cache(); if (find_copies_harder && detect_rename != DIFF_DETECT_COPY) usage(diff_files_usage); diff --git a/diff-tree.c b/diff-tree.c index 0dd3cdacb..fc87902a3 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -395,16 +395,25 @@ static int diff_tree_stdin(char *line) return diff_tree_commit(commit, line); } +static int count_paths(const char **paths) +{ + int i = 0; + while (*paths++) + i++; + return i; +} + static const char diff_tree_usage[] = "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] " "[] " COMMON_DIFF_OPTIONS_HELP; -int main(int argc, const char **argv) +int main(int argc, char **argv) { int nr_sha1; char line[1000]; unsigned char sha1[2][20]; + const char *prefix = setup_git_directory(); nr_sha1 = 0; for (;;) { @@ -523,11 +532,11 @@ int main(int argc, const char **argv) if (find_copies_harder && detect_rename != DIFF_DETECT_COPY) usage(diff_tree_usage); - if (argc > 0) { + paths = get_pathspec(prefix, argv); + if (paths) { int i; - paths = argv; - nr_paths = argc; + nr_paths = count_paths(paths); pathlens = xmalloc(nr_paths * sizeof(int)); for (i=0; i