Skip to content

Commit acd2a45

Browse files
committed
Refuse updating the current branch in a non-bare repository via push
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 <[email protected]>
1 parent 6641575 commit acd2a45

File tree

8 files changed

+40
-33
lines changed

8 files changed

+40
-33
lines changed

builtin-receive-pack.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,27 @@ static int is_ref_checked_out(const char *ref)
218218
return !strcmp(head_name, ref);
219219
}
220220

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

243-
static void warn_unconfigured_deny(void)
237+
static void refuse_unconfigured_deny(void)
244238
{
245239
int i;
246-
for (i = 0; i < ARRAY_SIZE(warn_unconfigured_deny_msg); i++)
247-
warning("%s", warn_unconfigured_deny_msg[i]);
240+
for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
241+
error("%s", refuse_unconfigured_deny_msg[i]);
248242
}
249243

250244
static char *warn_unconfigured_deny_delete_current_msg[] = {
@@ -290,14 +284,14 @@ static const char *update(struct command *cmd)
290284
switch (deny_current_branch) {
291285
case DENY_IGNORE:
292286
break;
293-
case DENY_UNCONFIGURED:
294287
case DENY_WARN:
295288
warning("updating the current branch");
296-
if (deny_current_branch == DENY_UNCONFIGURED)
297-
warn_unconfigured_deny();
298289
break;
299290
case DENY_REFUSE:
291+
case DENY_UNCONFIGURED:
300292
error("refusing to update checked out branch: %s", name);
293+
if (deny_current_branch == DENY_UNCONFIGURED)
294+
refuse_unconfigured_deny();
301295
return "branch is currently checked out";
302296
}
303297
}

t/t5400-send-pack.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test_expect_success setup '
3232
done &&
3333
git update-ref HEAD "$commit" &&
3434
git clone ./. victim &&
35-
( cd victim && git log ) &&
35+
( cd victim && git config receive.denyCurrentBranch warn && git log ) &&
3636
git update-ref HEAD "$zero" &&
3737
parent=$zero &&
3838
i=0 &&
@@ -129,6 +129,7 @@ rewound_push_setup() {
129129
cd parent &&
130130
git init &&
131131
echo one >file && git add file && git commit -m one &&
132+
git config receive.denyCurrentBranch warn &&
132133
echo two >file && git commit -a -m two
133134
) &&
134135
git clone parent child &&

t/t5401-update-hooks.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ test_expect_success setup '
1818
git update-ref refs/heads/master $commit0 &&
1919
git update-ref refs/heads/tofail $commit1 &&
2020
git clone ./. victim &&
21+
GIT_DIR=victim/.git git config receive.denyCurrentBranch warn &&
2122
GIT_DIR=victim/.git git update-ref refs/heads/tofail $commit1 &&
2223
git update-ref refs/heads/master $commit1 &&
2324
git update-ref refs/heads/tofail $commit0

t/t5405-send-pack-rewind.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_expect_success setup '
88
99
>file1 && git add file1 && test_tick &&
1010
git commit -m Initial &&
11+
git config receive.denyCurrentBranch warn &&
1112
1213
mkdir another && (
1314
cd another &&

t/t5516-fetch-push.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mk_empty () {
1212
(
1313
cd testrepo &&
1414
git init &&
15+
git config receive.denyCurrentBranch warn &&
1516
mv .git/hooks .git/hooks-disabled
1617
)
1718
}

t/t5517-push-mirror.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ mk_repo_pair () {
1919
mkdir mirror &&
2020
(
2121
cd mirror &&
22-
git init
22+
git init &&
23+
git config receive.denyCurrentBranch warn
2324
) &&
2425
mkdir master &&
2526
(

t/t5522-pull-symlink.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ fi
2020
#
2121
# The working directory is subdir-link.
2222

23-
mkdir subdir
24-
echo file >subdir/file
25-
git add subdir/file
26-
git commit -q -m file
27-
git clone -q . clone-repo
28-
ln -s clone-repo/subdir/ subdir-link
29-
23+
test_expect_success setup '
24+
mkdir subdir &&
25+
echo file >subdir/file &&
26+
git add subdir/file &&
27+
git commit -q -m file &&
28+
git clone -q . clone-repo &&
29+
ln -s clone-repo/subdir/ subdir-link &&
30+
(
31+
cd clone-repo &&
32+
git config receive.denyCurrentBranch warn
33+
) &&
34+
git config receive.denyCurrentBranch warn
35+
'
3036

3137
# Demonstrate that things work if we just avoid the symlink
3238
#

t/t5701-clone-local.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ test_expect_success 'bundle clone with nonexistent HEAD' '
119119
test_expect_success 'clone empty repository' '
120120
cd "$D" &&
121121
mkdir empty &&
122-
(cd empty && git init) &&
122+
(cd empty &&
123+
git init &&
124+
git config receive.denyCurrentBranch warn) &&
123125
git clone empty empty-clone &&
124126
test_tick &&
125127
(cd empty-clone

0 commit comments

Comments
 (0)