Skip to contentSkip to conversation

Why is there no git-update-cache --modified (aka I give up)

PATCH New Marc Singer 5 participants 9 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw
  # git-diff-cache HEAD

is really nice. But, do I really have to invoke git-update-cache with every modified file? I could write a script to cul the filenames from git-diff-cache, but I'm having a hard time believing that that is how others are preparing their commits.

#1 lore raw

Dear diary, on Tue, Jul 12, 2005 at 07:52:18AM CEST, I got a letter where Marc Singer <[email protected]> told me that...

Quoted 6 lines
  # git-diff-cache HEAD

is really nice. But, do I really have to invoke git-update-cache with every modified file? I could write a script to cul the filenames from git-diff-cache, but I'm having a hard time believing that that is how others are preparing their commits.

It is. :-) It's only that they have cool scripts to do it, e.g. cg-commit. (You have to enumerate the files explicitly for git-commit-script as well, IIRC.)

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
<Espy> be careful, some twit might quote you out of context..
#2 lore raw
In reply to Marc Singer
Quoted 6 lines · Marc Singer
  # git-diff-cache HEAD

is really nice. But, do I really have to invoke git-update-cache with every modified file? I could write a script to cul the filenames from git-diff-cache, but I'm having a hard time believing that that is how others are preparing their commits.

Me too. By the way, I think you mean diff-files not diff-cache.

I'd like to know how others do it, since this was the first thing I automated when I started using GIT. Having clear distinction between cache (aka index file) and work tree files and making user concious decision of when to and when not to register/update index is what is quite different in GIT from CVS, SVN and friends [*1*], and while I appreciate its flexibility, it often ends up to be simply more typing (and to certain degree more thinking which is not a bad thing) to the user [*2*].

This snippet is essentially what I do to match the cache with the current work tree [*3*]:

	case "$(git-ls-files --unmerged | sed -e 1q)" in
	?*)
		echo >&2 You have unmerged files and cannot commit.
        	exit 1
		;;
	esac
	git-update-cache --refresh >/dev/null
	git-diff-files |
        sed -e 's/.*	//' |
	xargs -r git-update-cache --add --remove --

[Footnote]

*1* I vaguely recall reading somewhere that GIT is not the first SCM to have these three (not two) levels. Usual two-level SCM needs to only move between your hackery and what's in the repo, and words like "committing" and "checking in" are used interchangeably, while the other three level SCM (I do not remember which one I read about) gives distinct meaning to these two words. Can anybody tell me which SCM I am talking about?

*2* The commit flow in GIT is three level thing. You have HEAD version (a commit with an associated tree already in the object database that you have checked out and started with), you have cache, and you have files in your work tree. Checking out is to match cache and work tree to HEAD; update-cache is to match cache to work tree; and committing is to create a new commit that matches the cache and make it your HEAD. Roughly speaking, diff-* brothers are about comparing these three stages:

- diff-files compares cache and work tree
- diff-cache compares commit and cache, or commit and work tree
- diff-tree compares two commits

*3* I do not officially do Porcelain ;-), but the script I use is slightly different from the one quoted above. It uses "git-diff-files -z" and a helper program to handle filenames with TAB in them.

#3 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Marc Singer
  # git-diff-cache HEAD

is really nice. But, do I really have to invoke git-update-cache with every modified file? I could write a script to cul the filenames from git-diff-cache, but I'm having a hard time believing that that is how others are preparing their commits.

Me too. By the way, I think you mean diff-files not diff-cache.

No, I mean git-diff-cache. I find that this works pretty well, though.

  # git-update-cache `git-diff-cache | cut -f2`

It looks like the same thing works for git-diff-files.

  # git-update-cache `git-diff-files | cut -f2`

This seems to agree with the way you handle things.

Similarly, there is the need to determine which files are new to the tree. This isn't much of a burden when creating files in the tree, but can be bothersome when using patch since git-apply is conservative about fuzz.

#4 lore raw
Quoted 1 line · Marc Singer
  # git-update-cache `git-diff-cache | cut -f2`

