{"thread":{"id":"18818","subject":"[PATCH] git-am: teach git-am to apply a patch to an unborn branch","startedAt":"2009-04-10T00:34:42Z","lastAt":"2009-04-13T03:46:46Z","messageCount":2,"participants":["Nanako Shiraishi","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"110976","messageId":"20090410093442.6117@nanako3.lavabit.com","threadId":"18818","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-04-10T00:34:42Z","receivedAt":"2009-04-10T00:34:42Z","isPatch":true,"sender":{"key":"nanako3@lavabit.com","avatar":"https://gravatar.com/avatar/3777b9e201c5883a62b1a6fdf7c53f2d712d1d80989146063ea861e33aad72a8?d=mp&s=160"},"body":"People sometimes wonder why they cannot apply a patch that only\ncreates new files to an unborn branch.\n\nSigned-off-by: Nanako Shiraishi <nanako3@lavabit.com>\n---\n git-am.sh     |   29 ++++++++++++++++++++++++-----\n t/t4150-am.sh |   15 +++++++++++++++\n 2 files changed, 39 insertions(+), 5 deletions(-)\n\ndiff --git a/git-am.sh b/git-am.sh\nindex d339075..774383f 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+\tcase \"$HAS_HEAD\" in\n+\t'')\n+\t\tfiles=$(git ls-files) ;;\n+\t?*)\n+\t\tfiles=$(git diff-index --cached --name-only HEAD --) ;;\n+\tesac || 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,18 +558,20 @@ do\n \tfi\n \n \ttree=$(git write-tree) &&\n-\tparent=$(git rev-parse --verify HEAD) &&\n \tcommit=$(\n \t\tif test -n \"$ignore_date\"\n \t\tthen\n \t\t\tGIT_AUTHOR_DATE=\n \t\tfi\n+\t\tparent=$(git rev-parse --verify -q HEAD) ||\n+\t\techo >&2 \"applying to an empty history\"\n+\n \t\tif test -n \"$committer_date_is_author_date\"\n \t\tthen\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 ${parent:+-p $parent} <\"$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 into 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.2\n\n-- \nNanako Shiraishi\nhttp://ivory.ap.teacup.com/nanako3/\n"},{"id":"111157","messageId":"7vprfhw6zd.fsf@gitster.siamese.dyndns.org","threadId":"18818","inReplyTo":"20090410093442.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-04-13T03:46:46Z","receivedAt":"2009-04-13T03:46:46Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.\n"}]}