{"thread":{"id":"18420","subject":"[PATCH] git-am: teach git-am to apply a patch to an unborn branch","startedAt":"2009-03-19T22:12:31Z","lastAt":"2009-03-20T04:37:52Z","messageCount":3,"participants":["Nanako Shiraishi","Junio C Hamano","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"108603","messageId":"20090320071231.6117@nanako3.lavabit.com","threadId":"18420","inReplyTo":null,"subject":"[PATCH] git-am: teach git-am to apply a patch to an unborn branch","fromName":"Nanako Shiraishi","fromEmail":"nanako3@lavabit.com","sentAt":"2009-03-19T22:12:31Z","receivedAt":"2009-03-19T22:12:31Z","isPatch":true,"sender":{"key":"nanako3@lavabit.com","avatar":"https://gravatar.com/avatar/3777b9e201c5883a62b1a6fdf7c53f2d712d1d80989146063ea861e33aad72a8?d=mp&s=160"},"body":"Signed-off-by: Nanako Shiraishi <nanako3@lavabit.com>\n---\n git-am.sh     |   33 ++++++++++++++++++++++++++++-----\n t/t4150-am.sh |   15 +++++++++++++++\n 2 files changed, 43 insertions(+), 5 deletions(-)\n\ndiff --git a/git-am.sh b/git-am.sh\nindex d339075..c21642b 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -36,6 +36,13 @@ cd_to_toplevel\n git var GIT_COMMITTER_IDENT >/dev/null ||\n \tdie \"You need to set your committer info first\"\n \n+if git rev-parse --verify -q HEAD >/dev/null\n+then\n+\tHAS_HEAD=yes\n+else\n+\tHAS_HEAD=\n+fi\n+\n sq () {\n \tfor sqarg\n \tdo\n@@ -290,16 +297,26 @@ else\n \t\t: >\"$dotest/rebasing\"\n \telse\n \t\t: >\"$dotest/applying\"\n-\t\tgit update-ref ORIG_HEAD HEAD\n+\t\tif test -n \"$HAS_HEAD\"\n+\t\tthen\n+\t\t\tgit update-ref ORIG_HEAD HEAD\n+\t\telse\n+\t\t\tgit update-ref -d ORIG_HEAD >/dev/null 2>&1\n+\t\tfi\n \tfi\n fi\n \n case \"$resolved\" in\n '')\n-\tfiles=$(git diff-index --cached --name-only HEAD --) || exit\n+\tif test -n \"$HAS_HEAD\"\n+\tthen\n+\t\tfiles=$(git diff-index --cached --name-only HEAD --)\n+\telse\n+\t\tfiles=$(git ls-files)\n+\tfi || exit\n \tif test \"$files\"\n \tthen\n-\t\t: >\"$dotest/dirtyindex\"\n+\t\ttest -n \"$HAS_HEAD\" && : >\"$dotest/dirtyindex\"\n \t\tdie \"Dirty index: cannot apply patches (dirty: $files)\"\n \tfi\n esac\n@@ -541,7 +558,13 @@ do\n \tfi\n \n \ttree=$(git write-tree) &&\n-\tparent=$(git rev-parse --verify HEAD) &&\n+\tif parent=$(git rev-parse --verify -q HEAD)\n+\tthen\n+\t\tpparent=\"-p $parent\"\n+\telse\n+\t\techo >&2 \"applying to an empty history\"\n+\t\tparent= pparent=\n+\tfi &&\n \tcommit=$(\n \t\tif test -n \"$ignore_date\"\n \t\tthen\n@@ -552,7 +575,7 @@ do\n \t\t\tGIT_COMMITTER_DATE=\"$GIT_AUTHOR_DATE\"\n \t\t\texport GIT_COMMITTER_DATE\n \t\tfi &&\n-\t\tgit commit-tree $tree -p $parent <\"$dotest/final-commit\"\n+\t\tgit commit-tree $tree $pparent <\"$dotest/final-commit\"\n \t) &&\n \tgit update-ref -m \"$GIT_REFLOG_ACTION: $FIRSTLINE\" HEAD $commit $parent ||\n \tstop_here $this\ndiff --git a/t/t4150-am.sh b/t/t4150-am.sh\nindex 5e65afa..b97d102 100755\n--- a/t/t4150-am.sh\n+++ b/t/t4150-am.sh\n@@ -290,4 +290,19 @@ test_expect_success 'am --ignore-date' '\n \techo \"$at\" | grep \"+0000\"\n '\n \n+test_expect_success 'am in an unborn branch' '\n+\trm -fr subdir &&\n+\tmkdir -p subdir &&\n+\tgit format-patch --numbered-files -o subdir -1 first &&\n+\t(\n+\t\tcd subdir &&\n+\t\tgit init &&\n+\t\tgit am 1\n+\t) &&\n+\tresult=$(\n+\t\tcd subdir && git rev-parse HEAD^{tree}\n+\t) &&\n+\ttest \"z$result\" = \"z$(git rev-parse first^{tree})\"\n+'\n+\n test_done\n-- \n1.6.2.1\n\n-- \nNanako Shiraishi\nhttp://ivory.ap.teacup.com/nanako3/\n"},{"id":"108606","messageId":"7viqm56tnm.fsf@gitster.siamese.dyndns.org","threadId":"18420","inReplyTo":"20090320071231.6117@nanako3.lavabit.com","subject":"Re: [PATCH] git-am: teach git-am to apply a patch to an unborn branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-03-19T22:21:17Z","receivedAt":"2009-03-19T22:21:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nanako Shiraishi <nanako3@lavabit.com> writes:\n\n> Signed-off-by: Nanako Shiraishi <nanako3@lavabit.com>\n> ---\n>  git-am.sh     |   33 ++++++++++++++++++++++++++++-----\n>  t/t4150-am.sh |   15 +++++++++++++++\n>  2 files changed, 43 insertions(+), 5 deletions(-)\n>\n> diff --git a/git-am.sh b/git-am.sh\n> index d339075..c21642b 100755\n> --- a/git-am.sh\n> +++ b/git-am.sh\n> @@ -36,6 +36,13 @@ cd_to_toplevel\n>  git var GIT_COMMITTER_IDENT >/dev/null ||\n>  \tdie \"You need to set your committer info first\"\n>  \n> +if git rev-parse --verify -q HEAD >/dev/null\n> +then\n> +\tHAS_HEAD=yes\n> +else\n> +\tHAS_HEAD=\n> +fi\n> +\n\nProbably nicer this way than Peff's as I suspect we would need to special\ncase the unborn-branch case a lot more.  Have you tried --skip and --abort\ncodepaths with your patch?\n\n>  sq () {\n>  \tfor sqarg\n>  \tdo\n> @@ -290,16 +297,26 @@ else\n>  \t\t: >\"$dotest/rebasing\"\n>  \telse\n>  \t\t: >\"$dotest/applying\"\n> -\t\tgit update-ref ORIG_HEAD HEAD\n> +\t\tif test -n \"$HAS_HEAD\"\n> +\t\tthen\n> +\t\t\tgit update-ref ORIG_HEAD HEAD\n> +\t\telse\n> +\t\t\tgit update-ref -d ORIG_HEAD >/dev/null 2>&1\n> +\t\tfi\n\nSo is this part.\n\n>  \tfi\n>  fi\n>  \n>  case \"$resolved\" in\n>  '')\n> -\tfiles=$(git diff-index --cached --name-only HEAD --) || exit\n> +\tif test -n \"$HAS_HEAD\"\n> +\tthen\n> +\t\tfiles=$(git diff-index --cached --name-only HEAD --)\n> +\telse\n> +\t\tfiles=$(git ls-files)\n> +\tfi || exit\n>  \tif test \"$files\"\n>  \tthen\n> -\t\t: >\"$dotest/dirtyindex\"\n> +\t\ttest -n \"$HAS_HEAD\" && : >\"$dotest/dirtyindex\"\n>  \t\tdie \"Dirty index: cannot apply patches (dirty: $files)\"\n>  \tfi\n>  esac\n\nAnd here...\n\n> @@ -541,7 +558,13 @@ do\n>  \tfi\n>  \n>  \ttree=$(git write-tree) &&\n> -\tparent=$(git rev-parse --verify HEAD) &&\n> +\tif parent=$(git rev-parse --verify -q HEAD)\n> +\tthen\n> +\t\tpparent=\"-p $parent\"\n> +\telse\n> +\t\techo >&2 \"applying to an empty history\"\n> +\t\tparent= pparent=\n> +\tfi &&\n>  \tcommit=$(\n>  \t\tif test -n \"$ignore_date\"\n>  \t\tthen\n> @@ -552,7 +575,7 @@ do\n>  \t\t\tGIT_COMMITTER_DATE=\"$GIT_AUTHOR_DATE\"\n>  \t\t\texport GIT_COMMITTER_DATE\n>  \t\tfi &&\n> -\t\tgit commit-tree $tree -p $parent <\"$dotest/final-commit\"\n> +\t\tgit commit-tree $tree $pparent <\"$dotest/final-commit\"\n\nPeff's ${a+something $a} trick looks more \"expert\" here ;-).\n\n> diff --git a/t/t4150-am.sh b/t/t4150-am.sh\n> index 5e65afa..b97d102 100755\n> --- a/t/t4150-am.sh\n> +++ b/t/t4150-am.sh\n> @@ -290,4 +290,19 @@ test_expect_success 'am --ignore-date' '\n>  \techo \"$at\" | grep \"+0000\"\n>  '\n>  \n> +test_expect_success 'am in an unborn branch' '\n> +\trm -fr subdir &&\n> +\tmkdir -p subdir &&\n> +\tgit format-patch --numbered-files -o subdir -1 first &&\n> +\t(\n> +\t\tcd subdir &&\n> +\t\tgit init &&\n> +\t\tgit am 1\n> +\t) &&\n> +\tresult=$(\n> +\t\tcd subdir && git rev-parse HEAD^{tree}\n> +\t) &&\n> +\ttest \"z$result\" = \"z$(git rev-parse first^{tree})\"\n> +'\n> +\n\nLooks good.\n"},{"id":"108648","messageId":"20090320043752.GA27160@coredump.intra.peff.net","threadId":"18420","inReplyTo":"20090320071231.6117@nanako3.lavabit.com","subject":"Re: [PATCH] git-am: teach git-am to apply a patch to an unborn branch","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-03-20T04:37:52Z","receivedAt":"2009-03-20T04:37:52Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Mar 20, 2009 at 07:12:31AM +0900, Nanako Shiraishi wrote:\n\n> Signed-off-by: Nanako Shiraishi <nanako3@lavabit.com>\n\nThanks for picking up this topic. It looks sane overall.\n\n> -\tparent=$(git rev-parse --verify HEAD) &&\n> +\tif parent=$(git rev-parse --verify -q HEAD)\n> +\tthen\n> +\t\tpparent=\"-p $parent\"\n> +\telse\n> +\t\techo >&2 \"applying to an empty history\"\n> +\t\tparent= pparent=\n> +\tfi &&\n\nNote that the '&&' here is now pointless, since one side of the\nconditional will always be true. We are losing the ability to detect\nerrors besides \"HEAD does not point to a valid object\", but I don't\nthink there is a way to tell the difference from the exit code of\nrev-parse.\n\n-Peff\n"}]}