g-d-c should have an option to print file names only. All that cutting and argument-backtick-ing gets pretty nasty when there are a lot of files, or if they contain special characters.

Signature · 6 lines
Matthias Urlichs   |   {M:U} IT Design @ m-u-it.de   |  [email protected]
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
 - -
God is a perfect example of the kind of aberration that can result from
an untrained intellect combining with an unrestrained imagination.
		-- Simon Ewins
#5 lore raw
Quoted 8 lines · Matthias Urlichs <smurf <at> smurf.noris.de> writes:
Quoted 1 line · Marc Singer
  # git-update-cache `git-diff-cache | cut -f2`

g-d-c should have an option to print file names only. All that cutting and argument-backtick-ing gets pretty nasty when there are a lot of files, or if they contain special characters.

I concur. I'll add --name-only flag to diff brothers soonish.

Sorry I am at work and have turned the incoming connection to my home network before I left for work today -- this is the same [email protected] who is guilty for all your diff problems ;-).

#6 lore raw

PATCH 1/1 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.

---

 diff-cache.c  |    8 ++++++++
 diff-files.c  |    4 ++++
 diff-stages.c |    4 ++++
 diff-tree.c   |    8 ++++++++
 diff.c        |   13 ++++++++++++-
 diff.h        |    2 ++
 6 files changed, 38 insertions(+), 1 deletions(-)

ea3228b62de0838cd854ecf503f6f534b0b7e25c

diff-cache.c +8 -0
@@ -223,6 +223,14 @@ int main(int argc, const char **argv)
 			diff_output_format = DIFF_FORMAT_MACHINE;
 			continue;
 		}
+		if (!strcmp(arg, "--name-only")) {
+			diff_output_format = DIFF_FORMAT_NAME;
+			continue;
+		}
+		if (!strcmp(arg, "--name-only-z")) {
+			diff_output_format = DIFF_FORMAT_NAME_Z;
+			continue;
+		}
 		if (!strcmp(arg, "-R")) {
 			diff_setup_opt |= DIFF_SETUP_REVERSE;
 			continue;
diff-files.c +4 -0
@@ -55,6 +55,10 @@ int main(int argc, const char **argv)
 			; /* no-op */
 		else if (!strcmp(argv[1], "-z"))
 			diff_output_format = DIFF_FORMAT_MACHINE;
+		else if (!strcmp(argv[1], "--name-only"))
+			diff_output_format = DIFF_FORMAT_NAME;
+		else if (!strcmp(argv[1], "--name-only-z"))
+			diff_output_format = DIFF_FORMAT_NAME_Z;
 		else if (!strcmp(argv[1], "-R"))
 			diff_setup_opt |= DIFF_SETUP_REVERSE;
 		else if (!strncmp(argv[1], "-S", 2))
diff-stages.c +4 -0
@@ -87,6 +87,10 @@ int main(int ac, const char **av)
 			find_copies_harder = 1;
 		else if (!strcmp(arg, "-z"))
 			diff_output_format = DIFF_FORMAT_MACHINE;
+		else if (!strcmp(arg, "--name-only"))
+			diff_output_format = DIFF_FORMAT_NAME;
+		else if (!strcmp(arg, "--name-only-z"))
+			diff_output_format = DIFF_FORMAT_NAME_Z;
 		else if (!strcmp(arg, "-R"))
 			diff_setup_opt |= DIFF_SETUP_REVERSE;
 		else if (!strncmp(arg, "-S", 2))
diff-tree.c +8 -0
@@ -480,6 +480,14 @@ int main(int argc, const char **argv)
 			find_copies_harder = 1;
 			continue;
 		}
