git-pull-script: do automatic merges
[git/git.git] / show-diff.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"
86436c28 7#include "diff.h"
c0fb976a 8
d2522a65 9static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [paths...]";
b8f80925
JH
10
11static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
12{
13 int i;
14 int namelen = ce_namelen(ce);
15 for (i = 0; i < cnt; i++) {
16 int speclen = strlen(spec[i]);
17 if (! strncmp(spec[i], ce->name, speclen) &&
18 speclen <= namelen &&
19 (ce->name[speclen] == 0 ||
20 ce->name[speclen] == '/'))
21 return 1;
22 }
23 return 0;
24}
25
e83c5163
LT
26int main(int argc, char **argv)
27{
e2e5e98a 28 int silent = 0;
ca2a0798 29 int silent_on_nonexisting_files = 0;
d94c6128 30 int machine_readable = 0;
4789576e 31 int reverse = 0;
e83c5163
LT
32 int entries = read_cache();
33 int i;
34
b8f80925 35 while (1 < argc && argv[1][0] == '-') {
4789576e
JH
36 if (!strcmp(argv[1], "-R"))
37 reverse = 1;
38 else if (!strcmp(argv[1], "-s"))
ca2a0798 39 silent_on_nonexisting_files = silent = 1;
b8f80925 40 else if (!strcmp(argv[1], "-q"))
ca2a0798 41 silent_on_nonexisting_files = 1;
d2522a65 42 else if (!strcmp(argv[1], "-z"))
d94c6128 43 machine_readable = 1;
b8f80925
JH
44 else
45 usage(show_diff_usage);
46 argv++; argc--;
e2e5e98a
PB
47 }
48
b8f80925
JH
49 /* At this point, if argc == 1, then we are doing everything.
50 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
51 */
e83c5163
LT
52 if (entries < 0) {
53 perror("read_cache");
54 exit(1);
55 }
be3cfa85 56
e83c5163
LT
57 for (i = 0; i < entries; i++) {
58 struct stat st;
59 struct cache_entry *ce = active_cache[i];
d94c6128 60 int changed;
e83c5163 61
d2522a65 62 if (1 < argc &&
b8f80925
JH
63 ! matches_pathspec(ce, argv+1, argc-1))
64 continue;
65
9fec8b26
JH
66 if (ce_stage(ce)) {
67 if (machine_readable)
68 printf("U %s%c", ce->name, 0);
69 else
70 printf("%s: Unmerged\n",
71 ce->name);
72 while (i < entries &&
73 !strcmp(ce->name, active_cache[i]->name))
74 i++;
75 i--; /* compensate for loop control increments */
76 continue;
77 }
78
e83c5163 79 if (stat(ce->name, &st) < 0) {
ca2a0798
JH
80 if (errno == ENOENT && silent_on_nonexisting_files)
81 continue;
d94c6128
JH
82 if (machine_readable)
83 printf("X %s%c", ce->name, 0);
84 else {
85 printf("%s: %s\n", ce->name, strerror(errno));
86 if (errno == ENOENT)
be3cfa85 87 show_diff_empty(ce, reverse);
d94c6128 88 }
e83c5163
LT
89 continue;
90 }
734aab75 91 changed = cache_match_stat(ce, &st);
5e76011c 92 if (!changed)
e83c5163 93 continue;
d94c6128
JH
94 if (!machine_readable)
95 printf("%s: %s\n", ce->name, sha1_to_hex(ce->sha1));
96 else {
97 printf("%s %s%c", sha1_to_hex(ce->sha1), ce->name, 0);
98 continue;
99 }
e2e5e98a
PB
100 if (silent)
101 continue;
102
be3cfa85 103 show_differences(ce, reverse);
e83c5163
LT
104 }
105 return 0;
106}