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

[RFC PATCH 1/4] Refactor builtin-verify-tag.c

From
Deskin Miller <deskinm@umich.edu>
Date
Nov 24, 2008, 03:23 UTC
Message-ID
<1227497000-8684-2-git-send-email-deskinm@umich.edu>
In-Reply-To
<1227497000-8684-1-git-send-email-deskinm@umich.edu>

builtin-verify-tag.c didn't expose any of its functionality to be used internally. Refactor some of it into new verify-tag.c and expose verify_tag_sha1 able to be called from elsewhere in git.

Signed-off-by: Deskin Miller <deskinm@umich.edu>
---
 Makefile             |    2 +
 builtin-verify-tag.c |   61 ++-------------------------------------
 verify-tag.c         |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++
 verify-tag.h         |   10 ++++++
 4 files changed, 93 insertions(+), 57 deletions(-)
 create mode 100644 verify-tag.c
 create mode 100644 verify-tag.h
diff --git a/Makefile b/Makefile
index 35adafa..b372aa4 100644
--- a/Makefile
+++ b/Makefile
@@ -392,6 +392,7 @@ LIB_H += tree-walk.h
 LIB_H += unpack-trees.h
 LIB_H += userdiff.h
 LIB_H += utf8.h
+LIB_H += verify-tag.h
 LIB_H += wt-status.h
 
 LIB_OBJS += abspath.o
@@ -490,6 +491,7 @@ LIB_OBJS += unpack-trees.o
 LIB_OBJS += userdiff.o
 LIB_OBJS += usage.o
 LIB_OBJS += utf8.o
+LIB_OBJS += verify-tag.o
 LIB_OBJS += walker.o
 LIB_OBJS += wrapper.o
 LIB_OBJS += write_or_die.o
diff --git a/builtin-verify-tag.c b/builtin-verify-tag.c
index 729a159..dd350e8 100644
--- a/builtin-verify-tag.c
+++ b/builtin-verify-tag.c
@@ -7,65 +7,16 @@
  */
 #include "cache.h"
 #include "builtin.h"
-#include "tag.h"
-#include "run-command.h"
+#include "verify-tag.h"
 #include <signal.h>
 
 static const char builtin_verify_tag_usage[] =
 		"git verify-tag [-v|--verbose] <tag>...";
 
-#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
-
-static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
-{
-	struct child_process gpg;
-	const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL};
-	char path[PATH_MAX], *eol;
-	size_t len;
-	int fd, ret;
-
-	fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
-	if (fd < 0)
-		return error("could not create temporary file '%s': %s",
-						path, strerror(errno));
-	if (write_in_full(fd, buf, size) < 0)
-		return error("failed writing temporary file '%s': %s",
-						path, strerror(errno));
-	close(fd);
-
-	/* find the length without signature */
-	len = 0;
-	while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
-		eol = memchr(buf + len, '\n', size - len);
-		len += eol ? eol - (buf + len) + 1 : size - len;
-	}
-	if (verbose)
-		write_in_full(1, buf, len);
-
-	memset(&gpg, 0, sizeof(gpg));
-	gpg.argv = args_gpg;
-	gpg.in = -1;
-	args_gpg[2] = path;
-	if (start_command(&gpg)) {
-		unlink(path);
-		return error("could not run gpg.");
-	}
-
-	write_in_full(gpg.in, buf, len);
-	close(gpg.in);
-	ret = finish_command(&gpg);
-
-	unlink(path);
-
-	return ret;
-}
-
 static int verify_tag(const char *name, int verbose)
 {
 	enum object_type type;
 	unsigned char sha1[20];
-	char *buf;
-	unsigned long size;
 	int ret;
 
 	if (get_sha1(name, sha1))
@@ -76,13 +27,9 @@ static int verify_tag(const char *name, int verbose)
 		return error("%s: cannot verify a non-tag object of type %s.",
 				name, typename(type));
 
-	buf = read_sha1_file(sha1, &type, &size);
-	if (!buf)
-		return error("%s: unable to read file.", name);
-
-	ret = run_gpg_verify(buf, size, verbose);
-
-	free(buf);
+	ret = verify_tag_sha1(sha1, verbose);
+	if (ret)
+		error("Failed to verify %s.", name);
 	return ret;
 }
 
