git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] add -i: Fix running from a subdirectory

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 25, 2007, 18:10 UTC
Message-ID
<7v7ik6nqm5.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<1195996542-86074-1-git-send-email-win@wincent.com>

This fixes the pathspec interactive_add() passes to the underlying git-add--interactive helper. When the command was run from a subdirectory, cmd_add() already has gone up to the toplevel of the work tree, and the helper will be spawned from there. The pathspec given on the command line from the user needs to be adjusted for this.

This adds "validate_pathspec()" function in the callchain, but it does not validate yet. The function can be changed to barf if there are unmatching pathspec given by the user, but that is not strictly necessary.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * The reason it is not "strictly necessary" is because if you give
   unmatching pathspecs, you simply would not see their effects (good or
   bad) in any of the subcommands in git-add--interactive.
 builtin-add.c    |   24 ++++++++++++++++++++----
 builtin-commit.c |    2 +-
 commit.h         |    2 +-
 3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/builtin-add.c b/builtin-add.c
index 7c6a296..865c475 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -135,13 +135,29 @@ static void refresh(int verbose, const char **pathspec)
         free(seen);
 }
 
-int interactive_add(int argc, const char **argv)
+static const char **validate_pathspec(int argc, const char **argv, const char *prefix)
+{
+	const char **pathspec = get_pathspec(prefix, argv);
+
+	return pathspec;
+}
+
+int interactive_add(int argc, const char **argv, const char *prefix)
 {
 	int status;
-	const char **args = xcalloc(sizeof(const char *), (argc + 2));
+	const char **args;
+	const char **pathspec = NULL;
+
+	if (argc) {
+		pathspec = validate_pathspec(argc, argv, prefix);
+		if (!pathspec)
+			return -1;
+	}
 
+	args = xcalloc(sizeof(const char *), (argc + 2));
 	args[0] = "add--interactive";
-	memcpy(&(args[1]), argv, sizeof(const char *) * argc);
+	if (argc)
+		memcpy(&(args[1]), pathspec, sizeof(const char *) * argc);
 	args[argc + 1] = NULL;
 
 	status = run_command_v_opt(args, RUN_GIT_CMD);
@@ -177,7 +193,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, builtin_add_options,
 			  builtin_add_usage, 0);
 	if (add_interactive)
-		exit(interactive_add(argc, argv));
+		exit(interactive_add(argc, argv, prefix));
 
 	git_config(git_default_config);
 
diff --git a/builtin-commit.c b/builtin-commit.c
index 5d27102..45e51b1 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -165,7 +165,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
 	const char **pathspec = NULL;
 
 	if (interactive) {
-		interactive_add(argc, argv);
+		interactive_add(argc, argv, prefix);
 		commit_style = COMMIT_AS_IS;
 		return get_index_file();
 	}
diff --git a/commit.h b/commit.h
index 9f0765b..10e2b5d 100644
--- a/commit.h
+++ b/commit.h
@@ -113,7 +113,7 @@ extern struct commit_list *get_shallow_commits(struct object_array *heads,
 
 int in_merge_bases(struct commit *, struct commit **, int);
 
-extern int interactive_add(int argc, const char **argv);
+extern int interactive_add(int argc, const char **argv, const char *prefix);
 extern int rerere(void);
 
 static inline int single_parent(struct commit *commit)
-- 
1.5.3.6.2014.g7500f
Previous: Jeff King
Message 12 of 12 in “Updates to git-add--interactive”
  1. 0/3 Updates to git-add--interactiveWincent Colaiuta, Nov 25, 2007
  2. 1/3 Rename patch_update_file function to patch_update_pathspecWincent Colaiuta, Nov 25, 2007
  3. 2/3 Move pathspec validation into interactive_addWincent Colaiuta, Nov 25, 2007
  4. 3/3 Add "--patch" option to git-add--interactiveWincent Colaiuta, Nov 25, 2007
  5. Junio C HamanoNov 25, 2007
  6. builtin-add: fix command line building to call interactiveJunio C Hamano, Nov 25, 2007
  7. Wincent ColaiutaNov 25, 2007
  8. Junio C HamanoNov 25, 2007
  9. Wincent ColaiutaNov 25, 2007
  10. Junio C HamanoNov 25, 2007
  11. Jeff KingNov 26, 2007
  12. add -i: Fix running from a subdirectoryJunio C Hamano, Nov 25, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.