git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Pascal Obry <pascal.obry@gmail.com>
Cc: git@vger.kernel.org, pascal@obry.net
Subject: [PATCH] Teach diff machinery to display other prefixes than "a/" and "b/"
Date: Tue, 18 Dec 2007 16:21:45 +0000 (GMT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0712181619550.23902@racer.site> (raw)
In-Reply-To: <1197992574-3464-1-git-send-email-pascal@obry.net>


With the new option "--prefix=<prefix1>[:<prefix2>]" you can change
the shown prefix, or suppress it (by specifying the empty string).

Initial patch by Pascal Obry.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---

	On Tue, 18 Dec 2007, Pascal Obry wrote:

	> This option can be used to generate a patch file
	> where file names are relative to the Git root
	> directory. Such a patch can then be applied with
	> the standard patch tool using option -p0.

	How about this instead?  It is not much longer, but more 
	versatile, as you can actually change the prefix, and not only in 
	format-patch.

	Oh, and if somebody has a better idea for the name of the option, 
	I would appreciate your input.

 Documentation/diff-options.txt |    4 ++++
 diff.c                         |   29 +++++++++++++++++++++--------
 diff.h                         |    1 +
 3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index 9ecc1d7..672a2d0 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -211,5 +211,9 @@ endif::git-format-patch[]
 --no-ext-diff::
 	Disallow external diff drivers.
 
+--prefix=<prefix1>[:<prefix2>]::
+	Show the given path prefixes instead of "a/" and "b/".  Leave
+	it empty to show no prefix at all.
+
 For more detailed explanation on these common options, see also
 link:diffcore.html[diffcore documentation].
diff --git a/diff.c b/diff.c
index e26584c..404ba91 100644
--- a/diff.c
+++ b/diff.c
@@ -290,9 +290,10 @@ static void emit_rewrite_diff(const char *name_a,
 			      const char *name_b,
 			      struct diff_filespec *one,
 			      struct diff_filespec *two,
-			      int color_diff)
+			      struct diff_options *o)
 {
 	int lc_a, lc_b;
+	int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
 	const char *name_a_tab, *name_b_tab;
 	const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
 	const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
@@ -309,9 +310,9 @@ static void emit_rewrite_diff(const char *name_a,
 	diff_populate_filespec(two, 0);
 	lc_a = count_lines(one->data, one->size);
 	lc_b = count_lines(two->data, two->size);
-	printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
-	       metainfo, name_a, name_a_tab, reset,
-	       metainfo, name_b, name_b_tab, reset, fraginfo);
+	printf("%s--- %s%s%s%s\n%s+++ %s%s%s%s\n%s@@ -",
+	       metainfo, o->a_prefix, name_a, name_a_tab, reset,
+	       metainfo, o->b_prefix, name_b, name_b_tab, reset, fraginfo);
 	print_line_count(lc_a);
 	printf(" +");
 	print_line_count(lc_b);
@@ -1212,8 +1213,8 @@ static void builtin_diff(const char *name_a,
 	const char *set = diff_get_color_opt(o, DIFF_METAINFO);
 	const char *reset = diff_get_color_opt(o, DIFF_RESET);
 
-	a_one = quote_two("a/", name_a + (*name_a == '/'));
-	b_two = quote_two("b/", name_b + (*name_b == '/'));
+	a_one = quote_two(o->a_prefix, name_a + (*name_a == '/'));
+	b_two = quote_two(o->b_prefix, name_b + (*name_b == '/'));
 	lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
 	lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
 	printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
@@ -1242,8 +1243,7 @@ static void builtin_diff(const char *name_a,
 		if ((one->mode ^ two->mode) & S_IFMT)
 			goto free_ab_and_return;
 		if (complete_rewrite) {
-			emit_rewrite_diff(name_a, name_b, one, two,
-					DIFF_OPT_TST(o, COLOR_DIFF));
+			emit_rewrite_diff(name_a, name_b, one, two, o);
 			o->found_changes = 1;
 			goto free_ab_and_return;
 		}
@@ -2020,6 +2020,9 @@ void diff_setup(struct diff_options *options)
 	else
 		DIFF_OPT_CLR(options, COLOR_DIFF);
 	options->detect_rename = diff_detect_rename_default;
+
+	options->a_prefix = "a/";
+	options->b_prefix = "b/";
 }
 
 int diff_setup_done(struct diff_options *options)
@@ -2291,6 +2294,16 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 		else if (40 < options->abbrev)
 			options->abbrev = 40;
 	}
+	else if (!strcmp(arg, "--prefix=")) {
+		char *colon = strchr(arg + 9, ':');
+		options->a_prefix = arg + 9;
+		if (colon) {
+			*colon = '\0';
+			options->b_prefix = colon + 1;
+		}
+		else
+			options->b_prefix = options->a_prefix;
+	}
 	else
 		return 0;
 	return 1;
diff --git a/diff.h b/diff.h
index 7e8000a..beccf85 100644
--- a/diff.h
+++ b/diff.h
@@ -69,6 +69,7 @@ struct diff_options {
 	const char *orderfile;
 	const char *pickaxe;
 	const char *single_follow;
+	const char *a_prefix, *b_prefix;
 	unsigned flags;
 	int context;
 	int break_opt;
-- 
1.5.4.rc0.70.g30f7

  parent reply	other threads:[~2007-12-18 16:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-18 15:42 [PATCH] Add format-patch option --no-name-prefix Pascal Obry
2007-12-18 15:47 ` Pascal Obry
2007-12-18 15:54 ` Johannes Sixt
2007-12-18 15:59   ` Pascal Obry
2007-12-18 16:50     ` Linus Torvalds
2007-12-18 16:54       ` Pascal Obry
2007-12-18 17:06       ` Johannes Schindelin
2007-12-18 17:56         ` Pascal Obry
2007-12-18 18:56           ` [PATCH] Teach diff machinery to display other prefixes than "a/" and "b/" Johannes Schindelin
2007-12-18 19:03             ` Pascal Obry
2007-12-18 19:05             ` Linus Torvalds
2007-12-18 19:32               ` [PATCH v4] " Johannes Schindelin
2007-12-18 19:55                 ` Linus Torvalds
2007-12-18 20:07       ` [PATCH] Add format-patch option --no-name-prefix Junio C Hamano
2007-12-18 16:03 ` Andreas Ericsson
2007-12-18 16:11   ` Pascal Obry
2007-12-19  8:55     ` Andreas Ericsson
2007-12-19  9:21       ` Johannes Sixt
2007-12-18 16:21 ` Johannes Schindelin [this message]
2007-12-18 16:45   ` [PATCH] Teach diff machinery to display other prefixes than "a/" and "b/" Pascal Obry
2007-12-18 16:58     ` Johannes Schindelin
2007-12-18 17:02     ` Matthieu Moy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0712181619550.23902@racer.site \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=pascal.obry@gmail.com \
    --cc=pascal@obry.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).