diff.c: don't add extra '/' to pathname
[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"
4a6bf9e1 7#include "diff.h"
c0fb976a 8
4a6bf9e1 9static const char *show_diff_usage = "show-diff [-q] [-s] [-z] [-p] [paths...]";
b8f80925 10
4a6bf9e1 11static int generate_patch = 0;
0a7668e9
LT
12static int line_termination = '\n';
13static int silent = 0;
14static int silent_on_nonexisting_files = 0;
15
b8f80925
JH
16static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
17{
18 int i;
19 int namelen = ce_namelen(ce);
20 for (i = 0; i < cnt; i++) {
21 int speclen = strlen(spec[i]);
22 if (! strncmp(spec[i], ce->name, speclen) &&
23 speclen <= namelen &&
24 (ce->name[speclen] == 0 ||
25 ce->name[speclen] == '/'))
26 return 1;
27 }
28 return 0;
29}
30
4a6bf9e1 31static void show_unmerge(const char *path)
0a7668e9 32{
4a6bf9e1
JH
33 if (generate_patch)
34 diff_unmerge(path);
35 else
36 printf("U %s%c", path, line_termination);
37}
38
39static void show_file(int pfx, struct cache_entry *ce)
40{
41 if (generate_patch)
42 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1,
43 ce->name, NULL);
44 else
45 printf("%c%06o\t%s\t%s\t%s%c",
46 pfx, ntohl(ce->ce_mode), "blob",
47 sha1_to_hex(ce->sha1), ce->name, line_termination);
48}
49
50static void show_modified(int oldmode, int mode,
51 const char *old_sha1, const char *sha1,
52 char *path)
53{
54 char old_sha1_hex[41];
55 strcpy(old_sha1_hex, sha1_to_hex(old_sha1));
56
57 if (generate_patch)
58 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
59 else
60 printf("*%06o->%06o\tblob\t%s->%s\t%s%c",
61 oldmode, mode, old_sha1_hex, sha1_to_hex(sha1), path,
62 line_termination);
0a7668e9
LT
63}
64
e83c5163
LT
65int main(int argc, char **argv)
66{
4a6bf9e1 67 static const char null_sha1[20] = { 0, };
e83c5163
LT
68 int entries = read_cache();
69 int i;
70
b8f80925 71 while (1 < argc && argv[1][0] == '-') {
0a7668e9 72 if (!strcmp(argv[1], "-s"))
ca2a0798 73 silent_on_nonexisting_files = silent = 1;
4a6bf9e1
JH
74 else if (!strcmp(argv[1], "-p"))
75 generate_patch = 1;
b8f80925 76 else if (!strcmp(argv[1], "-q"))
ca2a0798 77 silent_on_nonexisting_files = 1;
d2522a65 78 else if (!strcmp(argv[1], "-z"))
0a7668e9
LT
79 line_termination = 0;
80 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 81 ; /* no-op */
b8f80925
JH
82 else
83 usage(show_diff_usage);
84 argv++; argc--;
e2e5e98a
PB
85 }
86
b8f80925
JH
87 /* At this point, if argc == 1, then we are doing everything.
88 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
89 */
e83c5163
LT
90 if (entries < 0) {
91 perror("read_cache");
92 exit(1);
93 }
be3cfa85 94
e83c5163
LT
95 for (i = 0; i < entries; i++) {
96 struct stat st;
0a7668e9 97 unsigned int oldmode, mode;
e83c5163 98 struct cache_entry *ce = active_cache[i];
d94c6128 99 int changed;
e83c5163 100
d2522a65 101 if (1 < argc &&
b8f80925
JH
102 ! matches_pathspec(ce, argv+1, argc-1))
103 continue;
104
9fec8b26 105 if (ce_stage(ce)) {
4a6bf9e1 106 show_unmerge(ce->name);
9fec8b26
JH
107 while (i < entries &&
108 !strcmp(ce->name, active_cache[i]->name))
109 i++;
110 i--; /* compensate for loop control increments */
111 continue;
112 }
113
e83c5163 114 if (stat(ce->name, &st) < 0) {
0a7668e9
LT
115 if (errno != ENOENT) {
116 perror(ce->name);
ca2a0798 117 continue;
0a7668e9
LT
118 }
119 if (silent_on_nonexisting_files)
120 continue;
4a6bf9e1 121 show_file('-', ce);
e83c5163
LT
122 continue;
123 }
734aab75 124 changed = cache_match_stat(ce, &st);
5e76011c 125 if (!changed)
e83c5163 126 continue;
e2e5e98a 127
0a7668e9
LT
128 oldmode = ntohl(ce->ce_mode);
129 mode = S_IFREG | ce_permissions(st.st_mode);
130
4a6bf9e1
JH
131 show_modified(oldmode, mode, ce->sha1, null_sha1,
132 ce->name);
e83c5163
LT
133 }
134 return 0;
135}