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@xxxxxxxxx> --- 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@xxxxxxxxx> + * + */ +#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@xxxxxxxxx> + */ +extern int verify_tag_sha1(const unsigned char *sha1, int verbose); + +#endif /* VERIFY_TAG_H */ -- 1.6.0.4.770.ga8394 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html