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

[PATCH v2 5/7] push -s: receiving end

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 8, 2011, 20:01 UTC
Message-ID
<1315512102-19022-6-git-send-email-gitster@pobox.com>
In-Reply-To
<1315512102-19022-1-git-send-email-gitster@pobox.com>

This stores the GPG signed push certificate in the receiving repository using the notes mechanism. The certificate is appended to a note in the refs/notes/signed-push tree for each object that appears on the right hand side of the push certificate, i.e. the object that was pushed to update the tip of a ref.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/receive-pack.c |   72 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 65 insertions(+), 7 deletions(-)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 20b6799..344660e 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -11,6 +11,11 @@
 #include "transport.h"
 #include "string-list.h"
 #include "sha1-array.h"
+#include "gpg-interface.h"
+#include "notes.h"
+#include "notes-merge.h"
+#include "blob.h"
+#include "tag.h"
 
 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
 
@@ -581,6 +586,22 @@ static void check_aliased_updates(struct command *commands)
 	string_list_clear(&ref_list, 0);
 }
 
+static void get_note_text(struct strbuf *buf, struct notes_tree *t,
+			  const unsigned char *object)
+{
+	const unsigned char *sha1 = get_note(t, object);
+	char *text;
+	unsigned long len;
+	enum object_type type;
+
+	if (!sha1)
+		return;
+	text = read_sha1_file(sha1, &type, &len);
+	if (text && len && type == OBJ_BLOB)
+		strbuf_add(buf, text, len);
+	free(text);
+}
+
 static int record_signed_push(char *cert)
 {
 	/*
@@ -592,18 +613,55 @@ static int record_signed_push(char *cert)
 	 * You could also feed the signed push certificate to GPG,
 	 * verify the signer identity, and all the other fun stuff,
 	 * including feeding it to "pre-receive-signature" hook.
-	 *
-	 * Here we just throw it to stderr to demonstrate that the
-	 * codepath is being exercised.
 	 */
+	size_t total, payload;
 	char *cp, *ep;
-	for (cp = cert; *cp; cp = ep) {
+	int ret = 0;
+	struct notes_tree *t;
+	struct strbuf nbuf = STRBUF_INIT;
+
+	if (!cert)
+		return 0;
+
+	init_notes(NULL, "refs/notes/signed-push", NULL, 0);
+	t = &default_notes_tree;
+
+	total = strlen(cert);
+	payload = parse_signature(cert, total);
+	for (cp = cert; cp < cert + payload; cp = ep) {
+		unsigned char sha1[20], nsha1[20];
+
 		ep = strchrnul(cp, '\n');
 		if (*ep == '\n')
 			ep++;
-		fprintf(stderr, "RSP: %.*s", (int)(ep - cp), cp);
+		if (prefixcmp(cp, "Update: "))
+			continue;
+		cp += strlen("Update: ");
+		if (get_sha1_hex(cp, sha1) || cp[40] != ' ')
+			continue;
+
+		get_note_text(&nbuf, t, sha1);
+		if (nbuf.len)
+			strbuf_addch(&nbuf, '\n');
+		strbuf_add(&nbuf, cert, total);
+		if (write_sha1_file(nbuf.buf, nbuf.len, blob_type, nsha1) ||
+		    add_note(t, sha1, nsha1, NULL))
+			ret = error(_("unable to write note object"));
+		strbuf_reset(&nbuf);
 	}
-	return 0;
+
+	if (!ret) {
+		unsigned char commit[20];
+		unsigned char parent[20];
+		struct ref_lock *lock;
+
+		resolve_ref(t->ref, parent, 0, NULL);
+		lock = lock_any_ref_for_update(t->ref, parent, 0);
+		create_notes_commit(t, NULL, "push", commit);
+		ret = write_ref_sha1(lock, commit, "push");
+	}
+	free_notes(t);
+	return ret;
 }
 
 static void execute_commands(struct command *commands, const char *unpacker_error)
@@ -623,7 +681,7 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
 		return;
 	}
 
-	if (push_certificate && record_signed_push(push_certificate)) {
+	if (record_signed_push(push_certificate)) {
 		for (cmd = commands; cmd; cmd = cmd->next)
 			cmd->error_string = "n/a (push signature error)";
 		return;
-- 
1.7.7.rc0.188.g3793ac
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 22 in “"push -s"”
  1. 0/7 "push -s"Junio C Hamano, Sep 8, 2011
  2. 1/7 send-pack: typofix error messageJunio C Hamano, Sep 8, 2011
  3. 2/7 Split GPG interface into its own helper libraryJunio C Hamano, Sep 8, 2011
  4. 3/7 push -s: skeletonJunio C Hamano, Sep 8, 2011
  5. 4/7 push -s: send signed push certificateJunio C Hamano, Sep 8, 2011
  6. 5/7 push -s: receiving endJunio C Hamano, Sep 8, 2011
  7. 6/7 refactor run_receive_hook()Junio C Hamano, Sep 8, 2011
  8. 7/7 push -s: support pre-receive-signature hookJunio C Hamano, Sep 8, 2011
  9. 0/4 Signed pushJunio C Hamano, Sep 9, 2011
  10. 1/4 send-pack: typofix error messageJunio C Hamano, Sep 9, 2011
  11. 2/4 Split GPG interface into its own helper libraryJunio C Hamano, Sep 9, 2011
  12. 3/4 rename "match_refs()" to "match_push_refs()"Junio C Hamano, Sep 9, 2011
  13. 4/4 push -s: signed pushJunio C Hamano, Sep 9, 2011
  14. 4/4 push -s: signed pushJunio C Hamano, Sep 9, 2011
  15. Junio C HamanoSep 10, 2011
  16. Sverre RabbelierSep 10, 2011
  17. Junio C HamanoSep 10, 2011
  18. Ted Ts'oSep 10, 2011
  19. Junio C HamanoSep 11, 2011
  20. Sverre RabbelierSep 11, 2011
  21. Ted Ts'oSep 11, 2011
  22. Robin H. JohnsonSep 10, 2011

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.