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

Re: [PATCH] allow git-am to run in a subdirectory

From
Jeff King <peff@peff.net>
Date
Mar 1, 2008, 08:12 UTC
Message-ID
<20080301081235.GA31855@coredump.intra.peff.net>
In-Reply-To
<7vprue6ghc.fsf@gitster.siamese.dyndns.org>
On Fri, Feb 29, 2008 at 11:38:55PM -0800, Junio C Hamano wrote:
> As long as you do not lose sight of the mailbox parameter by chdir'ing
> around, I am Ok with the patch.
> 
> ... and after I started writing that, I find...

Ugh. Below is a patch that saves the original pwd and prefixes it for relative paths. However:

  - it probably doesn't correctly determine absolute versus relative
    paths on Windows. I don't think we have a solution for fixing this
    within shell scripts.
  - it will eat newlines in parameters names (actually, turning them
    into spaces)

The problem is that I need to turn the original "$@" into a new "$@" that is correctly prefixed, which requires proper quoting. Please, spend some of your shell guru points to show me how to do this correctly and portably.

We could wait on doing the 'cd_to_toplevel' until after the mailsplit, but then your .dotest will end up in a prefixed directory (and of course we have a similar munging problem when we point .dotest to the right spot).

Maybe I should just wait for a git-am rewrite in C. ;)
---
diff --git a/git-am.sh b/git-am.sh
index 2ecebc4..a5e0c43 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -2,6 +2,7 @@
 #
 # Copyright (c) 2005, 2006 Junio C Hamano
 
+SUBDIRECTORY_OK=Yes
 OPTIONS_KEEPDASHDASH=
 OPTIONS_SPEC="\
 git-am [options] <mbox>|<Maildir>...
@@ -25,6 +26,13 @@ skip            skip the current patch"
 . git-sh-setup
 set_reflog_action am
 require_work_tree
+orig_pwd=$(pwd)
+cd_to_toplevel
+if test "$(pwd)" = "$orig_pwd"; then
+	orig_pwd=
+else
+	orig_pwd="$orig_pwd/"
+fi
 
 git var GIT_COMMITTER_IDENT >/dev/null || exit
 
@@ -121,6 +129,21 @@ reread_subject () {
 	git stripspace <"$1" | sed -e 1q
 }
 
+shellquote() {
+	printf \'
+	printf "$1" | sed "s/'/\\'/g"
+	echo \'
+}
+
+handle_file_args() {
+	for i in "$@"; do
+		case "$i" in
+		/*) shellquote "$i";;
+		 *) shellquote "$orig_pwd$i";;
+		esac
+	done
+}
+
 prec=4
 dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary=
 resolvemsg= resume=
@@ -148,7 +171,7 @@ do
 	--skip)
 		skip=t ;;
 	-d|--dotest)
-		shift; dotest=$1;;
+		shift; eval dotest=`handle_file_args "$1"` ;;
 	--resolvemsg)
 		shift; resolvemsg=$1 ;;
 	--whitespace)
@@ -163,6 +186,8 @@ do
 	shift
 done
 
+eval set -- `handle_file_args "$@"`
+
 # If the dotest directory exists, but we have finished applying all the
 # patches in them, clear it out.
 if test -d "$dotest" &&
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 11 in “allow git-am to run in a subdirectory”
  1. allow git-am to run in a subdirectoryJeff King, Mar 1, 2008
  2. Junio C HamanoMar 1, 2008
  3. Jeff KingMar 1, 2008
  4. Junio C HamanoMar 1, 2008
  5. Jeff KingMar 1, 2008
  6. Jay SoffianMar 3, 2008
  7. Re* [PATCH] allow git-am to run in a subdirectoryJunio C Hamano, Mar 3, 2008
  8. Jeff KingMar 3, 2008
  9. Junio C HamanoMar 3, 2008
  10. Johannes SchindelinMar 3, 2008
  11. Junio C HamanoMar 3, 2008

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.