[RFC] Allow git-am to operate in an empty git repo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Try not to error out if we are in an emtpy git repo. This could be usefull
for exporting all your commits as emails and importing them latter with
git-am in a newly created repo.

Signed-off-by: Peter Baumann <waste.manager@xxxxxx>
---
It was brought up to my attention on IRC by gitster that git-am doesn't
work if you are on an unborn branch, so this is a first try to fix it.

Please have a look at the first few lines, because I'm not sure how to
handle a detached HEAD.

-Peter

 git-am.sh |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/git-am.sh b/git-am.sh
index 6809aa0..408d19e 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -108,6 +108,13 @@ prec=4
 dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary=
 resolvemsg= resume=
 git_apply_opt=
+empty_repo=
+
+# check if we are trying to import a patch series in an empty git repo
+on_branch=$(git symbolic-ref -q HEAD)
+if [ ! -e "${GIT_DIR}/${on_branch}" ]; then
+	empty_repo=t
+fi
 
 while case "$#" in 0) break;; esac
 do
@@ -214,8 +221,8 @@ else
 	echo 1 >"$dotest/next"
 fi
 
-case "$resolved" in
-'')
+case "$resolved,$empty_repo" in
+',')
 	files=$(git diff-index --cached --name-only HEAD) || exit
 	if [ "$files" ]; then
 	   echo "Dirty index: cannot apply patches (dirty: $files)" >&2
@@ -453,10 +460,21 @@ do
 		"$GIT_DIR"/hooks/pre-applypatch || stop_here $this
 	fi
 
+	parent=
+	parent_opt=
+	case "$empty_repo" in
+	'')
+		echo "empty_repo"
+		parent=$(git rev-parse --verify HEAD) &&
+		parent_opt="-p $parent"
+		;;
+	't')
+		empty_repo=
+		;;
+	esac &&
 	tree=$(git write-tree) &&
 	echo Wrote tree $tree &&
-	parent=$(git rev-parse --verify HEAD) &&
-	commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
+	commit=$(git commit-tree $tree $parent_opt <"$dotest/final-commit") &&
 	echo Committed: $commit &&
 	git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
 	stop_here $this
-- 
1.5.3.1.40.g6972
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux