Skip to content

Commit 52f2852

Browse files
Junio C HamanoLinus Torvalds
authored andcommitted
[PATCH] git-diff-*: --name-only and --name-only-z.
Porcelain layers often want to find only names of changed files, and even with diff-raw output format they end up having to pick out only the filename. Support --name-only (and --name-only-z for xargs -0 and cpio -0 users that want to treat filenames with embedded newlines sanely) flag to help them. Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8a62a30 commit 52f2852

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-1
lines changed

diff-cache.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ int main(int argc, const char **argv)
223223
diff_output_format = DIFF_FORMAT_MACHINE;
224224
continue;
225225
}
226+
if (!strcmp(arg, "--name-only")) {
227+
diff_output_format = DIFF_FORMAT_NAME;
228+
continue;
229+
}
230+
if (!strcmp(arg, "--name-only-z")) {
231+
diff_output_format = DIFF_FORMAT_NAME_Z;
232+
continue;
233+
}
226234
if (!strcmp(arg, "-R")) {
227235
diff_setup_opt |= DIFF_SETUP_REVERSE;
228236
continue;

diff-files.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ int main(int argc, const char **argv)
5555
; /* no-op */
5656
else if (!strcmp(argv[1], "-z"))
5757
diff_output_format = DIFF_FORMAT_MACHINE;
58+
else if (!strcmp(argv[1], "--name-only"))
59+
diff_output_format = DIFF_FORMAT_NAME;
60+
else if (!strcmp(argv[1], "--name-only-z"))
61+
diff_output_format = DIFF_FORMAT_NAME_Z;
5862
else if (!strcmp(argv[1], "-R"))
5963
diff_setup_opt |= DIFF_SETUP_REVERSE;
6064
else if (!strncmp(argv[1], "-S", 2))

diff-stages.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ int main(int ac, const char **av)
8787
find_copies_harder = 1;
8888
else if (!strcmp(arg, "-z"))
8989
diff_output_format = DIFF_FORMAT_MACHINE;
90+
else if (!strcmp(arg, "--name-only"))
91+
diff_output_format = DIFF_FORMAT_NAME;
92+
else if (!strcmp(arg, "--name-only-z"))
93+
diff_output_format = DIFF_FORMAT_NAME_Z;
9094
else if (!strcmp(arg, "-R"))
9195
diff_setup_opt |= DIFF_SETUP_REVERSE;
9296
else if (!strncmp(arg, "-S", 2))

diff-tree.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,14 @@ int main(int argc, const char **argv)
480480
find_copies_harder = 1;
481481
continue;
482482
}
483+
if (!strcmp(arg, "--name-only")) {
484+
diff_output_format = DIFF_FORMAT_NAME;
485+
continue;
486+
}
487+
if (!strcmp(arg, "--name-only-z")) {
488+
diff_output_format = DIFF_FORMAT_NAME_Z;
489+
continue;
490+
}
483491
if (!strcmp(arg, "-z")) {
484492
diff_output_format = DIFF_FORMAT_MACHINE;
485493
continue;

diff.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,12 @@ static void diff_flush_raw(struct diff_filepair *p,
818818
putchar(line_termination);
819819
}
820820

821+
static void diff_flush_name(struct diff_filepair *p,
822+
int line_termination)
823+
{
824+
printf("%s%c", p->two->path, line_termination);
825+
}
826+
821827
int diff_unmodified_pair(struct diff_filepair *p)
822828
{
823829
/* This function is written stricter than necessary to support
@@ -978,7 +984,8 @@ void diff_flush(int diff_output_style)
978984
int line_termination = '\n';
979985
int inter_name_termination = '\t';
980986

981-
if (diff_output_style == DIFF_FORMAT_MACHINE)
987+
if (diff_output_style == DIFF_FORMAT_MACHINE ||
988+
diff_output_style == DIFF_FORMAT_NAME_Z)
982989
line_termination = inter_name_termination = 0;
983990

984991
for (i = 0; i < q->nr; i++) {
@@ -997,6 +1004,10 @@ void diff_flush(int diff_output_style)
9971004
diff_flush_raw(p, line_termination,
9981005
inter_name_termination);
9991006
break;
1007+
case DIFF_FORMAT_NAME:
1008+
case DIFF_FORMAT_NAME_Z:
1009+
diff_flush_name(p, line_termination);
1010+
break;
10001011
}
10011012
}
10021013
for (i = 0; i < q->nr; i++)

diff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ extern int diff_queue_is_empty(void);
5959
#define DIFF_FORMAT_MACHINE 1
6060
#define DIFF_FORMAT_PATCH 2
6161
#define DIFF_FORMAT_NO_OUTPUT 3
62+
#define DIFF_FORMAT_NAME 4
63+
#define DIFF_FORMAT_NAME_Z 5
6264

6365
extern void diff_flush(int output_style);
6466

0 commit comments

Comments
 (0)