2005-04-26 18:25:05 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Junio C Hamano
|
|
|
|
*/
|
2005-05-07 10:41:41 +02:00
|
|
|
#include <limits.h>
|
2005-04-26 03:26:45 +02:00
|
|
|
#include "cache.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "diff.h"
|
|
|
|
|
2005-05-18 08:29:49 +02:00
|
|
|
static int detect_rename = 0;
|
2005-05-20 04:00:36 +02:00
|
|
|
static int diff_score_opt = 0;
|
2005-05-22 00:02:51 +02:00
|
|
|
static const char *pickaxe = NULL;
|
2005-05-22 04:42:18 +02:00
|
|
|
static int diff_output_style = DIFF_FORMAT_PATCH;
|
|
|
|
static int line_termination = '\n';
|
|
|
|
static int inter_name_termination = '\t';
|
2005-05-18 08:29:49 +02:00
|
|
|
|
2005-05-22 04:42:18 +02:00
|
|
|
static int parse_diff_raw(char *buf1, char *buf2, char *buf3)
|
2005-04-26 18:25:05 +02:00
|
|
|
{
|
2005-05-22 04:42:18 +02:00
|
|
|
char old_path[PATH_MAX];
|
|
|
|
unsigned char old_sha1[20], new_sha1[20];
|
|
|
|
char *ep;
|
|
|
|
char *cp = buf1;
|
|
|
|
int ch, old_mode, new_mode;
|
2005-04-26 18:25:05 +02:00
|
|
|
|
2005-05-22 04:42:18 +02:00
|
|
|
old_mode = new_mode = 0;
|
|
|
|
while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
|
|
|
|
old_mode = (old_mode << 3) | (ch - '0');
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
if (*cp++ != ' ')
|
|
|
|
return -1;
|
|
|
|
while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
|
|
|
|
new_mode = (new_mode << 3) | (ch - '0');
|
2005-04-26 18:25:05 +02:00
|
|
|
cp++;
|
|
|
|
}
|
2005-05-22 04:42:18 +02:00
|
|
|
if (*cp++ != ' ')
|
2005-04-26 03:26:45 +02:00
|
|
|
return -1;
|
2005-05-22 04:42:18 +02:00
|
|
|
if (get_sha1_hex(cp, old_sha1))
|
2005-04-26 03:26:45 +02:00
|
|
|
return -1;
|
|
|
|
cp += 40;
|
2005-05-22 04:42:18 +02:00
|
|
|
if (*cp++ != ' ')
|
2005-04-26 03:26:45 +02:00
|
|
|
return -1;
|
2005-05-22 04:42:18 +02:00
|
|
|
if (get_sha1_hex(cp, new_sha1))
|
2005-04-28 19:13:01 +02:00
|
|
|
return -1;
|
2005-05-22 04:42:18 +02:00
|
|
|
cp += 40;
|
|
|
|
if (*cp++ != inter_name_termination)
|
|
|
|
return -1;
|
|
|
|
if (buf2)
|
|
|
|
cp = buf2;
|
|
|
|
ep = strchr(cp, inter_name_termination);
|
|
|
|
if (!ep)
|
|
|
|
return -1;
|
|
|
|
*ep++ = 0;
|
|
|
|
strcpy(old_path, cp);
|
|
|
|
diff_guif(old_mode, new_mode, old_sha1, new_sha1,
|
|
|
|
old_path, buf3 ? buf3 : ep);
|
2005-04-26 03:26:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-14 03:41:36 +02:00
|
|
|
static const char *diff_helper_usage =
|
2005-05-21 11:40:01 +02:00
|
|
|
"git-diff-helper [-z] [-R] [-M] [-C] [-S<string>] paths...";
|
2005-04-26 03:26:45 +02:00
|
|
|
|
2005-04-28 19:13:01 +02:00
|
|
|
int main(int ac, const char **av) {
|
2005-05-22 04:42:18 +02:00
|
|
|
struct strbuf sb1, sb2, sb3;
|
|
|
|
int reverse_diff = 0;
|
2005-04-26 03:26:45 +02:00
|
|
|
|
2005-05-22 04:42:18 +02:00
|
|
|
strbuf_init(&sb1);
|
|
|
|
strbuf_init(&sb2);
|
|
|
|
strbuf_init(&sb3);
|
2005-04-26 03:26:45 +02:00
|
|
|
|
|
|
|
while (1 < ac && av[1][0] == '-') {
|
|
|
|
if (av[1][1] == 'R')
|
2005-05-22 04:42:18 +02:00
|
|
|
reverse_diff = 1;
|
2005-04-26 03:26:45 +02:00
|
|
|
else if (av[1][1] == 'z')
|
2005-05-22 04:42:18 +02:00
|
|
|
line_termination = inter_name_termination = 0;
|
2005-05-21 11:39:09 +02:00
|
|
|
else if (av[1][1] == 'p') /* hidden from the help */
|
2005-05-22 04:42:18 +02:00
|
|
|
diff_output_style = DIFF_FORMAT_HUMAN;
|
|
|
|
else if (av[1][1] == 'P') /* hidden from the help */
|
|
|
|
diff_output_style = DIFF_FORMAT_MACHINE;
|
2005-05-20 04:00:36 +02:00
|
|
|
else if (av[1][1] == 'M') {
|
2005-05-18 08:29:49 +02:00
|
|
|
detect_rename = 1;
|
2005-05-20 04:00:36 +02:00
|
|
|
diff_score_opt = diff_scoreopt_parse(av[1]);
|
|
|
|
}
|
2005-05-21 11:39:09 +02:00
|
|
|
else if (av[1][1] == 'C') {
|
|
|
|
detect_rename = 2;
|
|
|
|
diff_score_opt = diff_scoreopt_parse(av[1]);
|
|
|
|
}
|
2005-05-21 11:40:01 +02:00
|
|
|
else if (av[1][1] == 'S') {
|
|
|
|
pickaxe = av[1] + 2;
|
|
|
|
}
|
2005-04-26 03:26:45 +02:00
|
|
|
else
|
2005-05-14 03:41:36 +02:00
|
|
|
usage(diff_helper_usage);
|
2005-04-26 03:26:45 +02:00
|
|
|
ac--; av++;
|
|
|
|
}
|
|
|
|
/* the remaining parameters are paths patterns */
|
|
|
|
|
2005-05-22 04:42:18 +02:00
|
|
|
diff_setup(reverse_diff, diff_output_style);
|
2005-04-26 03:26:45 +02:00
|
|
|
while (1) {
|
2005-04-27 02:39:01 +02:00
|
|
|
int status;
|
2005-05-22 04:42:18 +02:00
|
|
|
read_line(&sb1, stdin, line_termination);
|
|
|
|
if (sb1.eof)
|
2005-04-26 03:26:45 +02:00
|
|
|
break;
|
2005-05-22 04:42:18 +02:00
|
|
|
switch (sb1.buf[0]) {
|
|
|
|
case 'U':
|
|
|
|
diff_unmerge(sb1.buf + 2);
|
|
|
|
continue;
|
|
|
|
case ':':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto unrecognized;
|
|
|
|
}
|
|
|
|
if (!line_termination) {
|
|
|
|
read_line(&sb2, stdin, line_termination);
|
|
|
|
if (sb2.eof)
|
|
|
|
break;
|
|
|
|
read_line(&sb3, stdin, line_termination);
|
|
|
|
if (sb3.eof)
|
|
|
|
break;
|
|
|
|
status = parse_diff_raw(sb1.buf+1, sb2.buf, sb3.buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = parse_diff_raw(sb1.buf+1, NULL, NULL);
|
2005-05-18 20:33:46 +02:00
|
|
|
if (status) {
|
2005-05-22 04:42:18 +02:00
|
|
|
unrecognized:
|
2005-05-22 04:40:36 +02:00
|
|
|
diff_flush(av+1, ac-1);
|
2005-05-22 04:42:18 +02:00
|
|
|
printf("%s%c", sb1.buf, line_termination);
|
2005-05-18 20:33:46 +02:00
|
|
|
}
|
2005-04-26 03:26:45 +02:00
|
|
|
}
|
2005-05-22 04:40:36 +02:00
|
|
|
if (detect_rename)
|
|
|
|
diff_detect_rename(detect_rename, diff_score_opt);
|
|
|
|
if (pickaxe)
|
|
|
|
diff_pickaxe(pickaxe);
|
|
|
|
diff_flush(av+1, ac-1);
|
2005-04-26 03:26:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|