+		if (!strcmp(arg, "--name-only")) {
+			diff_output_format = DIFF_FORMAT_NAME;
+			continue;
+		}
+		if (!strcmp(arg, "--name-only-z")) {
+			diff_output_format = DIFF_FORMAT_NAME_Z;
+			continue;
+		}
 		if (!strcmp(arg, "-z")) {
 			diff_output_format = DIFF_FORMAT_MACHINE;
 			continue;
diff.c +12 -1
@@ -818,6 +818,12 @@ static void diff_flush_raw(struct diff_f
 	putchar(line_termination);
 }
 
+static void diff_flush_name(struct diff_filepair *p,
+			    int line_termination)
+{
+	printf("%s%c", p->two->path, line_termination);
+}
+
 int diff_unmodified_pair(struct diff_filepair *p)
 {
 	/* This function is written stricter than necessary to support
@@ -978,7 +984,8 @@ void diff_flush(int diff_output_style)
 	int line_termination = '\n';
 	int inter_name_termination = '\t';
 
-	if (diff_output_style == DIFF_FORMAT_MACHINE)
+	if (diff_output_style == DIFF_FORMAT_MACHINE ||
+	    diff_output_style == DIFF_FORMAT_NAME_Z)
 		line_termination = inter_name_termination = 0;
 
 	for (i = 0; i < q->nr; i++) {
@@ -997,6 +1004,10 @@ void diff_flush(int diff_output_style)
 			diff_flush_raw(p, line_termination,
 				       inter_name_termination);
 			break;
+		case DIFF_FORMAT_NAME:
+		case DIFF_FORMAT_NAME_Z:
+			diff_flush_name(p, line_termination);
+			break;
 		}
 	}
 	for (i = 0; i < q->nr; i++)
diff.h +2 -0
@@ -59,6 +59,8 @@ extern int diff_queue_is_empty(void);
 #define DIFF_FORMAT_MACHINE	1
 #define DIFF_FORMAT_PATCH	2
 #define DIFF_FORMAT_NO_OUTPUT	3
+#define DIFF_FORMAT_NAME	4
+#define DIFF_FORMAT_NAME_Z	5
 
 extern void diff_flush(int output_style);
 
#7 lore raw

PATCH 1/1 diff-stages: support "-u" as a synonym for "-p".

In reply to Junio C Hamano

Just to be consistent, support "-u" as a synonym for "-p" like everybody else does.

---

 diff-stages.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

ef519b28e8edb19d4a0233152c9497ee807a4f5b

diff-stages.c +1 -1
@@ -67,7 +67,7 @@ int main(int ac, const char **av)
 		const char *arg = av[1];
 		if (!strcmp(arg, "-r"))
 			; /* as usual */
-		else if (!strcmp(arg, "-p"))
+		else if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
 			diff_output_format = DIFF_FORMAT_PATCH;
 		else if (!strncmp(arg, "-B", 2)) {
 			if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
#8 lore raw

PATCH 1/1 Clean up diff option descriptions.

In reply to Junio C Hamano

I got tired of maintaining almost duplicated descriptions in diff-* brothers, both in usage string and documentation.

---

*** This comes after the "diff-stages -u" and "--name-only" patches. *** I am in the process of a full sweep over Documentation/ and *** program usage strings right now so you will see more patches *** to Documentation/ area from me.

 Documentation/Makefile            |    2 +
 Documentation/diff-options.txt    |   53 +++++++++++++++++++++++++++++++++++++
 Documentation/git-diff-cache.txt  |   48 ++--------------------------------
 Documentation/git-diff-files.txt  |   44 +------------------------------
 Documentation/git-diff-stages.txt |   48 ++--------------------------------
 Documentation/git-diff-tree.txt   |   46 ++------------------------------
 diff-cache.c                      |    4 ++-
 diff-files.c                      |    4 ++-
 diff-stages.c                     |    3 +-
 diff-tree.c                       |    4 ++-
 diff.h                            |   20 ++++++++++++++
 11 files changed, 96 insertions(+), 180 deletions(-)
 create mode 100644 Documentation/diff-options.txt

6a3a580f9f9da9d5e821c2ffbe9d0dd1fe54cb63

Documentation/Makefile +1 -1
@@ -25,7 +25,7 @@ man1: $(DOC_MAN1)
 man7: $(DOC_MAN7)
 
 # 'include' dependencies
-git-diff-%.txt: diff-format.txt
+git-diff-%.txt: diff-format.txt diff-options.txt
 	touch $@
 
 clean:
Documentation/diff-options.txt new file +53 -0

53 lines not on this page · raw message

Documentation/git-diff-cache.txt +3 -45

63 lines not on this page · raw message

Documentation/git-diff-files.txt +2 -42

60 lines not on this page · raw message

Documentation/git-diff-stages.txt +3 -45

63 lines not on this page · raw message

Documentation/git-diff-tree.txt +3 -43

67 lines not on this page · raw message

diff-cache.c +3 -1
@@ -159,7 +159,9 @@ static void mark_merge_entries(void)
 }
 
 static char *diff_cache_usage =
-"git-diff-cache [-p] [-r] [-z] [-m] [--cached] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] <tree-ish> [<path>...]";
+"git-diff-cache [-m] [--cached] "
+"[<common diff options>] <tree-ish> [<path>...]"
+COMMON_DIFF_OPTIONS_HELP;
 
 int main(int argc, const char **argv)
 {
diff-files.c +3 -1
@@ -7,7 +7,9 @@
 #include "diff.h"
 
 static const char *diff_files_usage =
-"git-diff-files [-p] [-q] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";
+"git-diff-files [-q] "
+"[<common diff options>] [<path>...]"
+COMMON_DIFF_OPTIONS_HELP;
 
 static int diff_output_format = DIFF_FORMAT_HUMAN;
 static int detect_rename = 0;
diff-stages.c +2 -1
@@ -17,7 +17,8 @@ static const char *orderfile = NULL;
 static const char *diff_filter = NULL;
 
 static char *diff_stages_usage =
-"git-diff-stages [-p] [-r] [-z] [-R] [-B] [-M] [-C] [--find-copies-harder] [-O<orderfile>] [-S<string>] [--pickaxe-all] <stage1> <stage2> [<path>...]";
+"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
+COMMON_DIFF_OPTIONS_HELP;
 
 static void diff_stages(int stage1, int stage2)
 {
diff-tree.c +3 -1
@@ -395,7 +395,9 @@ static int diff_tree_stdin(char *line)
 }
 
 static char *diff_tree_usage =
-"git-diff-tree [-p] [-r] [-z] [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-R] [-B] [-M] [-C] [--find-copies-header] [-O<orderfile>] [-S<string>] [--pickaxe-all] <tree-ish> <tree-ish>";
+"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
+"[<common diff options>] <tree-ish> <tree-ish>"
+COMMON_DIFF_OPTIONS_HELP;
 
 int main(int argc, const char **argv)
 {
diff.h +20 -0
@@ -53,6 +53,26 @@ extern void diffcore_std_no_resolve(cons
 				    const char *pickaxe, int pickaxe_opts,
 				    const char *orderfile, const char *filter);
 
+#define COMMON_DIFF_OPTIONS_HELP \
+"\ncommon diff options:\n" \
+"  -r		diff recursively (only meaningful in diff-tree)\n" \
+"  -z		output diff-raw with lines terminated with NUL.\n" \
+"  -p		output patch format.\n" \
+"  -u		synonym for -p.\n" \
+"  --name-only	show only names of changed files.\n" \
+"  --name-only-z\n" \
+"		same as --name-only but terminate lines with NUL.\n" \
+"  -R		swap input file pairs.\n" \
+"  -B		detect complete rewrites.\n" \
+"  -M		detect renames.\n" \
+"  -C		detect copies.\n" \
+"  --find-copies-harder\n" \
+"		try unchanged files as candidate for copy detection.\n" \
+"  -O<file>	reorder diffs according to the <file>.\n" \
+"  -S<string>	find filepair whose only one side contains the string.\n" \
+"  --pickaxe-all\n" \
+"		show all files diff when -S is used and hit is found.\n"
+
 extern int diff_queue_is_empty(void);
 
 #define DIFF_FORMAT_HUMAN	0