git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Pushing to a non-bare repository

From
Junio C Hamano <junkio@cox.net>
Date
Feb 3, 2007, 19:42 UTC
Message-ID
<7vabzv58q4.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<45C459DD.8080201@fs.ei.tum.de>
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
Show 5 quoted lines
> I don't say "Push needs to behave like a fast forward pull",
> because that's wrong.  You can't just change the workdir,
> possibly due to permissions problems.  But push has to abort
> if you try to forward HEAD on the remote, except if it is
> bare, of course.

You can tweak receive-pack.c::update() to do that, perhaps along the lines of this.

---
diff --git a/receive-pack.c b/receive-pack.c
index 7311c82..ccd48e4 100644
--- a/receive-pack.c
+++ b/receive-pack.c
@@ -9,6 +9,7 @@
 
 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
 
+static char *current_branch;
 static int deny_non_fast_forwards = 0;
 static int receive_unpack_limit = -1;
 static int transfer_unpack_limit = -1;
@@ -115,6 +116,18 @@ static int update(struct command *cmd)
 			     name);
 	}
 
+	if (!is_bare_repository()) {
+		/*
+		 * Pusing into a live repository.  updating .git/HEAD
+		 * or the current branch is a no-no...
+		 */
+		if (strncmp(name, "refs/", 5) ||
+		    (current_branch && !strcmp(name, current_branch))) {
+			cmd->error_string = "update current";
+			return error("refusing to update the current branch '%s' in a live repository", name);
+		}
+	}
+
 	strcpy(new_hex, sha1_to_hex(new_sha1));
 	strcpy(old_hex, sha1_to_hex(old_sha1));
 
@@ -436,6 +449,13 @@ int main(int argc, char **argv)
 	else if (0 <= receive_unpack_limit)
 		unpack_limit = receive_unpack_limit;
 
+	if (!is_bare_repository()) {
+		unsigned char h[20];
+		const char *head = resolve_ref("HEAD", h, 0, NULL);
+		if (head && strcmp("HEAD", head))
+			current_branch = xstrdup(head);
+	}
+
 	write_head_info();
 
 	/* EOF */
Previous: Simon 'corecode' SchubertNext: Linus Torvalds
Message 4 of 6 in “Pushing to a non-bare repository”
  1. Steven GrimmFeb 3, 2007
  2. Junio C HamanoFeb 3, 2007
  3. Simon 'corecode' SchubertFeb 3, 2007
  4. Junio C HamanoFeb 3, 2007
  5. Linus TorvaldsFeb 3, 2007
  6. Junio C HamanoFeb 3, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.