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