Commit | Line | Data |
---|---|---|
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 | |
17710391 | 9 | static const char *diff_files_usage = |
52e95789 | 10 | "git-diff-files [-p] [-q] [-r] [-z] [-M] [-C] [-R] [-S<string>] [paths...]"; |
b8f80925 | 11 | |
81e50eab | 12 | static int diff_output_format = DIFF_FORMAT_HUMAN; |
5c97558c | 13 | static int detect_rename = 0; |
57fe64a4 JH |
14 | static int reverse_diff = 0; |
15 | static int diff_score_opt = 0; | |
057c7d30 | 16 | static const char *pickaxe = NULL; |
0a7668e9 | 17 | static int silent = 0; |
0a7668e9 | 18 | |
4a6bf9e1 | 19 | static void show_unmerge(const char *path) |
0a7668e9 | 20 | { |
57fe64a4 | 21 | diff_unmerge(path); |
4a6bf9e1 JH |
22 | } |
23 | ||
24 | static void show_file(int pfx, struct cache_entry *ce) | |
25 | { | |
57fe64a4 | 26 | diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL); |
4a6bf9e1 JH |
27 | } |
28 | ||
29 | static void show_modified(int oldmode, int mode, | |
bf0f910d | 30 | const unsigned char *old_sha1, const unsigned char *sha1, |
4a6bf9e1 JH |
31 | char *path) |
32 | { | |
57fe64a4 | 33 | diff_change(oldmode, mode, old_sha1, sha1, path, NULL); |
0a7668e9 LT |
34 | } |
35 | ||
6b14d7fa | 36 | int main(int argc, const char **argv) |
e83c5163 | 37 | { |
bf0f910d | 38 | static const unsigned char null_sha1[20] = { 0, }; |
e83c5163 LT |
39 | int entries = read_cache(); |
40 | int i; | |
41 | ||
b8f80925 | 42 | while (1 < argc && argv[1][0] == '-') { |
d15aa430 | 43 | if (!strcmp(argv[1], "-p")) |
81e50eab | 44 | diff_output_format = DIFF_FORMAT_PATCH; |
b8f80925 | 45 | else if (!strcmp(argv[1], "-q")) |
d15aa430 | 46 | silent = 1; |
0a7668e9 | 47 | else if (!strcmp(argv[1], "-r")) |
4a6bf9e1 | 48 | ; /* no-op */ |
d15aa430 JH |
49 | else if (!strcmp(argv[1], "-s")) |
50 | ; /* no-op */ | |
51 | else if (!strcmp(argv[1], "-z")) | |
81e50eab | 52 | diff_output_format = DIFF_FORMAT_MACHINE; |
57fe64a4 JH |
53 | else if (!strcmp(argv[1], "-R")) |
54 | reverse_diff = 1; | |
52e95789 JH |
55 | else if (!strcmp(argv[1], "-S")) |
56 | pickaxe = argv[1] + 2; | |
57fe64a4 JH |
57 | else if (!strncmp(argv[1], "-M", 2)) { |
58 | diff_score_opt = diff_scoreopt_parse(argv[1]); | |
6b14d7fa | 59 | detect_rename = DIFF_DETECT_RENAME; |
5c97558c | 60 | } |
427dcb4b JH |
61 | else if (!strncmp(argv[1], "-C", 2)) { |
62 | diff_score_opt = diff_scoreopt_parse(argv[1]); | |
6b14d7fa | 63 | detect_rename = DIFF_DETECT_COPY; |
427dcb4b | 64 | } |
b8f80925 | 65 | else |
17710391 | 66 | usage(diff_files_usage); |
b8f80925 | 67 | argv++; argc--; |
e2e5e98a PB |
68 | } |
69 | ||
b8f80925 JH |
70 | /* At this point, if argc == 1, then we are doing everything. |
71 | * Otherwise argv[1] .. argv[argc-1] have the explicit paths. | |
72 | */ | |
e83c5163 LT |
73 | if (entries < 0) { |
74 | perror("read_cache"); | |
75 | exit(1); | |
76 | } | |
be3cfa85 | 77 | |
6b14d7fa | 78 | diff_setup(reverse_diff); |
5c97558c | 79 | |
e83c5163 LT |
80 | for (i = 0; i < entries; i++) { |
81 | struct stat st; | |
0a7668e9 | 82 | unsigned int oldmode, mode; |
e83c5163 | 83 | struct cache_entry *ce = active_cache[i]; |
d94c6128 | 84 | int changed; |
e83c5163 | 85 | |
9fec8b26 | 86 | if (ce_stage(ce)) { |
4a6bf9e1 | 87 | show_unmerge(ce->name); |
9fec8b26 JH |
88 | while (i < entries && |
89 | !strcmp(ce->name, active_cache[i]->name)) | |
90 | i++; | |
91 | i--; /* compensate for loop control increments */ | |
92 | continue; | |
93 | } | |
57fe64a4 | 94 | |
ffbe1add | 95 | if (lstat(ce->name, &st) < 0) { |
41174694 | 96 | if (errno != ENOENT && errno != ENOTDIR) { |
0a7668e9 | 97 | perror(ce->name); |
ca2a0798 | 98 | continue; |
57fe64a4 | 99 | } |
d15aa430 | 100 | if (silent) |
0a7668e9 | 101 | continue; |
4a6bf9e1 | 102 | show_file('-', ce); |
e83c5163 LT |
103 | continue; |
104 | } | |
5d728c84 | 105 | changed = ce_match_stat(ce, &st); |
6b14d7fa | 106 | if (!changed && detect_rename < DIFF_DETECT_COPY) |
e83c5163 | 107 | continue; |
e2e5e98a | 108 | |
0a7668e9 | 109 | oldmode = ntohl(ce->ce_mode); |
95649d6c JH |
110 | mode = (S_ISLNK(st.st_mode) ? S_IFLNK : |
111 | S_IFREG | ce_permissions(st.st_mode)); | |
0a7668e9 | 112 | |
4a6bf9e1 JH |
113 | show_modified(oldmode, mode, ce->sha1, null_sha1, |
114 | ce->name); | |
e83c5163 | 115 | } |
38c6f780 | 116 | if (detect_rename) |
6b14d7fa JH |
117 | diffcore_rename(detect_rename, diff_score_opt); |
118 | diffcore_prune(); | |
38c6f780 | 119 | if (pickaxe) |
6b14d7fa JH |
120 | diffcore_pickaxe(pickaxe); |
121 | if (1 < argc) | |
122 | diffcore_pathspec(argv + 1); | |
123 | diff_flush(diff_output_format); | |
e83c5163 LT |
124 | return 0; |
125 | } |