2005-04-26 20:25:05 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Junio C Hamano
|
|
|
|
*/
|
2005-04-26 05:26:45 +04:00
|
|
|
#include "cache.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "diff.h"
|
|
|
|
|
2005-05-18 10:29:49 +04:00
|
|
|
static int detect_rename = 0;
|
2005-05-20 06:00:36 +04:00
|
|
|
static int diff_score_opt = 0;
|
2005-05-22 02:02:51 +04:00
|
|
|
static const char *pickaxe = NULL;
|
2005-05-22 06:42:18 +04:00
|
|
|
static int diff_output_style = DIFF_FORMAT_PATCH;
|
|
|
|
static int line_termination = '\n';
|
|
|
|
static int inter_name_termination = '\t';
|
2005-05-18 10:29:49 +04:00
|
|
|
|
2005-05-22 06:42:18 +04:00
|
|
|
static int parse_diff_raw(char *buf1, char *buf2, char *buf3)
|
2005-04-26 20:25:05 +04:00
|
|
|
{
|
2005-05-22 06:42:18 +04: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 20:25:05 +04:00
|
|
|
|
2005-05-22 06:42:18 +04: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 20:25:05 +04:00
|
|
|
cp++;
|
|
|
|
}
|
2005-05-22 06:42:18 +04:00
|
|
|
if (*cp++ != ' ')
|
2005-04-26 05:26:45 +04:00
|
|
|
return -1;
|
2005-05-22 06:42:18 +04:00
|
|
|
if (get_sha1_hex(cp, old_sha1))
|
2005-04-26 05:26:45 +04:00
|
|
|
return -1;
|
|
|
|
cp += 40;
|
2005-05-22 06:42:18 +04:00
|
|
|
if (*cp++ != ' ')
|
2005-04-26 05:26:45 +04:00
|
|
|
return -1;
|
2005-05-22 06:42:18 +04:00
|
|
|
if (get_sha1_hex(cp, new_sha1))
|
2005-04-28 21:13:01 +04:00
|
|
|
return -1;
|
2005-05-22 06:42:18 +04: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 05:26:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-14 05:41:36 +04:00
|
|
|
static const char *diff_helper_usage =
|
2005-05-21 13:40:01 +04:00
|
|
|
"git-diff-helper [-z] [-R] [-M] [-C] [-S<string>] paths...";
|
2005-04-26 05:26:45 +04:00
|
|
|
|
2005-04-28 21:13:01 +04:00
|
|
|
int main(int ac, const char **av) {
|
2005-05-22 06:42:18 +04:00
|
|
|
struct strbuf sb1, sb2, sb3;
|
|
|
|
int reverse_diff = 0;
|
2005-04-26 05:26:45 +04:00
|
|
|
|
2005-05-22 06:42:18 +04:00
|
|
|
strbuf_init(&sb1);
|
|
|
|
strbuf_init(&sb2);
|
|
|
|
strbuf_init(&sb3);
|
2005-04-26 05:26:45 +04:00
|
|
|
|
|
|
|
while (1 < ac && av[1][0] == '-') {
|
|
|
|
if (av[1][1] == 'R')
|
2005-05-22 06:42:18 +04:00
|
|
|
reverse_diff = 1;
|
2005-04-26 05:26:45 +04:00
|
|
|
else if (av[1][1] == 'z')
|
2005-05-22 06:42:18 +04:00
|
|
|
line_termination = inter_name_termination = 0;
|
2005-05-21 13:39:09 +04:00
|
|
|
else if (av[1][1] == 'p') /* hidden from the help */
|
2005-05-22 06:42:18 +04: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 06:00:36 +04:00
|
|
|
else if (av[1][1] == 'M') {
|
2005-05-22 21:04:37 +04:00
|
|
|
detect_rename = DIFF_DETECT_RENAME;
|
2005-05-20 06:00:36 +04:00
|
|
|
diff_score_opt = diff_scoreopt_parse(av[1]);
|
|
|
|
}
|
2005-05-21 13:39:09 +04:00
|
|
|
else if (av[1][1] == 'C') {
|
2005-05-22 21:04:37 +04:00
|
|
|
detect_rename = DIFF_DETECT_COPY;
|
2005-05-21 13:39:09 +04:00
|
|
|
diff_score_opt = diff_scoreopt_parse(av[1]);
|
|
|
|
}
|
2005-05-21 13:40:01 +04:00
|
|
|
else if (av[1][1] == 'S') {
|
|
|
|
pickaxe = av[1] + 2;
|
|
|
|
}
|
2005-04-26 05:26:45 +04:00
|
|
|
else
|
2005-05-14 05:41:36 +04:00
|
|
|
usage(diff_helper_usage);
|
2005-04-26 05:26:45 +04:00
|
|
|
ac--; av++;
|
|
|
|
}
|
|
|
|
/* the remaining parameters are paths patterns */
|
|
|
|
|
2005-05-22 21:04:37 +04:00
|
|
|
diff_setup(reverse_diff);
|
2005-04-26 05:26:45 +04:00
|
|
|
while (1) {
|
2005-04-27 04:39:01 +04:00
|
|
|
int status;
|
2005-05-22 06:42:18 +04:00
|
|
|
read_line(&sb1, stdin, line_termination);
|
|
|
|
if (sb1.eof)
|
2005-04-26 05:26:45 +04:00
|
|
|
break;
|
2005-05-22 06:42:18 +04: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 22:33:46 +04:00
|
|
|
if (status) {
|
2005-05-22 06:42:18 +04:00
|
|
|
unrecognized:
|
2005-05-22 21:04:37 +04:00
|
|
|
diff_flush(diff_output_style);
|
2005-05-22 06:42:18 +04:00
|
|
|
printf("%s%c", sb1.buf, line_termination);
|
2005-05-18 22:33:46 +04:00
|
|
|
}
|
2005-04-26 05:26:45 +04:00
|
|
|
}
|
2005-05-22 06:40:36 +04:00
|
|
|
if (detect_rename)
|
2005-05-22 21:04:37 +04:00
|
|
|
diffcore_rename(detect_rename, diff_score_opt);
|
|
|
|
diffcore_prune();
|
2005-05-22 06:40:36 +04:00
|
|
|
if (pickaxe)
|
2005-05-22 21:04:37 +04:00
|
|
|
diffcore_pickaxe(pickaxe);
|
|
|
|
if (ac)
|
|
|
|
diffcore_pathspec(av + 1);
|
|
|
|
diff_flush(diff_output_style);
|
2005-04-26 05:26:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|