@matheusbsbs / matheus-bagliano / commits / 3d95d92b9a
Fork 0

receive-pack: explain what to do when push updates the current branch

This makes "git push" issue a more detailed instruction when a user pushes into the current branch of a non-bare repository without having an explicit configuration set to receive.denycurrentbranch. In such a case, it will also tell the user that the default will change to refusal in a future version of git. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 31, 2009 at 17:34 UTC 3d95d92b9a75825b2d9c9bd770d85471b18597f8
2 files changed +46 -18
builtin-receive-pack.c
+43 -15
@@ -12,6 +12,7 @@
12 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
13
14 enum deny_action {
15 + DENY_UNCONFIGURED,
16 DENY_IGNORE,
17 DENY_WARN,
18 DENY_REFUSE,
@@ -19,7 +20,7 @@ enum deny_action {
20
21 static int deny_deletes = 0;
22 static int deny_non_fast_forwards = 0;
22 -static enum deny_action deny_current_branch = DENY_WARN;
23 +static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
24 static int receive_fsck_objects;
25 static int receive_unpack_limit = -1;
26 static int transfer_unpack_limit = -1;
@@ -214,6 +215,35 @@ static int is_ref_checked_out(const char *ref)
215 return !strcmp(head, ref);
216 }
217
218 +static char *warn_unconfigured_deny_msg[] = {
219 + "Updating the currently checked out branch may cause confusion,",
220 + "as the index and work tree do not reflect changes that are in HEAD.",
221 + "As a result, you may see the changes you just pushed into it",
222 + "reverted when you run 'git diff' over there, and you may want",
223 + "to run 'git reset --hard' before starting to work to recover.",
224 + "",
225 + "You can set 'receive.denyCurrentBranch' configuration variable to",
226 + "'refuse' in the remote repository to forbid pushing into its",
227 + "current branch."
228 + "",
229 + "To allow pushing into the current branch, you can set it to 'ignore';",
230 + "but this is not recommended unless you arranged to update its work",
231 + "tree to match what you pushed in some other way.",
232 + "",
233 + "To squelch this message, you can set it to 'warn'.",
234 + "",
235 + "Note that the default will change in a future version of git",
236 + "to refuse updating the current branch unless you have the",
237 + "configuration variable set to either 'ignore' or 'warn'."
238 +};
239 +
240 +static void warn_unconfigured_deny(void)
241 +{
242 + int i;
243 + for (i = 0; i < ARRAY_SIZE(warn_unconfigured_deny_msg); i++)
244 + warning(warn_unconfigured_deny_msg[i]);
245 +}
246 +
247 static const char *update(struct command *cmd)
248 {
249 const char *name = cmd->ref_name;
@@ -227,22 +257,20 @@ static const char *update(struct command *cmd)
257 return "funny refname";
258 }
259
230 - switch (deny_current_branch) {
231 - case DENY_IGNORE:
232 - break;
233 - case DENY_WARN:
234 - if (!is_ref_checked_out(name))
260 + if (is_ref_checked_out(name)) {
261 + switch (deny_current_branch) {
262 + case DENY_IGNORE:
263 break;
236 - warning("updating the currently checked out branch; this may"
237 - " cause confusion,\n"
238 - "as the index and working tree do not reflect changes"
239 - " that are now in HEAD.");
240 - break;
241 - case DENY_REFUSE:
242 - if (!is_ref_checked_out(name))
264 + case DENY_UNCONFIGURED:
265 + case DENY_WARN:
266 + warning("updating the current branch");
267 + if (deny_current_branch == DENY_UNCONFIGURED)
268 + warn_unconfigured_deny();
269 break;
244 - error("refusing to update checked out branch: %s", name);
245 - return "branch is currently checked out";
270 + case DENY_REFUSE:
271 + error("refusing to update checked out branch: %s", name);
272 + return "branch is currently checked out";
273 + }
274 }
275
276 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
t/t5516-fetch-push.sh
+3 -3
@@ -492,7 +492,7 @@ test_expect_success 'warn on push to HEAD of non-bare repository' '
492 git checkout master &&
493 git config receive.denyCurrentBranch warn) &&
494 git push testrepo master 2>stderr &&
495 - grep "warning.*this may cause confusion" stderr
495 + grep "warning: updating the current branch" stderr
496 '
497
498 test_expect_success 'deny push to HEAD of non-bare repository' '
@@ -510,7 +510,7 @@ test_expect_success 'allow push to HEAD of bare repository (bare)' '
510 git config receive.denyCurrentBranch true &&
511 git config core.bare true) &&
512 git push testrepo master 2>stderr &&
513 - ! grep "warning.*this may cause confusion" stderr
513 + ! grep "warning: updating the current branch" stderr
514 '
515
516 test_expect_success 'allow push to HEAD of non-bare repository (config)' '
@@ -520,7 +520,7 @@ test_expect_success 'allow push to HEAD of non-bare repository (config)' '
520 git config receive.denyCurrentBranch false
521 ) &&
522 git push testrepo master 2>stderr &&
523 - ! grep "warning.*this may cause confusion" stderr
523 + ! grep "warning: updating the current branch" stderr
524 '
525
526 test_expect_success 'fetch with branches' '