git-am: teach git-am to apply a patch to an unborn branch
People sometimes wonder why they cannot apply a patch that only creates new files to an unborn branch. Signed-off-by: Nanako Shiraishi <nanako3@lavabit.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nanako Shiraishi committed
Apr 10, 2009 at 09:34 UTC
f79d4c8a0f22d7fd25018be846c7e48127ed3200
2 files changed
+39
-5
git-am.sh
+24
-5
@@ -36,6 +36,13 @@ cd_to_toplevel
36
git var GIT_COMMITTER_IDENT >/dev/null ||
37
die "You need to set your committer info first"
38
39
+if git rev-parse --verify -q HEAD >/dev/null
40
+then
41
+ HAS_HEAD=yes
42
+else
43
+ HAS_HEAD=
44
+fi
45
+
46
sq () {
47
for sqarg
48
do
@@ -290,16 +297,26 @@ else
297
: >"$dotest/rebasing"
298
else
299
: >"$dotest/applying"
293
- git update-ref ORIG_HEAD HEAD
300
+ if test -n "$HAS_HEAD"
301
+ then
302
+ git update-ref ORIG_HEAD HEAD
303
+ else
304
+ git update-ref -d ORIG_HEAD >/dev/null 2>&1
305
+ fi
306
fi
307
fi
308
309
case "$resolved" in
310
'')
299
- files=$(git diff-index --cached --name-only HEAD --) || exit
311
+ case "$HAS_HEAD" in
312
+ '')
313
+ files=$(git ls-files) ;;
314
+ ?*)
315
+ files=$(git diff-index --cached --name-only HEAD --) ;;
316
+ esac || exit
317
if test "$files"
318
then
302
- : >"$dotest/dirtyindex"
319
+ test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
320
die "Dirty index: cannot apply patches (dirty: $files)"
321
fi
322
esac
@@ -541,18 +558,20 @@ do
558
fi
559
560
tree=$(git write-tree) &&
544
- parent=$(git rev-parse --verify HEAD) &&
561
commit=$(
562
if test -n "$ignore_date"
563
then
564
GIT_AUTHOR_DATE=
565
fi
566
+ parent=$(git rev-parse --verify -q HEAD) ||
567
+ echo >&2 "applying to an empty history"
568
+
569
if test -n "$committer_date_is_author_date"
570
then
571
GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
572
export GIT_COMMITTER_DATE
573
fi &&
555
- git commit-tree $tree -p $parent <"$dotest/final-commit"
574
+ git commit-tree $tree ${parent:+-p $parent} <"$dotest/final-commit"
575
) &&
576
git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
577
stop_here $this
t/t4150-am.sh
+15
@@ -290,4 +290,19 @@ test_expect_success 'am --ignore-date' '
290
echo "$at" | grep "+0000"
291
'
292
293
+test_expect_success 'am into an unborn branch' '
294
+ rm -fr subdir &&
295
+ mkdir -p subdir &&
296
+ git format-patch --numbered-files -o subdir -1 first &&
297
+ (
298
+ cd subdir &&
299
+ git init &&
300
+ git am 1
301
+ ) &&
302
+ result=$(
303
+ cd subdir && git rev-parse HEAD^{tree}
304
+ ) &&
305
+ test "z$result" = "z$(git rev-parse first^{tree})"
306
+'
307
+
308
test_done