@matheusbsbs / matheus-bagliano / commits / f1af60bdba
Fork 0

Support 'diff=pgm' attribute

This enhances the attributes mechanism so that external programs meant for existing GIT_EXTERNAL_DIFF interface can be specifed per path. To configure such a custom diff driver, first define a custom diff driver in the configuration: [diff "my-c-diff"] command = <<your command string comes here>> Then mark the paths that you want to use this custom driver using the attribute mechanism. *.c diff=my-c-diff The intent of this separation is that the attribute mechanism is used for specifying the type of the contents, while the configuration mechanism is used to define what needs to be done to that type of the contents, which would be specific to both platform and personal taste. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 22, 2007 at 17:52 UTC f1af60bdba465779df92090ed370988f202ff043
5 files changed +182 -5
builtin-diff.c
+1
@@ -225,6 +225,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
225 if (diff_setup_done(&rev.diffopt) < 0)
226 die("diff_setup_done failed");
227 }
228 + rev.diffopt.allow_external = 1;
229
230 /* Do we have --cached and not have a pending object, then
231 * default to HEAD by hand. Eek.
combine-diff.c
+1
@@ -943,6 +943,7 @@ void diff_tree_combined(const unsigned char *sha1,
943 diffopts = *opt;
944 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
945 diffopts.recursive = 1;
946 + diffopts.allow_external = 0;
947
948 show_log_first = !!rev->loginfo && !rev->no_commit_id;
949 needsep = 0;
diff.c
+82 -5
@@ -52,6 +52,49 @@ static int parse_diff_color_slot(const char *var, int ofs)
52 die("bad config variable '%s'", var);
53 }
54
55 +static struct ll_diff_driver {
56 + const char *name;
57 + struct ll_diff_driver *next;
58 + char *cmd;
59 +} *user_diff, **user_diff_tail;
60 +
61 +/*
62 + * Currently there is only "diff.<drivername>.command" variable;
63 + * because there are "diff.color.<slot>" variables, we are parsing
64 + * this in a bit convoluted way to allow low level diff driver
65 + * called "color".
66 + */
67 +static int parse_lldiff_command(const char *var, const char *ep, const char *value)
68 +{
69 + const char *name;
70 + int namelen;
71 + struct ll_diff_driver *drv;
72 +
73 + name = var + 5;
74 + namelen = ep - name;
75 + for (drv = user_diff; drv; drv = drv->next)
76 + if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
77 + break;
78 + if (!drv) {
79 + char *namebuf;
80 + drv = xcalloc(1, sizeof(struct ll_diff_driver));
81 + namebuf = xmalloc(namelen + 1);
82 + memcpy(namebuf, name, namelen);
83 + namebuf[namelen] = 0;
84 + drv->name = namebuf;
85 + drv->next = NULL;
86 + if (!user_diff_tail)
87 + user_diff_tail = &user_diff;
88 + *user_diff_tail = drv;
89 + user_diff_tail = &(drv->next);
90 + }
91 +
92 + if (!value)
93 + return error("%s: lacks value", var);
94 + drv->cmd = strdup(value);
95 + return 0;
96 +}
97 +
98 /*
99 * These are to give UI layer defaults.
100 * The core-level commands such as git-diff-files should
@@ -78,11 +121,18 @@ int git_diff_ui_config(const char *var, const char *value)
121 diff_detect_rename_default = DIFF_DETECT_RENAME;
122 return 0;
123 }
124 + if (!prefixcmp(var, "diff.")) {
125 + const char *ep = strrchr(var, '.');
126 +
127 + if (ep != var + 4 && !strcmp(ep, ".command"))
128 + return parse_lldiff_command(var, ep, value);
129 + }
130 if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
131 int slot = parse_diff_color_slot(var, 11);
132 color_parse(value, var, diff_colors[slot]);
133 return 0;
134 }
135 +
136 return git_default_config(var, value);
137 }
138
@@ -1074,11 +1124,6 @@ static int file_is_binary(struct diff_filespec *one)
1124 return 0;
1125 else if (ATTR_FALSE(value))
1126 return 1;
1077 - else if (ATTR_UNSET(value))
1078 - ;
1079 - else
1080 - die("unknown value %s given to 'diff' attribute",
1081 - value);
1127 }
1128
1129 if (!one->data) {
@@ -1752,6 +1797,30 @@ static void run_external_diff(const char *pgm,
1797 }
1798 }
1799
1800 +static const char *external_diff_attr(const char *name)
1801 +{
1802 + struct git_attr_check attr_diff_check;
1803 +
1804 + setup_diff_attr_check(&attr_diff_check);
1805 + if (!git_checkattr(name, 1, &attr_diff_check)) {
1806 + const char *value = attr_diff_check.value;
1807 + if (!ATTR_TRUE(value) &&
1808 + !ATTR_FALSE(value) &&
1809 + !ATTR_UNSET(value)) {
1810 + struct ll_diff_driver *drv;
1811 +
1812 + if (!user_diff_tail) {
1813 + user_diff_tail = &user_diff;
1814 + git_config(git_diff_ui_config);
1815 + }
1816 + for (drv = user_diff; drv; drv = drv->next)
1817 + if (!strcmp(drv->name, value))
1818 + return drv->cmd;
1819 + }
1820 + }
1821 + return NULL;
1822 +}
1823 +
1824 static void run_diff_cmd(const char *pgm,
1825 const char *name,
1826 const char *other,
@@ -1761,6 +1830,14 @@ static void run_diff_cmd(const char *pgm,
1830 struct diff_options *o,
1831 int complete_rewrite)
1832 {
1833 + if (!o->allow_external)
1834 + pgm = NULL;
1835 + else {
1836 + const char *cmd = external_diff_attr(name);
1837 + if (cmd)
1838 + pgm = cmd;
1839 + }
1840 +
1841 if (pgm) {
1842 run_external_diff(pgm, name, other, one, two, xfrm_msg,
1843 complete_rewrite);
diff.h
+1
@@ -59,6 +59,7 @@ struct diff_options {
59 color_diff_words:1,
60 has_changes:1,
61 quiet:1,
62 + allow_external:1,
63 exit_with_status:1;
64 int context;
65 int break_opt;
t/t4020-diff-external.sh new
+97
@@ -0,0 +1,97 @@
1 +#!/bin/sh
2 +
3 +test_description='external diff interface test'
4 +
5 +. ./test-lib.sh
6 +
7 +_z40=0000000000000000000000000000000000000000
8 +
9 +test_expect_success setup '
10 +
11 + test_tick &&
12 + echo initial >file &&
13 + git add file &&
14 + git commit -m initial &&
15 +
16 + test_tick &&
17 + echo second >file &&
18 + git add file &&
19 + git commit -m second &&
20 +
21 + test_tick &&
22 + echo third >file
23 +'
24 +
25 +test_expect_success 'GIT_EXTERNAL_DIFF environment' '
26 +
27 + GIT_EXTERNAL_DIFF=echo git diff | {
28 + read path oldfile oldhex oldmode newfile newhex newmode &&
29 + test "z$path" = zfile &&
30 + test "z$oldmode" = z100644 &&
31 + test "z$newhex" = "z$_z40" &&
32 + test "z$newmode" = z100644 &&
33 + oh=$(git rev-parse --verify HEAD:file) &&
34 + test "z$oh" = "z$oldhex"
35 + }
36 +
37 +'
38 +
39 +test_expect_success 'GIT_EXTERNAL_DIFF environment should apply only to diff' '
40 +
41 + GIT_EXTERNAL_DIFF=echo git log -p -1 HEAD |
42 + grep "^diff --git a/file b/file"
43 +
44 +'
45 +
46 +test_expect_success 'diff attribute' '
47 +
48 + git config diff.parrot.command echo &&
49 +
50 + echo >.gitattributes "file diff=parrot" &&
51 +
52 + git diff | {
53 + read path oldfile oldhex oldmode newfile newhex newmode &&
54 + test "z$path" = zfile &&
55 + test "z$oldmode" = z100644 &&
56 + test "z$newhex" = "z$_z40" &&
57 + test "z$newmode" = z100644 &&
58 + oh=$(git rev-parse --verify HEAD:file) &&
59 + test "z$oh" = "z$oldhex"
60 + }
61 +
62 +'
63 +
64 +test_expect_success 'diff attribute should apply only to diff' '
65 +
66 + git log -p -1 HEAD |
67 + grep "^diff --git a/file b/file"
68 +
69 +'
70 +
71 +test_expect_success 'diff attribute' '
72 +
73 + git config --unset diff.parrot.command &&
74 + git config diff.color.command echo &&
75 +
76 + echo >.gitattributes "file diff=color" &&
77 +
78 + git diff | {
79 + read path oldfile oldhex oldmode newfile newhex newmode &&
80 + test "z$path" = zfile &&
81 + test "z$oldmode" = z100644 &&
82 + test "z$newhex" = "z$_z40" &&
83 + test "z$newmode" = z100644 &&
84 + oh=$(git rev-parse --verify HEAD:file) &&
85 + test "z$oh" = "z$oldhex"
86 + }
87 +
88 +'
89 +
90 +test_expect_success 'diff attribute should apply only to diff' '
91 +
92 + git log -p -1 HEAD |
93 + grep "^diff --git a/file b/file"
94 +
95 +'
96 +
97 +test_done