Skip to content

Commit

Permalink
Refuse updating the current branch in a non-bare repository via push
Browse files Browse the repository at this point in the history
This makes git-push refuse pushing into a non-bare repository to update
the current branch by default.  To help people who are used to be able to
do this (and later "reset --hard" it in some other way), an error message
is issued when this refusal is triggered, instructing how to resurrect the
old behaviour.

Hosting sites that do not give the users direct access to customize their
repositories (e.g. repo.or.cz, gitorious, github etc.) may further want to
explicitly set the configuration variable to "refuse" for their customers'
repositories.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Jul 29, 2009
1 parent 6641575 commit acd2a45
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
40 changes: 17 additions & 23 deletions builtin-receive-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,33 +218,27 @@ static int is_ref_checked_out(const char *ref)
return !strcmp(head_name, ref);
}

static char *warn_unconfigured_deny_msg[] = {
"Updating the currently checked out branch may cause confusion,",
"as the index and work tree do not reflect changes that are in HEAD.",
"As a result, you may see the changes you just pushed into it",
"reverted when you run 'git diff' over there, and you may want",
"to run 'git reset --hard' before starting to work to recover.",
static char *refuse_unconfigured_deny_msg[] = {
"By default, updating the current branch in a non-bare repository",
"is denied, because it will make the index and work tree inconsistent",
"with what you pushed, and will require 'git reset --hard' to match",
"the work tree to HEAD.",
"",
"You can set 'receive.denyCurrentBranch' configuration variable to",
"'refuse' in the remote repository to forbid pushing into its",
"current branch."
"'ignore' or 'warn' in the remote repository to allow pushing into",
"its current branch; however, this is not recommended unless you",
"arranged to update its work tree to match what you pushed in some",
"other way.",
"",
"To allow pushing into the current branch, you can set it to 'ignore';",
"but this is not recommended unless you arranged to update its work",
"tree to match what you pushed in some other way.",
"",
"To squelch this message, you can set it to 'warn'.",
"",
"Note that the default will change in a future version of git",
"to refuse updating the current branch unless you have the",
"configuration variable set to either 'ignore' or 'warn'."
"To squelch this message and still keep the default behaviour, set",
"'receive.denyCurrentBranch' configuration variable to 'refuse'."
};

static void warn_unconfigured_deny(void)
static void refuse_unconfigured_deny(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(warn_unconfigured_deny_msg); i++)
warning("%s", warn_unconfigured_deny_msg[i]);
for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
error("%s", refuse_unconfigured_deny_msg[i]);
}

static char *warn_unconfigured_deny_delete_current_msg[] = {
Expand Down Expand Up @@ -290,14 +284,14 @@ static const char *update(struct command *cmd)
switch (deny_current_branch) {
case DENY_IGNORE:
break;
case DENY_UNCONFIGURED:
case DENY_WARN:
warning("updating the current branch");
if (deny_current_branch == DENY_UNCONFIGURED)
warn_unconfigured_deny();
break;
case DENY_REFUSE:
case DENY_UNCONFIGURED:
error("refusing to update checked out branch: %s", name);
if (deny_current_branch == DENY_UNCONFIGURED)
refuse_unconfigured_deny();
return "branch is currently checked out";
}
}
Expand Down
3 changes: 2 additions & 1 deletion t/t5400-send-pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test_expect_success setup '
done &&
git update-ref HEAD "$commit" &&
git clone ./. victim &&
( cd victim && git log ) &&
( cd victim && git config receive.denyCurrentBranch warn && git log ) &&
git update-ref HEAD "$zero" &&
parent=$zero &&
i=0 &&
Expand Down Expand Up @@ -129,6 +129,7 @@ rewound_push_setup() {
cd parent &&
git init &&
echo one >file && git add file && git commit -m one &&
git config receive.denyCurrentBranch warn &&
echo two >file && git commit -a -m two
) &&
git clone parent child &&
Expand Down
1 change: 1 addition & 0 deletions t/t5401-update-hooks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test_expect_success setup '
git update-ref refs/heads/master $commit0 &&
git update-ref refs/heads/tofail $commit1 &&
git clone ./. victim &&
GIT_DIR=victim/.git git config receive.denyCurrentBranch warn &&
GIT_DIR=victim/.git git update-ref refs/heads/tofail $commit1 &&
git update-ref refs/heads/master $commit1 &&
git update-ref refs/heads/tofail $commit0
Expand Down
1 change: 1 addition & 0 deletions t/t5405-send-pack-rewind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test_expect_success setup '
>file1 && git add file1 && test_tick &&
git commit -m Initial &&
git config receive.denyCurrentBranch warn &&
mkdir another && (
cd another &&
Expand Down
1 change: 1 addition & 0 deletions t/t5516-fetch-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mk_empty () {
(
cd testrepo &&
git init &&
git config receive.denyCurrentBranch warn &&
mv .git/hooks .git/hooks-disabled
)
}
Expand Down
3 changes: 2 additions & 1 deletion t/t5517-push-mirror.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ mk_repo_pair () {
mkdir mirror &&
(
cd mirror &&
git init
git init &&
git config receive.denyCurrentBranch warn
) &&
mkdir master &&
(
Expand Down
20 changes: 13 additions & 7 deletions t/t5522-pull-symlink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ fi
#
# The working directory is subdir-link.

mkdir subdir
echo file >subdir/file
git add subdir/file
git commit -q -m file
git clone -q . clone-repo
ln -s clone-repo/subdir/ subdir-link

test_expect_success setup '
mkdir subdir &&
echo file >subdir/file &&
git add subdir/file &&
git commit -q -m file &&
git clone -q . clone-repo &&
ln -s clone-repo/subdir/ subdir-link &&
(
cd clone-repo &&
git config receive.denyCurrentBranch warn
) &&
git config receive.denyCurrentBranch warn
'

# Demonstrate that things work if we just avoid the symlink
#
Expand Down
4 changes: 3 additions & 1 deletion t/t5701-clone-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ test_expect_success 'bundle clone with nonexistent HEAD' '
test_expect_success 'clone empty repository' '
cd "$D" &&
mkdir empty &&
(cd empty && git init) &&
(cd empty &&
git init &&
git config receive.denyCurrentBranch warn) &&
git clone empty empty-clone &&
test_tick &&
(cd empty-clone
Expand Down

0 comments on commit acd2a45

Please sign in to comment.