Subject: [PATCH/RFC 3/3] diff.c: add --relative-names to be used with --name-only

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 26 Jun 2016 19:16:16 +0200

The difference with --relative option is, this option does not filter
paths outside cwd. You can add two more chars " ." on your command
line for that.

This serves two purposes

 - user friendlier to copy/paste. When full paths are shown, you can
   still use ":/" magic to get around it, but unless you're a lefty
   you need to move your right hand between the keyboard and mouse for
   each path. Not very efficient.

 - script friendly. Strictly speaking a very light form of scripting
   from command line prompt when you just pipe paths to the next
   command. Paths relative to cwd would be much easier to pip this
   way.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 It would be great if --relative could be used for this (and it feels
 weird that the option performs both actions at once, stripping _and_
 filtering where filtering could easily be done with pathspec). But
 it's too late to change --relative behavior now.

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

diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index d9ae681..1554008 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -481,6 +481,10 @@ ifndef::git-format-patch[]
 	not in a subdirectory (e.g. in a bare repository), you
 	can name which subdirectory to make the output relative
 	to by giving a <path> as an argument.
+
+--relative-names::
+	When used with `--name-only`, paths are shown relative to
+	current working directory.
 endif::git-format-patch[]
 
 -a::
diff --git a/diff.c b/diff.c
index bd5d190..3a938ac 100644
--- a/diff.c
+++ b/diff.c
@@ -3335,6 +3335,13 @@ void diff_setup_done(struct diff_options *options)
 	if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
 		options->detect_rename = DIFF_DETECT_COPY;
 
+	/*
+	 * --relative can change "prefix", which may cause unintended
+	 * consequences for --relative-names
+	 */
+	if (DIFF_OPT_TST(options, RELATIVE_NAME) && options->relative_names)
+		die(_("--relative and --relative-names are mutually exclusive"));
+
 	if (options->prefix)
 		options->prefix_length = strlen(options->prefix);
 	else
@@ -3792,7 +3799,8 @@ int diff_opt_parse(struct diff_options *options,
 	else if (skip_prefix(arg, "--relative=", &arg)) {
 		DIFF_OPT_SET(options, RELATIVE_NAME);
 		options->prefix = arg;
-	}
+	} else if (!strcmp(arg, "--relative-names"))
+		options->relative_names = 1;
 
 	/* xdiff options */
 	else if (!strcmp(arg, "--minimal"))
@@ -4347,8 +4355,15 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
 		const char *name_a, *name_b;
 		name_a = p->two->path;
 		name_b = NULL;
-		strip_prefix(opt, &name_a, &name_b);
-		write_name_quoted(name_a, opt->file, opt->line_termination);
+		if (opt->relative_names)
+			write_name_quoted_relative(name_a,
+						   opt->prefix, opt->file,
+						   opt->line_termination);
+		else {
+			strip_prefix(opt, &name_a, &name_b);
+			write_name_quoted(name_a, opt->file,
+					  opt->line_termination);
+		}
 	}
 }
 
diff --git a/diff.h b/diff.h
index 125447b..ad6423d 100644
--- a/diff.h
+++ b/diff.h
@@ -139,6 +139,7 @@ struct diff_options {
 	int dirstat_permille;
 	int setup;
 	int abbrev;
+	int relative_names;
 /* white-space error highlighting */
 #define WSEH_NEW 1
 #define WSEH_CONTEXT 2
-- 
2.8.2.531.gd073806