diff --git a/verify-tag.c b/verify-tag.c
new file mode 100644
index 0000000..c9be331
--- /dev/null
+++ b/verify-tag.c
@@ -0,0 +1,77 @@
+/*
+ * Internals for "git verify-tag"
+ *
+ * Copyright (c) 2008 Deskin Miller <deskinm@umich.edu>
+ *
+ */
+#include "cache.h"
+#include "object.h"
+#include "run-command.h"
+
+#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
+
+static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
+{
+	struct child_process gpg;
+	const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL};
+	char path[PATH_MAX], *eol;
+	size_t len;
+	int fd, ret;
+
+	fd = git_mkstemp(path, PATH_MAX, ".git_vtag_tmpXXXXXX");
+	if (fd < 0)
+		return error("could not create temporary file '%s': %s",
+						path, strerror(errno));
+	if (write_in_full(fd, buf, size) < 0)
+		return error("failed writing temporary file '%s': %s",
+						path, strerror(errno));
+	close(fd);
+
+	/* find the length without signature */
+	len = 0;
+	while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
+		eol = memchr(buf + len, '\n', size - len);
+		len += eol ? eol - (buf + len) + 1 : size - len;
+	}
+	if (verbose)
+		write_in_full(1, buf, len);
+
+	memset(&gpg, 0, sizeof(gpg));
+	gpg.argv = args_gpg;
+	gpg.in = -1;
+	args_gpg[2] = path;
+	if (start_command(&gpg)) {
+		unlink(path);
+		return error("could not run gpg.");
+	}
+
+	write_in_full(gpg.in, buf, len);
+	close(gpg.in);
+	ret = finish_command(&gpg);
+
+	unlink(path);
+
+	return ret;
+}
+
+int verify_tag_sha1(const unsigned char *sha1, int verbose)
+{
+	enum object_type type;
+	char *buf;
+	unsigned long size;
+	int ret;
+
+	type = sha1_object_info(sha1, NULL);
+	if (type != OBJ_TAG)
+		return error("Cannot verify a non-tag object of type %s.",
+				typename(type));
+
+	buf = read_sha1_file(sha1, &type, &size);
+	if (!buf)
+		return error("Cnable to read file.");
+
+	ret = run_gpg_verify(buf, size, verbose);
+
+	free(buf);
+	return ret;
+}
diff --git a/verify-tag.h b/verify-tag.h
new file mode 100644
index 0000000..45bdca7
--- /dev/null
+++ b/verify-tag.h
@@ -0,0 +1,10 @@
+#ifndef VERIFY_TAG_H
+#define VERIFY_TAG_H
+/*
+ * Internals for "git verify-tag"
+ *
+ * Copyright (c) 2008 Deskin Miller <deskinm@umich.edu>
+ */
+extern int verify_tag_sha1(const unsigned char *sha1, int verbose);
+
+#endif /* VERIFY_TAG_H */
-- 
1.6.0.4.770.ga8394
Previous: Deskin MillerNext: Deskin Miller
Message 2 of 16 in “Teach git fetch to verify signed tags automatically”
  1. 0/4 Teach git fetch to verify signed tags automaticallyDeskin Miller, Nov 24, 2008
  2. 1/4 Refactor builtin-verify-tag.cDeskin Miller, Nov 24, 2008
  3. 2/4 verify-tag.c: ignore SIGPIPE around gpg invocationDeskin Miller, Nov 24, 2008
  4. 3/4 verify-tag.c: suppress gpg output if askedDeskin Miller, Nov 24, 2008
  5. 4/4 Make git fetch verify signed tagsDeskin Miller, Nov 24, 2008
  6. Johannes SchindelinNov 24, 2008
  7. Deskin MillerNov 28, 2008
  8. Johannes SchindelinNov 24, 2008
  9. Deskin MillerNov 28, 2008
  10. Junio C HamanoNov 24, 2008
  11. Junio C HamanoNov 24, 2008
  12. Deskin MillerNov 28, 2008
  13. Johannes SchindelinNov 28, 2008
  14. Johannes SchindelinNov 24, 2008
  15. Deskin MillerNov 28, 2008
  16. Junio C HamanoNov 28, 2008

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.