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