2005-04-26 16:25:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Junio C Hamano
|
|
|
|
*/
|
2005-05-07 08:41:41 +00:00
|
|
|
#include <limits.h>
|
2005-04-26 01:26:45 +00:00
|
|
|
#include "cache.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "diff.h"
|
|
|
|
|
2005-05-18 06:29:49 +00:00
|
|
|
static int detect_rename = 0;
|
2005-05-20 02:00:36 +00:00
|
|
|
static int diff_score_opt = 0;
|
2005-05-18 06:29:49 +00:00
|
|
|
|
2005-05-19 10:32:35 +00:00
|
|
|
static int parse_oneside_change(const char *cp, int *mode,
|
|
|
|
unsigned char *sha1, char *path)
|
2005-04-26 16:25:05 +00:00
|
|
|
{
|
2005-05-19 10:32:35 +00:00
|
|
|
int ch, m;
|
2005-04-26 16:25:05 +00:00
|
|
|
|
2005-05-19 10:32:35 +00:00
|
|
|
m = 0;
|
2005-04-26 16:25:05 +00:00
|
|
|
while ((ch = *cp) && '0' <= ch && ch <= '7') {
|
2005-05-19 10:32:35 +00:00
|
|
|
m = (m << 3) | (ch - '0');
|
2005-04-26 16:25:05 +00:00
|
|
|
cp++;
|
|
|
|
}
|
2005-05-19 10:32:35 +00:00
|
|
|
*mode = m;
|
2005-05-20 02:00:36 +00:00
|
|
|
if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6))
|
2005-04-26 01:26:45 +00:00
|
|
|
return -1;
|
|
|
|
cp += 6;
|
2005-05-19 10:32:35 +00:00
|
|
|
if (get_sha1_hex(cp, sha1))
|
2005-04-26 01:26:45 +00:00
|
|
|
return -1;
|
|
|
|
cp += 40;
|
2005-05-20 02:00:36 +00:00
|
|
|
if ((*cp != '\t') && *cp != ' ')
|
2005-04-26 01:26:45 +00:00
|
|
|
return -1;
|
2005-05-20 02:00:36 +00:00
|
|
|
strcpy(path, ++cp);
|
2005-04-26 01:26:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-19 10:32:35 +00:00
|
|
|
static int parse_diff_raw_output(const char *buf)
|
2005-04-28 17:13:01 +00:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
2005-05-19 10:32:35 +00:00
|
|
|
unsigned char old_sha1[20], new_sha1[20];
|
2005-04-26 01:26:45 +00:00
|
|
|
const char *cp = buf;
|
2005-05-19 10:32:35 +00:00
|
|
|
int ch, old_mode, new_mode;
|
2005-04-26 01:26:45 +00:00
|
|
|
|
|
|
|
switch (*cp++) {
|
2005-04-27 00:39:01 +00:00
|
|
|
case 'U':
|
2005-05-19 10:32:35 +00:00
|
|
|
diff_unmerge(cp + 1);
|
|
|
|
break;
|
2005-04-26 01:26:45 +00:00
|
|
|
case '+':
|
2005-05-19 10:32:35 +00:00
|
|
|
parse_oneside_change(cp, &new_mode, new_sha1, path);
|
|
|
|
diff_addremove('+', new_mode, new_sha1, path, NULL);
|
2005-04-28 17:13:01 +00:00
|
|
|
break;
|
2005-04-26 01:26:45 +00:00
|
|
|
case '-':
|
2005-05-19 10:32:35 +00:00
|
|
|
parse_oneside_change(cp, &old_mode, old_sha1, path);
|
|
|
|
diff_addremove('-', old_mode, old_sha1, path, NULL);
|
2005-04-28 17:13:01 +00:00
|
|
|
break;
|
2005-04-26 01:26:45 +00:00
|
|
|
case '*':
|
2005-05-19 10:32:35 +00:00
|
|
|
old_mode = new_mode = 0;
|
2005-04-28 17:13:01 +00:00
|
|
|
while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
|
2005-05-19 10:32:35 +00:00
|
|
|
old_mode = (old_mode << 3) | (ch - '0');
|
2005-04-28 17:13:01 +00:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
if (strncmp(cp, "->", 2))
|
|
|
|
return -1;
|
|
|
|
cp += 2;
|
|
|
|
while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
|
2005-05-19 10:32:35 +00:00
|
|
|
new_mode = (new_mode << 3) | (ch - '0');
|
2005-04-28 17:13:01 +00:00
|
|
|
cp++;
|
|
|
|
}
|
2005-05-20 02:00:36 +00:00
|
|
|
if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6))
|
2005-04-28 17:13:01 +00:00
|
|
|
return -1;
|
|
|
|
cp += 6;
|
2005-05-19 10:32:35 +00:00
|
|
|
if (get_sha1_hex(cp, old_sha1))
|
2005-04-28 17:13:01 +00:00
|
|
|
return -1;
|
|
|
|
cp += 40;
|
|
|
|
if (strncmp(cp, "->", 2))
|
|
|
|
return -1;
|
|
|
|
cp += 2;
|
2005-05-19 10:32:35 +00:00
|
|
|
if (get_sha1_hex(cp, new_sha1))
|
2005-04-28 17:13:01 +00:00
|
|
|
return -1;
|
|
|
|
cp += 40;
|
2005-05-20 02:00:36 +00:00
|
|
|
if ((*cp != '\t') && *cp != ' ')
|
2005-04-28 17:13:01 +00:00
|
|
|
return -1;
|
2005-05-20 02:00:36 +00:00
|
|
|
strcpy(path, ++cp);
|
2005-05-19 10:32:35 +00:00
|
|
|
diff_change(old_mode, new_mode, old_sha1, new_sha1, path, 0);
|
2005-04-26 01:26:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-04-28 17:13:01 +00:00
|
|
|
return -1;
|
2005-04-26 16:25:05 +00:00
|
|
|
}
|
2005-04-26 01:26:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-14 01:41:36 +00:00
|
|
|
static const char *diff_helper_usage =
|
2005-05-19 10:32:35 +00:00
|
|
|
"git-diff-helper [-z] [-R] [-M] paths...";
|
2005-04-26 01:26:45 +00:00
|
|
|
|
2005-04-28 17:13:01 +00:00
|
|
|
int main(int ac, const char **av) {
|
2005-04-26 01:26:45 +00:00
|
|
|
struct strbuf sb;
|
2005-05-01 00:59:35 +00:00
|
|
|
int reverse = 0;
|
2005-04-26 01:26:45 +00:00
|
|
|
int line_termination = '\n';
|
|
|
|
|
|
|
|
strbuf_init(&sb);
|
|
|
|
|
|
|
|
while (1 < ac && av[1][0] == '-') {
|
|
|
|
if (av[1][1] == 'R')
|
2005-05-01 00:59:35 +00:00
|
|
|
reverse = 1;
|
2005-04-26 01:26:45 +00:00
|
|
|
else if (av[1][1] == 'z')
|
|
|
|
line_termination = 0;
|
2005-05-20 02:00:36 +00:00
|
|
|
else if (av[1][1] == 'M') {
|
2005-05-18 06:29:49 +00:00
|
|
|
detect_rename = 1;
|
2005-05-20 02:00:36 +00:00
|
|
|
diff_score_opt = diff_scoreopt_parse(av[1]);
|
|
|
|
}
|
2005-04-26 01:26:45 +00:00
|
|
|
else
|
2005-05-14 01:41:36 +00:00
|
|
|
usage(diff_helper_usage);
|
2005-04-26 01:26:45 +00:00
|
|
|
ac--; av++;
|
|
|
|
}
|
|
|
|
/* the remaining parameters are paths patterns */
|
|
|
|
|
2005-05-20 02:00:36 +00:00
|
|
|
diff_setup(detect_rename, diff_score_opt, reverse, -1, av+1, ac-1);
|
2005-05-19 10:32:35 +00:00
|
|
|
|
2005-04-26 01:26:45 +00:00
|
|
|
while (1) {
|
2005-04-27 00:39:01 +00:00
|
|
|
int status;
|
2005-04-26 01:26:45 +00:00
|
|
|
read_line(&sb, stdin, line_termination);
|
|
|
|
if (sb.eof)
|
|
|
|
break;
|
2005-05-19 10:32:35 +00:00
|
|
|
status = parse_diff_raw_output(sb.buf);
|
2005-05-18 18:33:46 +00:00
|
|
|
if (status) {
|
2005-05-19 10:32:35 +00:00
|
|
|
diff_flush();
|
2005-05-18 18:33:46 +00:00
|
|
|
printf("%s%c", sb.buf, line_termination);
|
|
|
|
}
|
2005-04-26 01:26:45 +00:00
|
|
|
}
|
2005-05-18 06:29:49 +00:00
|
|
|
|
2005-05-19 10:32:35 +00:00
|
|
|
diff_flush();
|
2005-04-26 01:26:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|