2 * Copyright (C) 2005 Junio C Hamano
9 static int detect_rename = 0;
10 static int diff_score_opt = 0;
11 static int generate_patch = 1;
13 static int parse_oneside_change(const char *cp, int *mode,
14 unsigned char *sha1, char *path)
19 while ((ch = *cp) && '0' <= ch && ch <= '7') {
20 m = (m << 3) | (ch - '0');
24 if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6) &&
25 strncmp(cp, "\ttree\t", 6) && strncmp(cp, " tree ", 6))
28 if (get_sha1_hex(cp, sha1))
31 if ((*cp != '\t') && *cp != ' ')
37 static int parse_diff_raw_output(const char *buf)
40 unsigned char old_sha1[20], new_sha1[20];
42 int ch, old_mode, new_mode;
49 if (parse_oneside_change(cp, &new_mode, new_sha1, path))
51 diff_addremove('+', new_mode, new_sha1, path, NULL);
54 if (parse_oneside_change(cp, &old_mode, old_sha1, path))
56 diff_addremove('-', old_mode, old_sha1, path, NULL);
59 old_mode = new_mode = 0;
60 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
61 old_mode = (old_mode << 3) | (ch - '0');
64 if (strncmp(cp, "->", 2))
67 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
68 new_mode = (new_mode << 3) | (ch - '0');
71 if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6) &&
72 strncmp(cp, "\ttree\t", 6) && strncmp(cp, " tree ", 6))
75 if (get_sha1_hex(cp, old_sha1))
78 if (strncmp(cp, "->", 2))
81 if (get_sha1_hex(cp, new_sha1))
84 if ((*cp != '\t') && *cp != ' ')
87 diff_change(old_mode, new_mode, old_sha1, new_sha1, path, NULL);
95 static const char *diff_helper_usage =
96 "git-diff-helper [-z] [-R] [-M] [-C] paths...";
98 int main(int ac, const char **av) {
101 int line_termination = '\n';
105 while (1 < ac && av[1][0] == '-') {
108 else if (av[1][1] == 'z')
109 line_termination = 0;
110 else if (av[1][1] == 'p') /* hidden from the help */
112 else if (av[1][1] == 'M') {
114 diff_score_opt = diff_scoreopt_parse(av[1]);
116 else if (av[1][1] == 'C') {
118 diff_score_opt = diff_scoreopt_parse(av[1]);
121 usage(diff_helper_usage);
124 /* the remaining parameters are paths patterns */
126 diff_setup(detect_rename, diff_score_opt, reverse,
127 (generate_patch ? -1 : line_termination),
132 read_line(&sb, stdin, line_termination);
135 status = parse_diff_raw_output(sb.buf);
138 printf("%s%c", sb.buf, line_termination);