@matheusbsbs / matheus-bagliano / commits / 986e82396a
Fork 0

receive-pack: detect push to current branch of non-bare repo

Pushing into the currently checked out branch of a non-bare repository can be dangerous; the HEAD then loses sync with the index and working tree, and it looks in the receiving repo as if the pushed changes have been reverted in the index (since they were never there in the first place). This patch adds a safety valve that checks for this condition and either generates a warning or denies the update. We trigger the check only on a non-bare repository, since a bare repo does not have a working tree (and in fact, pushing to the HEAD branch is a common workflow for publishing repositories). The behavior is configurable via receive.denyCurrentBranch, defaulting to "warn" so as not to break existing setups (though it may, after a deprecation period, switch to "refuse" by default). For users who know what they are doing and want to silence the warning (e.g., because they have a post-receive hook that reconciles the HEAD and working tree), they can turn off the warning by setting it to false or "ignore". Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 8, 2008 at 20:49 UTC 986e82396ab23b9e5f4eab7183bbf76e7bc756d0
3 files changed +105
Documentation/config.txt
+9
@@ -1198,6 +1198,15 @@ receive.denyNonFastForwards::
1198 even if that push is forced. This configuration variable is
1199 set when initializing a shared repository.
1200
1201 +receive.denyCurrentBranch::
1202 + If set to true or "refuse", receive-pack will deny a ref update
1203 + to the currently checked out branch of a non-bare repository.
1204 + Such a push is potentially dangerous because it brings the HEAD
1205 + out of sync with the index and working tree. If set to "warn",
1206 + print a warning of such a push to stderr, but allow the push to
1207 + proceed. If set to false or "ignore", allow such pushes with no
1208 + message. Defaults to "warn".
1209 +
1210 transfer.unpackLimit::
1211 When `fetch.unpackLimit` or `receive.unpackLimit` are
1212 not set, the value of this variable is used instead.
builtin-receive-pack.c
+59
@@ -11,8 +11,15 @@
11
12 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
13
14 +enum deny_action {
15 + DENY_IGNORE,
16 + DENY_WARN,
17 + DENY_REFUSE,
18 +};
19 +
20 static int deny_deletes = 0;
21 static int deny_non_fast_forwards = 0;
22 +static enum deny_action deny_current_branch = DENY_WARN;
23 static int receive_fsck_objects;
24 static int receive_unpack_limit = -1;
25 static int transfer_unpack_limit = -1;
@@ -22,6 +29,21 @@ static int report_status;
29 static char capabilities[] = " report-status delete-refs ";
30 static int capabilities_sent;
31
32 +static enum deny_action parse_deny_action(const char *var, const char *value)
33 +{
34 + if (value) {
35 + if (!strcasecmp(value, "ignore"))
36 + return DENY_IGNORE;
37 + if (!strcasecmp(value, "warn"))
38 + return DENY_WARN;
39 + if (!strcasecmp(value, "refuse"))
40 + return DENY_REFUSE;
41 + }
42 + if (git_config_bool(var, value))
43 + return DENY_REFUSE;
44 + return DENY_IGNORE;
45 +}
46 +
47 static int receive_pack_config(const char *var, const char *value, void *cb)
48 {
49 if (strcmp(var, "receive.denydeletes") == 0) {
@@ -49,6 +71,11 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
71 return 0;
72 }
73
74 + if (!strcmp(var, "receive.denycurrentbranch")) {
75 + deny_current_branch = parse_deny_action(var, value);
76 + return 0;
77 + }
78 +
79 return git_default_config(var, value, cb);
80 }
81
@@ -173,6 +200,20 @@ static int run_update_hook(struct command *cmd)
200 return hook_status(run_command(&proc), update_hook);
201 }
202
203 +static int is_ref_checked_out(const char *ref)
204 +{
205 + unsigned char sha1[20];
206 + const char *head;
207 +
208 + if (is_bare_repository())
209 + return 0;
210 +
211 + head = resolve_ref("HEAD", sha1, 0, NULL);
212 + if (!head)
213 + return 0;
214 + return !strcmp(head, ref);
215 +}
216 +
217 static const char *update(struct command *cmd)
218 {
219 const char *name = cmd->ref_name;
@@ -186,6 +227,24 @@ static const char *update(struct command *cmd)
227 return "funny refname";
228 }
229
230 + switch (deny_current_branch) {
231 + case DENY_IGNORE:
232 + break;
233 + case DENY_WARN:
234 + if (!is_ref_checked_out(name))
235 + 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))
243 + break;
244 + error("refusing to update checked out branch: %s", name);
245 + return "branch is currently checked out";
246 + }
247 +
248 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
249 error("unpack should have generated %s, "
250 "but I can't find it!", sha1_to_hex(new_sha1));
t/t5516-fetch-push.sh
+37
@@ -486,4 +486,41 @@ test_expect_success 'allow deleting an invalid remote ref' '
486
487 '
488
489 +test_expect_success 'warn on push to HEAD of non-bare repository' '
490 + mk_test heads/master
491 + (cd testrepo &&
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
496 +'
497 +
498 +test_expect_success 'deny push to HEAD of non-bare repository' '
499 + mk_test heads/master
500 + (cd testrepo &&
501 + git checkout master &&
502 + git config receive.denyCurrentBranch true) &&
503 + test_must_fail git push testrepo master
504 +'
505 +
506 +test_expect_success 'allow push to HEAD of bare repository (bare)' '
507 + mk_test heads/master
508 + (cd testrepo &&
509 + git checkout master &&
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
514 +'
515 +
516 +test_expect_success 'allow push to HEAD of non-bare repository (config)' '
517 + mk_test heads/master
518 + (cd testrepo &&
519 + git checkout master &&
520 + git config receive.denyCurrentBranch false
521 + ) &&
522 + git push testrepo master 2>stderr &&
523 + ! grep "warning.*this may cause confusion" stderr
524 +'
525 +
526 test_done