git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Jeff King" <peff@peff.net>,
	"Matthieu Moy" <Matthieu.Moy@grenoble-inp.fr>,
	git@vger.kernel.org, "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: Re: [PATCH 3/4] add -u: only show pathless 'add -u' warning when changes exist outside cwd
Date: Mon, 18 Mar 2013 22:28:06 -0700	[thread overview]
Message-ID: <20130319052805.GO5062@elie.Belkin> (raw)
In-Reply-To: <7vli9kkoci.fsf@alter.siamese.dyndns.org>

Junio C Hamano wrote:

> The implemenation appears to run an extra diff_files() in addition
> to the one we already have to run in order to implement the "add -u"
> to collect modified/deleted paths.  Is that the best we can do?  
>
> I am wondering if we can special case the no-pathspec case to have
> add_files_to_cache() call underlying run_diff_files() without any
> pathspec, inspect the paths that are modified and/or deleted in the
> update_callback, add ones that are under the $prefix while noticing
> the ones outside as warning worthy events.

Yes, that can work, for example like this (replacing the patch you're
replying to).

-- >8 --
Subject: add -u: only show pathless 'add -u' warning when changes exist outside cwd

A common workflow in large projects is to chdir into a subdirectory of
interest and only do work there:

	cd src
	vi foo.c
	make test
	git add -u
	git commit

The upcoming change to 'git add -u' behavior would not affect such a
workflow: when the only changes present are in the current directory,
'git add -u' will add all changes, and whether that happens via an
implicit "." or implicit ":/" parameter is an unimportant
implementation detail.

The warning about use of 'git add -u' with no pathspec is annoying
because it serves no purpose in this case.  So suppress the warning
unless there are changes outside the cwd that are not being added.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 builtin/add.c | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index a424e69d..e3ed5d93 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -26,6 +26,7 @@ static int take_worktree_changes;
 struct update_callback_data {
 	int flags;
 	int add_errors;
+	const char **implicit_dot;
 };
 
 static const char *option_with_implicit_dot;
