5
5
#include "strbuf.h"
6
6
#include "diff.h"
7
7
8
- static int detect_rename = 0 ;
9
- static int diff_score_opt = 0 ;
10
8
static const char * pickaxe = NULL ;
11
- static int diff_output_style = DIFF_FORMAT_PATCH ;
12
9
static int line_termination = '\n' ;
13
10
static int inter_name_termination = '\t' ;
14
11
15
- static int parse_diff_raw (char * buf1 , char * buf2 , char * buf3 )
16
- {
17
- char old_path [PATH_MAX ];
18
- unsigned char old_sha1 [20 ], new_sha1 [20 ];
19
- char * ep ;
20
- char * cp = buf1 ;
21
- int ch , old_mode , new_mode ;
22
-
23
- old_mode = new_mode = 0 ;
24
- while ((ch = * cp ) && ('0' <= ch && ch <= '7' )) {
25
- old_mode = (old_mode << 3 ) | (ch - '0' );
26
- cp ++ ;
27
- }
28
- if (* cp ++ != ' ' )
29
- return -1 ;
30
- while ((ch = * cp ) && ('0' <= ch && ch <= '7' )) {
31
- new_mode = (new_mode << 3 ) | (ch - '0' );
32
- cp ++ ;
33
- }
34
- if (* cp ++ != ' ' )
35
- return -1 ;
36
- if (get_sha1_hex (cp , old_sha1 ))
37
- return -1 ;
38
- cp += 40 ;
39
- if (* cp ++ != ' ' )
40
- return -1 ;
41
- if (get_sha1_hex (cp , new_sha1 ))
42
- return -1 ;
43
- cp += 40 ;
44
- if (* cp ++ != inter_name_termination )
45
- return -1 ;
46
- if (buf2 )
47
- cp = buf2 ;
48
- ep = strchr (cp , inter_name_termination );
49
- if (!ep )
50
- return -1 ;
51
- * ep ++ = 0 ;
52
- strcpy (old_path , cp );
53
- diff_guif (old_mode , new_mode , old_sha1 , new_sha1 ,
54
- old_path , buf3 ? buf3 : ep );
55
- return 0 ;
56
- }
57
-
58
12
static const char * diff_helper_usage =
59
- "git-diff-helper [-z] [-R] [-M] [-C] [- S<string>] paths..." ;
13
+ "git-diff-helper [-z] [-S<string>] paths..." ;
60
14
61
15
int main (int ac , const char * * av ) {
62
- struct strbuf sb1 , sb2 , sb3 ;
63
- int reverse_diff = 0 ;
16
+ struct strbuf sb ;
64
17
65
- strbuf_init (& sb1 );
66
- strbuf_init (& sb2 );
67
- strbuf_init (& sb3 );
18
+ strbuf_init (& sb );
68
19
69
20
while (1 < ac && av [1 ][0 ] == '-' ) {
70
- if (av [1 ][1 ] == 'R' )
71
- reverse_diff = 1 ;
72
- else if (av [1 ][1 ] == 'z' )
21
+ if (av [1 ][1 ] == 'z' )
73
22
line_termination = inter_name_termination = 0 ;
74
- else if (av [1 ][1 ] == 'p' ) /* hidden from the help */
75
- diff_output_style = DIFF_FORMAT_HUMAN ;
76
- else if (av [1 ][1 ] == 'P' ) /* hidden from the help */
77
- diff_output_style = DIFF_FORMAT_MACHINE ;
78
- else if (av [1 ][1 ] == 'M' ) {
79
- detect_rename = DIFF_DETECT_RENAME ;
80
- diff_score_opt = diff_scoreopt_parse (av [1 ]);
81
- }
82
- else if (av [1 ][1 ] == 'C' ) {
83
- detect_rename = DIFF_DETECT_COPY ;
84
- diff_score_opt = diff_scoreopt_parse (av [1 ]);
85
- }
86
23
else if (av [1 ][1 ] == 'S' ) {
87
24
pickaxe = av [1 ] + 2 ;
88
25
}
@@ -92,45 +29,114 @@ int main(int ac, const char **av) {
92
29
}
93
30
/* the remaining parameters are paths patterns */
94
31
95
- diff_setup (reverse_diff );
32
+ diff_setup (0 );
96
33
while (1 ) {
97
- int status ;
98
- read_line (& sb1 , stdin , line_termination );
99
- if (sb1 .eof )
34
+ unsigned old_mode , new_mode ;
35
+ unsigned char old_sha1 [20 ], new_sha1 [20 ];
36
+ char old_path [PATH_MAX ];
37
+ int status , score , two_paths ;
38
+ char new_path [PATH_MAX ];
39
+
40
+ int ch ;
41
+ char * cp , * ep ;
42
+
43
+ read_line (& sb , stdin , line_termination );
44
+ if (sb .eof )
100
45
break ;
101
- switch (sb1 .buf [0 ]) {
102
- case 'U' :
103
- diff_unmerge (sb1 .buf + 2 );
104
- continue ;
46
+ switch (sb .buf [0 ]) {
105
47
case ':' :
106
- break ;
107
- default :
108
- goto unrecognized ;
109
- }
110
- if (!line_termination ) {
111
- read_line (& sb2 , stdin , line_termination );
112
- if (sb2 .eof )
48
+ /* parse the first part up to the status */
49
+ cp = sb .buf + 1 ;
50
+ old_mode = new_mode = 0 ;
51
+ while ((ch = * cp ) && ('0' <= ch && ch <= '7' )) {
52
+ old_mode = (old_mode << 3 ) | (ch - '0' );
53
+ cp ++ ;
54
+ }
55
+ if (* cp ++ != ' ' )
113
56
break ;
114
- read_line (& sb3 , stdin , line_termination );
115
- if (sb3 .eof )
57
+ while ((ch = * cp ) && ('0' <= ch && ch <= '7' )) {
58
+ new_mode = (new_mode << 3 ) | (ch - '0' );
59
+ cp ++ ;
60
+ }
61
+ if (* cp ++ != ' ' )
116
62
break ;
117
- status = parse_diff_raw (sb1 .buf + 1 , sb2 .buf , sb3 .buf );
118
- }
119
- else
120
- status = parse_diff_raw (sb1 .buf + 1 , NULL , NULL );
121
- if (status ) {
122
- unrecognized :
123
- diff_flush (diff_output_style );
124
- printf ("%s\n" , sb1 .buf );
63
+ if (get_sha1_hex (cp , old_sha1 ))
64
+ break ;
65
+ cp += 40 ;
66
+ if (* cp ++ != ' ' )
67
+ break ;
68
+ if (get_sha1_hex (cp , new_sha1 ))
69
+ break ;
70
+ cp += 40 ;
71
+ if (* cp ++ != ' ' )
72
+ break ;
73
+ status = * cp ++ ;
74
+ if (!strchr ("MCRNDU" , status ))
75
+ break ;
76
+ two_paths = score = 0 ;
77
+ if (status == 'R' || status == 'C' ) {
78
+ two_paths = 1 ;
79
+ sscanf (cp , "%d" , & score );
80
+ if (line_termination ) {
81
+ cp = strchr (cp ,
82
+ inter_name_termination );
83
+ if (!cp )
84
+ break ;
85
+ }
86
+ }
87
+
88
+ if (* cp ++ != inter_name_termination )
89
+ break ;
90
+
91
+ /* first pathname */
92
+ if (!line_termination ) {
93
+ read_line (& sb , stdin , line_termination );
94
+ if (sb .eof )
95
+ break ;
96
+ strcpy (old_path , sb .buf );
97
+ }
98
+ else if (!two_paths )
99
+ strcpy (old_path , cp );
100
+ else {
101
+ ep = strchr (cp , inter_name_termination );
102
+ if (!ep )
103
+ break ;
104
+ strncpy (old_path , cp , ep - cp );
105
+ old_path [ep - cp ] = 0 ;
106
+ cp = ep + 1 ;
107
+ }
108
+
109
+ /* second pathname */
110
+ if (!two_paths )
111
+ strcpy (new_path , old_path );
112
+ else {
113
+ if (!line_termination ) {
114
+ read_line (& sb , stdin ,
115
+ line_termination );
116
+ if (sb .eof )
117
+ break ;
118
+ strcpy (new_path , sb .buf );
119
+ }
120
+ else
121
+ strcpy (new_path , cp );
122
+ }
123
+ diff_helper_input (old_mode , new_mode ,
124
+ old_sha1 , new_sha1 ,
125
+ old_path , status , score ,
126
+ new_path );
127
+ continue ;
125
128
}
129
+ if (pickaxe )
130
+ diffcore_pickaxe (pickaxe );
131
+ if (1 < ac )
132
+ diffcore_pathspec (av + 1 );
133
+ diff_flush (DIFF_FORMAT_PATCH , 0 );
134
+ printf ("%s\n" , sb .buf );
126
135
}
127
- if (detect_rename )
128
- diffcore_rename (detect_rename , diff_score_opt );
129
- diffcore_prune ();
130
136
if (pickaxe )
131
137
diffcore_pickaxe (pickaxe );
132
- if (ac )
138
+ if (1 < ac )
133
139
diffcore_pathspec (av + 1 );
134
- diff_flush (diff_output_style );
140
+ diff_flush (DIFF_FORMAT_PATCH , 0 );
135
141
return 0 ;
136
142
}
0 commit comments