Skip to content

Commit fa9aaa8

Browse files
committed
Merge branch 'jc/update-instead-into-void'
A push into an unborn branch, with "receive.denyCurrentBranch" set to "updateInstead", did not check out the working tree as expected. * jc/update-instead-into-void: push-to-deploy: allow pushing into an unborn branch and updating it
2 parents d2ae751 + 1a51b52 commit fa9aaa8

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

builtin/receive-pack.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,22 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
743743
return 0;
744744
}
745745

746+
/*
747+
* NEEDSWORK: we should consolidate various implementions of "are we
748+
* on an unborn branch?" test into one, and make the unified one more
749+
* robust. !get_sha1() based check used here and elsewhere would not
750+
* allow us to tell an unborn branch from corrupt ref, for example.
751+
* For the purpose of fixing "deploy-to-update does not work when
752+
* pushing into an empty repository" issue, this should suffice for
753+
* now.
754+
*/
755+
static int head_has_history(void)
756+
{
757+
unsigned char sha1[20];
758+
759+
return !get_sha1("HEAD", sha1);
760+
}
761+
746762
static const char *push_to_deploy(unsigned char *sha1,
747763
struct argv_array *env,
748764
const char *work_tree)
@@ -755,7 +771,7 @@ static const char *push_to_deploy(unsigned char *sha1,
755771
};
756772
const char *diff_index[] = {
757773
"diff-index", "--quiet", "--cached", "--ignore-submodules",
758-
"HEAD", "--", NULL
774+
NULL, "--", NULL
759775
};
760776
const char *read_tree[] = {
761777
"read-tree", "-u", "-m", NULL, NULL
@@ -782,6 +798,9 @@ static const char *push_to_deploy(unsigned char *sha1,
782798
if (run_command(&child))
783799
return "Working directory has unstaged changes";
784800

801+
/* diff-index with either HEAD or an empty tree */
802+
diff_index[4] = head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX;
803+
785804
child_process_init(&child);
786805
child.argv = diff_index;
787806
child.env = env->argv;

t/t5516-fetch-push.sh

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,8 +1437,22 @@ test_expect_success 'receive.denyCurrentBranch = updateInstead' '
14371437
test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
14381438
git diff --quiet &&
14391439
test fifth = "$(cat path3)"
1440-
)
1440+
) &&
14411441
1442+
# (5) push into void
1443+
rm -fr void &&
1444+
git init void &&
1445+
(
1446+
cd void &&
1447+
git config receive.denyCurrentBranch updateInstead
1448+
) &&
1449+
git push void master &&
1450+
(
1451+
cd void &&
1452+
test $(git -C .. rev-parse master) = $(git rev-parse HEAD) &&
1453+
git diff --quiet &&
1454+
git diff --cached --quiet
1455+
)
14421456
'
14431457

14441458
test_expect_success 'updateInstead with push-to-checkout hook' '
@@ -1501,6 +1515,45 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
15011515
test "$(cat path5)" = irrelevant &&
15021516
test "$(git diff --name-only --cached HEAD)" = path5 &&
15031517
test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
1518+
) &&
1519+
1520+
# push into void
1521+
rm -fr void &&
1522+
git init void &&
1523+
(
1524+
cd void &&
1525+
git config receive.denyCurrentBranch updateInstead &&
1526+
write_script .git/hooks/push-to-checkout <<-\EOF
1527+
if git rev-parse --quiet --verify HEAD
1528+
then
1529+
has_head=yes
1530+
echo >&2 updating from $(git rev-parse HEAD)
1531+
else
1532+
has_head=no
1533+
echo >&2 pushing into void
1534+
fi
1535+
echo >&2 updating to "$1"
1536+
1537+
git update-index -q --refresh &&
1538+
case "$has_head" in
1539+
yes)
1540+
git read-tree -u -m HEAD "$1" ;;
1541+
no)
1542+
git read-tree -u -m "$1" ;;
1543+
esac || {
1544+
status=$?
1545+
echo >&2 read-tree failed
1546+
exit $status
1547+
}
1548+
EOF
1549+
) &&
1550+
1551+
git push void master &&
1552+
(
1553+
cd void &&
1554+
git diff --quiet &&
1555+
git diff --cached --quiet &&
1556+
test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
15041557
)
15051558
'
15061559

0 commit comments

Comments
 (0)