Update the merge scripts for the big git rename.
[git/git.git] / diff-tree-helper.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
d1df5743
JH
4#include "cache.h"
5#include "strbuf.h"
6#include "diff.h"
7
91a6eaa0 8static int matches_pathspec(const char *name, const char **spec, int cnt)
d1df5743
JH
9{
10 int i;
11 int namelen = strlen(name);
12 for (i = 0; i < cnt; i++) {
13 int speclen = strlen(spec[i]);
14 if (! strncmp(spec[i], name, speclen) &&
15 speclen <= namelen &&
16 (name[speclen] == 0 ||
17 name[speclen] == '/'))
18 return 1;
19 }
20 return 0;
21}
22
be3cfa85
JH
23static int parse_oneside_change(const char *cp, struct diff_spec *one,
24 char *path)
25{
d1df5743 26 int ch;
be3cfa85
JH
27
28 one->file_valid = one->sha1_valid = 1;
29 one->mode = 0;
30 while ((ch = *cp) && '0' <= ch && ch <= '7') {
31 one->mode = (one->mode << 3) | (ch - '0');
32 cp++;
33 }
34
d1df5743
JH
35 if (strncmp(cp, "\tblob\t", 6))
36 return -1;
37 cp += 6;
be3cfa85 38 if (get_sha1_hex(cp, one->u.sha1))
d1df5743
JH
39 return -1;
40 cp += 40;
41 if (*cp++ != '\t')
42 return -1;
43 strcpy(path, cp);
44 return 0;
45}
46
91a6eaa0
JH
47static int parse_diff_tree_output(const char *buf, const char **spec, int cnt)
48{
49 struct diff_spec old, new;
50 char path[PATH_MAX];
d1df5743
JH
51 const char *cp = buf;
52 int ch;
d1df5743
JH
53
54 switch (*cp++) {
0b32ff0d 55 case 'U':
91a6eaa0
JH
56 if (!cnt || matches_pathspec(cp + 1, spec, cnt))
57 diff_unmerge(cp + 1);
58 return 0;
d1df5743 59 case '+':
91a6eaa0
JH
60 old.file_valid = 0;
61 parse_oneside_change(cp, &new, path);
62 break;
d1df5743 63 case '-':
91a6eaa0
JH
64 new.file_valid = 0;
65 parse_oneside_change(cp, &old, path);
66 break;
d1df5743 67 case '*':
91a6eaa0
JH
68 old.file_valid = old.sha1_valid =
69 new.file_valid = new.sha1_valid = 1;
70 old.mode = new.mode = 0;
71 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
72 old.mode = (old.mode << 3) | (ch - '0');
73 cp++;
74 }
75 if (strncmp(cp, "->", 2))
76 return -1;
77 cp += 2;
78 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
79 new.mode = (new.mode << 3) | (ch - '0');
80 cp++;
81 }
82 if (strncmp(cp, "\tblob\t", 6))
83 return -1;
84 cp += 6;
85 if (get_sha1_hex(cp, old.u.sha1))
86 return -1;
87 cp += 40;
88 if (strncmp(cp, "->", 2))
89 return -1;
90 cp += 2;
91 if (get_sha1_hex(cp, new.u.sha1))
92 return -1;
93 cp += 40;
94 if (*cp++ != '\t')
95 return -1;
96 strcpy(path, cp);
d1df5743
JH
97 break;
98 default:
91a6eaa0 99 return -1;
be3cfa85 100 }
91a6eaa0
JH
101 if (!cnt || matches_pathspec(path, spec, cnt))
102 run_external_diff(path, &old, &new);
d1df5743
JH
103 return 0;
104}
105
106static const char *diff_tree_helper_usage =
107"diff-tree-helper [-R] [-z] paths...";
108
91a6eaa0 109int main(int ac, const char **av) {
d1df5743
JH
110 struct strbuf sb;
111 int reverse_diff = 0;
112 int line_termination = '\n';
113
114 strbuf_init(&sb);
115
116 while (1 < ac && av[1][0] == '-') {
117 if (av[1][1] == 'R')
118 reverse_diff = 1;
119 else if (av[1][1] == 'z')
120 line_termination = 0;
121 else
122 usage(diff_tree_helper_usage);
123 ac--; av++;
124 }
125 /* the remaining parameters are paths patterns */
126
d1df5743 127 while (1) {
0b32ff0d 128 int status;
d1df5743
JH
129 read_line(&sb, stdin, line_termination);
130 if (sb.eof)
131 break;
91a6eaa0
JH
132 status = parse_diff_tree_output(sb.buf, av+1, ac-1);
133 if (status)
134 fprintf(stderr, "cannot parse %s\n", sb.buf);
d1df5743
JH
135 }
136 return 0;
137}