Teach "git diff" to handle filenames starting with '-'
[git/git.git] / diff-files.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
4a6bf9e1 7#include "diff.h"
c0fb976a 8
4d1f1190 9static const char diff_files_usage[] =
dda2d79a
JH
10"git-diff-files [-q] "
11"[<common diff options>] [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
b8f80925 13
6b5ee137 14static struct diff_options diff_options;
0a7668e9 15static int silent = 0;
0a7668e9 16
4a6bf9e1 17static void show_unmerge(const char *path)
0a7668e9 18{
6b5ee137 19 diff_unmerge(&diff_options, path);
4a6bf9e1
JH
20}
21
22static void show_file(int pfx, struct cache_entry *ce)
23{
6b5ee137
JH
24 diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25 ce->sha1, ce->name, NULL);
4a6bf9e1
JH
26}
27
28static void show_modified(int oldmode, int mode,
bf0f910d 29 const unsigned char *old_sha1, const unsigned char *sha1,
4a6bf9e1
JH
30 char *path)
31{
6b5ee137 32 diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
0a7668e9
LT
33}
34
6b5ee137 35int main(int argc, const char **argv)
e83c5163 36{
c0fd1f51 37 const char **pathspec;
d288a700
LT
38 const char *prefix = setup_git_directory();
39 int entries, i;
e83c5163 40
17712991 41 git_config(git_default_config);
6b5ee137 42 diff_setup(&diff_options);
b8f80925 43 while (1 < argc && argv[1][0] == '-') {
ea51d416
LT
44 if (!strcmp(argv[1], "--")) {
45 argv++;
46 argc--;
47 break;
48 }
6b5ee137 49 if (!strcmp(argv[1], "-q"))
d15aa430 50 silent = 1;
0a7668e9 51 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 52 ; /* no-op */
d15aa430
JH
53 else if (!strcmp(argv[1], "-s"))
54 ; /* no-op */
6b5ee137
JH
55 else {
56 int diff_opt_cnt;
57 diff_opt_cnt = diff_opt_parse(&diff_options,
58 argv+1, argc-1);
59 if (diff_opt_cnt < 0)
0e3994fa 60 usage(diff_files_usage);
6b5ee137
JH
61 else if (diff_opt_cnt) {
62 argv += diff_opt_cnt;
63 argc -= diff_opt_cnt;
64 continue;
65 }
66 else
0e3994fa 67 usage(diff_files_usage);
427dcb4b 68 }
b8f80925 69 argv++; argc--;
e2e5e98a
PB
70 }
71
d288a700
LT
72 /* Find the directory, and set up the pathspec */
73 pathspec = get_pathspec(prefix, argv + 1);
74 entries = read_cache();
c0fd1f51 75
6b5ee137 76 if (diff_setup_done(&diff_options) < 0)
4727f640
JH
77 usage(diff_files_usage);
78
b8f80925
JH
79 /* At this point, if argc == 1, then we are doing everything.
80 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
81 */
e83c5163
LT
82 if (entries < 0) {
83 perror("read_cache");
84 exit(1);
85 }
be3cfa85 86
e83c5163
LT
87 for (i = 0; i < entries; i++) {
88 struct stat st;
3e09cdfd 89 unsigned int oldmode, newmode;
e83c5163 90 struct cache_entry *ce = active_cache[i];
d94c6128 91 int changed;
e83c5163 92
c0fd1f51
LT
93 if (!ce_path_match(ce, pathspec))
94 continue;
95
9fec8b26 96 if (ce_stage(ce)) {
4a6bf9e1 97 show_unmerge(ce->name);
9fec8b26
JH
98 while (i < entries &&
99 !strcmp(ce->name, active_cache[i]->name))
100 i++;
101 i--; /* compensate for loop control increments */
102 continue;
103 }
57fe64a4 104
ffbe1add 105 if (lstat(ce->name, &st) < 0) {
41174694 106 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 107 perror(ce->name);
ca2a0798 108 continue;
57fe64a4 109 }
d15aa430 110 if (silent)
0a7668e9 111 continue;
4a6bf9e1 112 show_file('-', ce);
e83c5163
LT
113 continue;
114 }
5d728c84 115 changed = ce_match_stat(ce, &st);
6b5ee137 116 if (!changed && !diff_options.find_copies_harder)
e83c5163 117 continue;
0a7668e9 118 oldmode = ntohl(ce->ce_mode);
3e09cdfd
JH
119
120 newmode = DIFF_FILE_CANON_MODE(st.st_mode);
121 if (!trust_executable_bit &&
122 S_ISREG(newmode) && S_ISREG(oldmode) &&
123 ((newmode ^ oldmode) == 0111))
124 newmode = oldmode;
125 show_modified(oldmode, newmode,
4727f640 126 ce->sha1, (changed ? null_sha1 : ce->sha1),
4a6bf9e1 127 ce->name);
e83c5163 128 }
6b5ee137
JH
129 diffcore_std(&diff_options);
130 diff_flush(&diff_options);
e83c5163
LT
131 return 0;
132}