[PATCH] Detect renames in diff family.
[git/dscho.git] / diff-helper.c
blob5b0feb1d8a856e3ecf6e81b4c95ea306d2ac5c97
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;
11 static int parse_oneside_change(const char *cp, int *mode,
12 unsigned char *sha1, char *path)
14 int ch, m;
16 m = 0;
17 while ((ch = *cp) && '0' <= ch && ch <= '7') {
18 m = (m << 3) | (ch - '0');
19 cp++;
21 *mode = m;
22 if (strncmp(cp, "\tblob\t", 6))
23 return -1;
24 cp += 6;
25 if (get_sha1_hex(cp, sha1))
26 return -1;
27 cp += 40;
28 if (*cp++ != '\t')
29 return -1;
30 strcpy(path, cp);
31 return 0;
34 static int parse_diff_raw_output(const char *buf)
36 char path[PATH_MAX];
37 unsigned char old_sha1[20], new_sha1[20];
38 const char *cp = buf;
39 int ch, old_mode, new_mode;
41 switch (*cp++) {
42 case 'U':
43 diff_unmerge(cp + 1);
44 break;
45 case '+':
46 parse_oneside_change(cp, &new_mode, new_sha1, path);
47 diff_addremove('+', new_mode, new_sha1, path, NULL);
48 break;
49 case '-':
50 parse_oneside_change(cp, &old_mode, old_sha1, path);
51 diff_addremove('-', old_mode, old_sha1, path, NULL);
52 break;
53 case '*':
54 old_mode = new_mode = 0;
55 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
56 old_mode = (old_mode << 3) | (ch - '0');
57 cp++;
59 if (strncmp(cp, "->", 2))
60 return -1;
61 cp += 2;
62 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
63 new_mode = (new_mode << 3) | (ch - '0');
64 cp++;
66 if (strncmp(cp, "\tblob\t", 6))
67 return -1;
68 cp += 6;
69 if (get_sha1_hex(cp, old_sha1))
70 return -1;
71 cp += 40;
72 if (strncmp(cp, "->", 2))
73 return -1;
74 cp += 2;
75 if (get_sha1_hex(cp, new_sha1))
76 return -1;
77 cp += 40;
78 if (*cp++ != '\t')
79 return -1;
80 strcpy(path, cp);
81 diff_change(old_mode, new_mode, old_sha1, new_sha1, path, 0);
82 break;
83 default:
84 return -1;
86 return 0;
89 static const char *diff_helper_usage =
90 "git-diff-helper [-z] [-R] [-M] paths...";
92 int main(int ac, const char **av) {
93 struct strbuf sb;
94 int reverse = 0;
95 int line_termination = '\n';
97 strbuf_init(&sb);
99 while (1 < ac && av[1][0] == '-') {
100 if (av[1][1] == 'R')
101 reverse = 1;
102 else if (av[1][1] == 'z')
103 line_termination = 0;
104 else if (av[1][1] == 'M')
105 detect_rename = 1;
106 else
107 usage(diff_helper_usage);
108 ac--; av++;
110 /* the remaining parameters are paths patterns */
112 diff_setup(detect_rename, 0, reverse, av+1, ac-1);
114 while (1) {
115 int status;
116 read_line(&sb, stdin, line_termination);
117 if (sb.eof)
118 break;
119 status = parse_diff_raw_output(sb.buf);
120 if (status) {
121 diff_flush();
122 printf("%s%c", sb.buf, line_termination);
126 diff_flush();
127 return 0;