[PATCH] Tweak diffcore-rename heuristics.
[git/haiku.git] / diff-helper.c
blob3c39c96d7e854f784e2c2dd06182fd0ec6e586a6
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include <limits.h>
5 #include "cache.h"
6 #include "strbuf.h"
7 #include "diff.h"
9 static int detect_rename = 0;
10 static int diff_score_opt = 0;
11 static int generate_patch = 1;
12 static const char *pickaxe = NULL;
14 static int parse_oneside_change(const char *cp, int *mode,
15 unsigned char *sha1, char *path)
17 int ch, m;
19 m = 0;
20 while ((ch = *cp) && '0' <= ch && ch <= '7') {
21 m = (m << 3) | (ch - '0');
22 cp++;
24 *mode = m;
25 if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6) &&
26 strncmp(cp, "\ttree\t", 6) && strncmp(cp, " tree ", 6))
27 return -1;
28 cp += 6;
29 if (get_sha1_hex(cp, sha1))
30 return -1;
31 cp += 40;
32 if ((*cp != '\t') && *cp != ' ')
33 return -1;
34 strcpy(path, ++cp);
35 return 0;
38 static int parse_diff_raw_output(const char *buf)
40 char path[PATH_MAX];
41 unsigned char old_sha1[20], new_sha1[20];
42 const char *cp = buf;
43 int ch, old_mode, new_mode;
45 switch (*cp++) {
46 case 'U':
47 diff_unmerge(cp + 1);
48 break;
49 case '+':
50 if (parse_oneside_change(cp, &new_mode, new_sha1, path))
51 return -1;
52 diff_addremove('+', new_mode, new_sha1, path, NULL);
53 break;
54 case '-':
55 if (parse_oneside_change(cp, &old_mode, old_sha1, path))
56 return -1;
57 diff_addremove('-', old_mode, old_sha1, path, NULL);
58 break;
59 case '*':
60 old_mode = new_mode = 0;
61 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
62 old_mode = (old_mode << 3) | (ch - '0');
63 cp++;
65 if (strncmp(cp, "->", 2))
66 return -1;
67 cp += 2;
68 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
69 new_mode = (new_mode << 3) | (ch - '0');
70 cp++;
72 if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6) &&
73 strncmp(cp, "\ttree\t", 6) && strncmp(cp, " tree ", 6))
74 return -1;
75 cp += 6;
76 if (get_sha1_hex(cp, old_sha1))
77 return -1;
78 cp += 40;
79 if (strncmp(cp, "->", 2))
80 return -1;
81 cp += 2;
82 if (get_sha1_hex(cp, new_sha1))
83 return -1;
84 cp += 40;
85 if ((*cp != '\t') && *cp != ' ')
86 return -1;
87 strcpy(path, ++cp);
88 diff_change(old_mode, new_mode, old_sha1, new_sha1, path, NULL);
89 break;
90 default:
91 return -1;
93 return 0;
96 static const char *diff_helper_usage =
97 "git-diff-helper [-z] [-R] [-M] [-C] [-S<string>] paths...";
99 int main(int ac, const char **av) {
100 struct strbuf sb;
101 int reverse = 0;
102 int line_termination = '\n';
104 strbuf_init(&sb);
106 while (1 < ac && av[1][0] == '-') {
107 if (av[1][1] == 'R')
108 reverse = 1;
109 else if (av[1][1] == 'z')
110 line_termination = 0;
111 else if (av[1][1] == 'p') /* hidden from the help */
112 generate_patch = 0;
113 else if (av[1][1] == 'M') {
114 detect_rename = 1;
115 diff_score_opt = diff_scoreopt_parse(av[1]);
117 else if (av[1][1] == 'C') {
118 detect_rename = 2;
119 diff_score_opt = diff_scoreopt_parse(av[1]);
121 else if (av[1][1] == 'S') {
122 pickaxe = av[1] + 2;
124 else
125 usage(diff_helper_usage);
126 ac--; av++;
128 /* the remaining parameters are paths patterns */
130 diff_setup(detect_rename, diff_score_opt, pickaxe,
131 reverse, (generate_patch ? -1 : line_termination),
132 av+1, ac-1);
134 while (1) {
135 int status;
136 read_line(&sb, stdin, line_termination);
137 if (sb.eof)
138 break;
139 status = parse_diff_raw_output(sb.buf);
140 if (status) {
141 diff_flush();
142 printf("%s%c", sb.buf, line_termination);
146 diff_flush();
147 return 0;