@@ -94,10 +95,26 @@ static void update_callback(struct diff_queue_struct *q,
 {
 	int i;
 	struct update_callback_data *data = cbdata;
+	const char **implicit_dot = data->implicit_dot;
 
 	for (i = 0; i < q->nr; i++) {
 		struct diff_filepair *p = q->queue[i];
 		const char *path = p->one->path;
+
+		/*
+		 * Check if "git add -A" or "git add -u" was run from a
+		 * subdirectory with a modified file outside that directory,
+		 * and warn if so.
+		 *
+		 * "git add -u" will behave like "git add -u :/" instead of
+		 * "git add -u ." in the future.  This warning prepares for
+		 * that change.
+		 */
+		if (implicit_dot &&
+		    !match_pathspec(implicit_dot, path, strlen(path), 0, NULL)) {
+			warn_pathless_add();
+			continue;
+		}
 		switch (fix_unmerged_status(p, data)) {
 		default:
 			die(_("unexpected diff status %c"), p->status);
@@ -121,17 +138,30 @@ static void update_callback(struct diff_queue_struct *q,
 	}
 }
 
+#define ADD_CACHE_IMPLICIT_DOT 32
 int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
 {
 	struct update_callback_data data;
 	struct rev_info rev;
+
+	data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;
+	data.add_errors = 0;
+	data.implicit_dot = NULL;
+	if (flags & ADD_CACHE_IMPLICIT_DOT) {
+		/*
+		 * Check for modified files throughout the worktree so
+		 * update_callback has a chance to warn about changes
+		 * outside the cwd.
+		 */
+		data.implicit_dot = pathspec;
+		pathspec = NULL;
+	}
+
 	init_revisions(&rev, prefix);
 	setup_revisions(0, NULL, &rev, NULL);
 	init_pathspec(&rev.prune_data, pathspec);
 	rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
 	rev.diffopt.format_callback = update_callback;
-	data.flags = flags;
-	data.add_errors = 0;
 	rev.diffopt.format_callback_data = &data;
 	rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
 	run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
@@ -371,6 +401,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	int add_new_files;
 	int require_pathspec;
 	char *seen = NULL;
+	int implicit_dot = 0;
 
 	git_config(add_config, NULL);
 
@@ -400,10 +431,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	}
 	if (option_with_implicit_dot && !argc) {
 		static const char *here[2] = { ".", NULL };
-		if (prefix)
+		if (prefix && addremove)
 			warn_pathless_add();
 		argc = 1;
 		argv = here;
+		implicit_dot = 1;
 	}
 
 	add_new_files = !take_worktree_changes && !refresh_only;
@@ -416,7 +448,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		 (intent_to_add ? ADD_CACHE_INTENT : 0) |
 		 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
 		 (!(addremove || take_worktree_changes)
-		  ? ADD_CACHE_IGNORE_REMOVAL : 0));
+		  ? ADD_CACHE_IGNORE_REMOVAL : 0)) |
+		 (implicit_dot ? ADD_CACHE_IMPLICIT_DOT : 0);
 
 	if (require_pathspec && argc == 0) {
 		fprintf(stderr, _("Nothing specified, nothing added.\n"));
-- 
1.8.2.rc3

  reply	other threads:[~2013-03-19  5:28 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-08 23:54 [PATCH 0/2] "git add -u/-A" from future Junio C Hamano
2013-03-08 23:54 ` [PATCH 1/2] require pathspec for "git add -u/-A" Junio C Hamano
2013-03-10 15:49   ` Matthieu Moy
2013-03-11  7:04     ` Junio C Hamano
2013-03-11  8:00       ` Matthieu Moy
2013-03-11  8:01         ` [PATCH 1/2] add: update pathless 'add [-u|-A]' warning to reflect change of plan Matthieu Moy
2013-03-11  8:01           ` [PATCH 2/2] add: add a newline at the end of pathless 'add [-u|-A]' warning Matthieu Moy
2013-03-11 16:06             ` Junio C Hamano
2013-04-02 14:43               ` Matthieu Moy
2013-04-02 16:31                 ` Junio C Hamano
2013-04-02 16:57                   ` Matthieu Moy
2013-03-12 11:28     ` [PATCH 1/2] require pathspec for "git add -u/-A" Jeff King
2013-03-12 13:58       ` Matthieu Moy
2013-03-13  4:08         ` Jeff King
2013-03-13  4:10           ` [PATCH 1/2] t2200: check that "add -u" limits itself to subdirectory Jeff King
2013-03-13  8:52             ` Matthieu Moy
2013-03-13 17:44             ` Junio C Hamano
2013-03-14  6:44               ` Jeff King
2013-03-13  4:10           ` [PATCH 2/2] add: respect add.updateroot config option Jeff King
2013-03-13  9:07             ` Matthieu Moy
2013-03-13  9:27               ` Jeff King
2013-03-13 15:51                 ` Junio C Hamano
2013-03-14 12:39                   ` Matthieu Moy
2013-03-19  3:44             ` [PATCH 0/4] make pathless 'add [-u|-A]' warning less noisy Jonathan Nieder
2013-03-19  3:45               ` [PATCH 1/4] add: make pathless 'add [-u|-A]' warning a file-global function Jonathan Nieder
2013-03-19  3:46               ` [PATCH 2/4] add: make warn_pathless_add() a no-op after first call Jonathan Nieder
2013-03-19  3:48               ` [PATCH 3/4] add -u: only show pathless 'add -u' warning when changes exist outside cwd Jonathan Nieder
2013-03-19  4:25                 ` Junio C Hamano
2013-03-19  5:28                   ` Jonathan Nieder [this message]
2013-03-19 14:57                     ` Junio C Hamano
2013-03-19  5:34                   ` Jonathan Nieder
2013-03-19  5:37                   ` Duy Nguyen
2013-03-19  5:44                     ` Jonathan Nieder
2013-03-19  6:21                 ` Matthieu Moy
2013-03-19 15:06                   ` Junio C Hamano
2013-03-19 19:06                     ` Jonathan Nieder
2013-03-19 19:47                       ` Junio C Hamano
2013-03-19 20:34                         ` Jonathan Nieder
2013-03-19  3:49               ` [PATCH 4/4] add -A: only show pathless 'add -A' " Jonathan Nieder
2013-03-19  4:25               ` [PATCH 0/4] make pathless 'add [-u|-A]' warning less noisy Jeff King
2013-03-08 23:54 ` [PATCH 2/2] git add: -u/-A now affects the entire working tree Junio C Hamano
2013-03-19 22:44 ` [PATCH v2 0/6] make pathless 'add [-u|-A]' warning less noisy Jonathan Nieder
2013-03-19 22:44   ` [PATCH 1/6] t2200: check that "add -u" limits itself to subdirectory Jonathan Nieder
2013-03-19 22:45   ` [PATCH 2/6] add: make pathless 'add [-u|-A]' warning a file-global function Jonathan Nieder
2013-03-19 22:45   ` [PATCH 3/6] add: make warn_pathless_add() a no-op after first call Jonathan Nieder
2013-03-19 22:50   ` [PATCH 4/6] add -u: only show pathless 'add -u' warning when changes exist outside cwd Jonathan Nieder
2013-03-20  5:06     ` Jeff King
2013-03-20 15:10     ` Junio C Hamano
2013-03-19 22:51   ` [PATCH 5/6] add -A: only show pathless 'add -A' " Jonathan Nieder
2013-03-20 15:30     ` Junio C Hamano
2013-03-19 22:53   ` [PATCH 6/6] git add: -u/-A now affects the entire working tree Jonathan Nieder

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=20130319052805.GO5062@elie.Belkin \
    --to=jrnieder@gmail.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.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).