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

Re: [PATCH 1/3] After chdir to run grep, return to old directory

From
Jeff King <peff@peff.net>
Date
May 7, 2014, 03:00 UTC
Message-ID
<20140507030037.GB20726@sigill.intra.peff.net>
In-Reply-To
<1399421211.11843.53.camel@stross>
On Tue, May 06, 2014 at 05:06:51PM -0700, David Turner wrote:
Show 17 quoted lines
> On Tue, 2014-05-06 at 15:24 -0700, Junio C Hamano wrote:
> > dturner@twopensource.com writes:
> > 
> > > From: David Turner <dturner@twitter.com>
> > >
> > > Signed-off-by: David Turner <dturner@twitter.com>
> > 
> > Ehh, why?
> 
> Briefly, because otherwise ./t7811-grep-open.sh fails when run under
> watchman.
> 
> This is actually something that I think I'm doing wrong, but I can't see
> what the sensible way to do it is.  When we go to write the fs_cache (in
> an atexit hook), we use get_fs_cache_file() from environment.c.  This is
> a relative path, because all of the other similar paths are.  So if we
> have chdired, then we fail.

It sounds like a reasonable code-hygiene thing, too. It looks like we are only doing the chdir to impact the environment of the pager, and affecting the global environment of the parent process is a side effect. It happens not to cause problems now because we exit immediately, but anybody who adds code after run_pager has to deal with it (and that is basically what you are doing here). Something like cleaning up tempfiles would be similarly affected.

That being said, this really seems like something that the run-command interface should be doing, since it can handle the chdir in the forked child. And indeed, it seems to support that.

Maybe:
-- >8 --
Subject: grep: use run-command's "dir" option for --open-files-in-pager

Git generally changes directory to the repository root on startup. When running "grep --open-files-in-pager" from a subdirectory, we chdir back to the original directory before running the pager, so that we can feed the relative pathnames to the pager.

We currently do this chdir manually, but we can ask run_command to do it for us. This is fewer lines of code, and as a bonus, the chdir is limited to the child process, which avoids any unexpected surprises for code running after the pager (there isn't any currently, but this is future-proofing).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/grep.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index 69ac2d8..43af5b7 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -361,9 +361,7 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
 		argv[i] = path_list->items[i].string;
 	argv[path_list->nr] = NULL;
 
-	if (prefix && chdir(prefix))
-		die(_("Failed to chdir: %s"), prefix);
-	status = run_command_v_opt(argv, RUN_USING_SHELL);
+	status = run_command_v_opt_cd_env(argv, RUN_USING_SHELL, prefix, NULL);
 	if (status)
 		exit(status);
 	free(argv);
-- 
2.0.0.rc1.436.g03cb729
Previous: David TurnerNext: David Turner
Message 5 of 43 in “Watchman support for git”
  1. dturner@twopensource.comMay 2, 2014
  2. 1/3 After chdir to run grep, return to old directorydturner@twopensource.com, May 2, 2014
  3. Junio C HamanoMay 6, 2014
  4. David TurnerMay 7, 2014
  5. Jeff KingMay 7, 2014
  6. David TurnerMay 7, 2014
  7. Junio C HamanoMay 7, 2014
  8. David TurnerMay 7, 2014
  9. 3/3 Watchman supportdturner@twopensource.com, May 2, 2014
  10. Felipe ContrerasMay 2, 2014
  11. David TurnerMay 3, 2014
  12. Felipe ContrerasMay 3, 2014
  13. David TurnerMay 5, 2014
  14. Felipe ContrerasMay 5, 2014
  15. Sebastian SchuberthMay 8, 2014
  16. David LangMay 9, 2014
  17. David TurnerMay 9, 2014
  18. David LangMay 9, 2014
  19. David TurnerMay 9, 2014
  20. David LangMay 9, 2014
  21. David TurnerMay 9, 2014
  22. Duy NguyenMay 3, 2014
  23. David TurnerMay 3, 2014
  24. Duy NguyenMay 3, 2014
  25. David TurnerMay 3, 2014
  26. Duy NguyenMay 4, 2014
  27. David TurnerMay 6, 2014
  28. Duy NguyenMay 6, 2014
  29. Duy NguyenMay 6, 2014
  30. Duy NguyenMay 10, 2014
  31. David TurnerMay 10, 2014
  32. Duy NguyenMay 11, 2014
  33. David TurnerMay 11, 2014
  34. Duy NguyenMay 12, 2014
  35. David TurnerMay 13, 2014
  36. Duy NguyenMay 13, 2014
  37. David TurnerMay 13, 2014
  38. Duy NguyenMay 10, 2014
  39. David TurnerMay 13, 2014
  40. Duy NguyenMay 14, 2014
  41. Duy NguyenMay 14, 2014
  42. David TurnerMay 15, 2014
  43. Duy NguyenMay 19, 2014

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.