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

[PATCH] guilt(1): reimplement push_patch, using a subshell to avoid locals.

From
Pierre Habouzit <madcoder@debian.org>
Date
Jun 14, 2007, 12:50 UTC
Message-ID
<11818254623497-git-send-email-madcoder@debian.org>
In-Reply-To
<11818254621527-git-send-email-madcoder@debian.org>
  this is a bit ugly, but makes the code really shorter, and more readable
anyway. We still need the old bail variable, named __push_patch_bail to
avoid spurious collisions (we are not recursing into ourselves, so it's
safe).
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 guilt |  133 ++++++++++++++++++++++++-----------------------------------------
 1 files changed, 49 insertions(+), 84 deletions(-)
diff --git a/guilt b/guilt
index 44273f3..f77c050 100755
--- a/guilt
+++ b/guilt
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
 #
@@ -341,106 +341,71 @@ update_stack_tags()
 # usage: push_patch patchname [bail_action]
 push_patch()
 {
-	local p="$GUILT_DIR/$branch/$1"
-	local pname="$1"
-	local bail_action="$2"
+	__push_patch_bail=0
 
-	local bail=0
-	local reject="--reject"
-
-	assert_head_check
-
-	cd "$TOP_DIR"
+	(
+		p="$GUILT_DIR/$branch/$1"
+		pname="$1"
+		bail_action="$2"
+		reject="--reject"
 
-	# apply the patch if and only if there is something to apply
-	if [ `git-apply --numstat "$p" | wc -l` -gt 0 ]; then
-		if [ "$bail_action" = abort ]; then
-		    reject=""
-		fi
-		git-apply -C$guilt_push_diff_context \
-			$reject "$p" > /dev/null 2> /tmp/guilt.log.$$
-		bail=$?
+		assert_head_check
+		cd "$TOP_DIR"
 
-		if [ $bail -ne 0 ]; then
-			cat /tmp/guilt.log.$$ >&2
+		# apply the patch if and only if there is something to apply
+		if [ `git-apply --numstat "$p" | wc -l` -gt 0 ]; then
 			if [ "$bail_action" = abort ]; then
-				return $bail
+				reject=""
+			fi
+			git-apply -C$guilt_push_diff_context \
+				$reject "$p" > /dev/null 2> /tmp/guilt.log.$$
+			__push_patch_bail=$?
+
+			if [ $__push_patch_bail -ne 0 ]; then
+				cat /tmp/guilt.log.$$ >&2
+				if [ "$bail_action" = "abort" ]; then
+					rm -f /tmp/guilt.log.$$ /tmp/guilt.msg.$$
+					return $__push_patch_bail
+				fi
 			fi
-		fi
-
-		# FIXME: Path munging is being done, we need to convince
-		# git-apply to just give us list of files with \0 as a
-		# delimiter, and pass -z to git-update-index
-		git-apply --numstat "$p" | cut -f 3- | git-update-index --add --remove --stdin
-	fi
 
-	# grab a commit message out of the patch
-	do_get_header "$p" > /tmp/guilt.msg.$$
+			# FIXME: Path munging is being done, we need to convince
+			# git-apply to just give us list of files with \0 as a
+			# delimiter, and pass -z to git-update-index
+			git-apply --numstat "$p" | cut -f 3- | git-update-index --add --remove --stdin
+		fi
 
-	# make a default commit message if patch doesn't contain one
-	[ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
+		# grab a commit message out of the patch
+		do_get_header "$p" > /tmp/guilt.msg.$$
 
-	# extract a From line from the patch header, and set
-	# GIT_AUTHOR_{NAME,EMAIL}
-	local author_str=`cat "$p" | grep -e '^From: ' | sed -e 's/^From: //'`
-	if [ ! -z "$author_str" ]; then
-		local backup_author_name="$GIT_AUTHOR_NAME"
-		local backup_author_email="$GIT_AUTHOR_EMAIL"
-		GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
-		GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
+		# make a default commit message if patch doesn't contain one
+		[ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
 
-		if [ -z "$GIT_AUTHOR_NAME" ]; then
-			GIT_AUTHOR_NAME=" "
+		# extract a From line from the patch header, and set
+		# GIT_AUTHOR_{NAME,EMAIL}
+		author_str=`sed -n -e '/^From:/ { s/^From: //; p; q }' "$p"`
+		if [ ! -z "$author_str" ]; then
+			GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
+			export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
+                        export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
 		fi
+		export GIT_AUTHOR_DATE="`stat -c %y "$p"`"
+		export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 
-		export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
-	fi
-	local backup_author_date="$GIT_AUTHOR_DATE"
-	local backup_committer_date="$GIT_COMMITTER_DATE"
-	export GIT_AUTHOR_DATE=`stat -c %y "$p"`
-	export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE
-
-	# commit
-	local treeish=`git-write-tree`
-	local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
-	echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
+		# commit
+		treeish=`git-write-tree`
+		commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
+		echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
 
-	# mark patch as applied
-	echo "$commitish:$pname" >> $applied
-
-	cd - 2>&1 >/dev/null
+		# mark patch as applied
+		echo "$commitish:$pname" >> $applied
+	)
 
 	# update references to top, bottom, and base of the stack
 	update_stack_tags
 
-	# restore original GIT_AUTHOR_{NAME,EMAIL}
-	if [ ! -z "$author_str" ]; then
-		if [ ! -z "$backup_author_name" ]; then
-			export GIT_AUTHOR_NAME="$backup_author_name"
-		else
-			unset GIT_AUTHOR_NAME
-		fi
-
-		if [ ! -z "$backup_author_name" ]; then
-			export GIT_AUTHOR_EMAIL="$backup_author_email"
-		else
-			unset GIT_AUTHOR_EMAIL
-		fi
-	fi
-	if [ ! -z "$backup_author_date" ]; then
-		export GIT_AUTHOR_DATE="$backup_author_date"
-	else
-		unset GIT_AUTHOR_DATE
-	fi
-		if [ ! -z "$backup_committer_date" ]; then
-		export GIT_COMMITTER_DATE="$backup_committer_date"
-	else
-		unset GIT_COMMITTER_DATE
-	fi
-
 	rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
-
-	return $bail
+	return $__push_patch_bail
 }
 
 # usage: must_commit_first
-- 
1.5.2.1
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 9 of 22 in “make guilt use standard POSIX shell”
  1. make guilt use standard POSIX shellPierre Habouzit, Jun 14, 2007
  2. Regression test suite needs bash, that's OK.Pierre Habouzit, Jun 14, 2007
  3. guilt(1): Obvious bashisms fixed.Pierre Habouzit, Jun 14, 2007
  4. Josef SipekJun 14, 2007
  5. Pierre HabouzitJun 14, 2007
  6. guilt(1): simplifications...Pierre Habouzit, Jun 14, 2007
  7. Josef SipekJun 14, 2007
  8. Pierre HabouzitJun 14, 2007
  9. guilt(1): reimplement push_patch, using a subshell to avoid locals.Pierre Habouzit, Jun 14, 2007
  10. Easy commands, without bashisms.Pierre Habouzit, Jun 14, 2007
  11. guilt-status(1): Remove bashisms.Pierre Habouzit, Jun 14, 2007
  12. Remove last bashisms from remaining commands.Pierre Habouzit, Jun 14, 2007
  13. Pierre HabouzitJun 14, 2007
  14. Small regresson when series file is empty.Pierre Habouzit, Jun 14, 2007
  15. Josef SipekJun 14, 2007
  16. Pierre HabouzitJun 14, 2007
  17. Pierre HabouzitJun 14, 2007
  18. Josef SipekJun 14, 2007
  19. Pierre HabouzitJun 14, 2007
  20. More regressions fixes.Pierre Habouzit, Jun 14, 2007
  21. Derek FawcusJun 15, 2007
  22. Benjamin SergeantJun 15, 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.