From 4c0ea82da3cd35eff50377cab78208c8ef57933b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 4 Nov 2011 17:00:03 -0700 Subject: fmt-merge-msg: avoid early returns In various places in the codepath, the program tries to return early assuming there is no more work needed. That is generally untrue when over time new features are added. Signed-off-by: Junio C Hamano --- builtin/fmt-merge-msg.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'builtin/fmt-merge-msg.c') diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c index 7e2f22589..7b492f917 100644 --- a/builtin/fmt-merge-msg.c +++ b/builtin/fmt-merge-msg.c @@ -26,6 +26,7 @@ static int fmt_merge_msg_config(const char *key, const char *value, void *cb) return 0; } +/* merge data per repository where the merged tips came from */ struct src_data { struct string_list branch, tag, r_branch, generic; int head_status; @@ -71,6 +72,11 @@ static int handle_line(char *line) line[len - 1] = 0; line += 42; + /* + * At this point, line points at the beginning of comment e.g. + * "branch 'frotz' of git://that/repository.git". + * Find the repository name and point it with src. + */ src = strstr(line, " of "); if (src) { *src = 0; @@ -283,10 +289,7 @@ static int do_fmt_merge_msg(int merge_title, struct strbuf *in, die ("Error in line %d: %.*s", i, len, p); } - if (!srcs.nr) - return 0; - - if (merge_title) + if (merge_title && srcs.nr) do_fmt_merge_msg_title(out, current_branch); if (shortlog_len) { @@ -306,6 +309,8 @@ static int do_fmt_merge_msg(int merge_title, struct strbuf *in, shortlog(origins.items[i].string, origins.items[i].util, head, &rev, shortlog_len, out); } + if (out->len && out->buf[out->len-1] != '\n') + strbuf_addch(out, '\n'); return 0; } @@ -341,12 +346,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) 0); if (argc > 0) usage_with_options(fmt_merge_msg_usage, options); - if (message && !shortlog_len) { - char nl = '\n'; - write_in_full(STDOUT_FILENO, message, strlen(message)); - write_in_full(STDOUT_FILENO, &nl, 1); - return 0; - } + if (shortlog_len < 0) die("Negative --log=%d", shortlog_len); -- cgit v1.2.1 From cbda121c993bf14d3743c7fdf0f8c476c9f7c636 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 4 Nov 2011 17:35:42 -0700 Subject: fmt-merge-msg: package options into a structure This way new features can be added more easily Signed-off-by: Junio C Hamano --- builtin/fmt-merge-msg.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'builtin/fmt-merge-msg.c') diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c index 7b492f917..3ff9564e7 100644 --- a/builtin/fmt-merge-msg.c +++ b/builtin/fmt-merge-msg.c @@ -209,7 +209,7 @@ static void shortlog(const char *name, unsigned char *sha1, string_list_clear(&subjects, 0); } -static void do_fmt_merge_msg_title(struct strbuf *out, +static void fmt_merge_msg_title(struct strbuf *out, const char *current_branch) { int i = 0; char *sep = ""; @@ -262,8 +262,9 @@ static void do_fmt_merge_msg_title(struct strbuf *out, strbuf_addf(out, " into %s\n", current_branch); } -static int do_fmt_merge_msg(int merge_title, struct strbuf *in, - struct strbuf *out, int shortlog_len) { +int fmt_merge_msg(struct strbuf *in, struct strbuf *out, + struct fmt_merge_msg_opts *opts) +{ int i = 0, pos = 0; unsigned char head_sha1[20]; const char *current_branch; @@ -289,10 +290,10 @@ static int do_fmt_merge_msg(int merge_title, struct strbuf *in, die ("Error in line %d: %.*s", i, len, p); } - if (merge_title && srcs.nr) - do_fmt_merge_msg_title(out, current_branch); + if (opts->add_title && srcs.nr) + fmt_merge_msg_title(out, current_branch); - if (shortlog_len) { + if (opts->shortlog_len) { struct commit *head; struct rev_info rev; @@ -307,18 +308,13 @@ static int do_fmt_merge_msg(int merge_title, struct strbuf *in, for (i = 0; i < origins.nr; i++) shortlog(origins.items[i].string, origins.items[i].util, - head, &rev, shortlog_len, out); + head, &rev, opts->shortlog_len, out); } if (out->len && out->buf[out->len-1] != '\n') strbuf_addch(out, '\n'); return 0; } -int fmt_merge_msg(struct strbuf *in, struct strbuf *out, - int merge_title, int shortlog_len) { - return do_fmt_merge_msg(merge_title, in, out, shortlog_len); -} - int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) { const char *inpath = NULL; @@ -340,6 +336,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) FILE *in = stdin; struct strbuf input = STRBUF_INIT, output = STRBUF_INIT; int ret; + struct fmt_merge_msg_opts opts; git_config(fmt_merge_msg_config, NULL); argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage, @@ -361,10 +358,12 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) if (message) strbuf_addstr(&output, message); - ret = fmt_merge_msg(&input, &output, - message ? 0 : 1, - shortlog_len); + memset(&opts, 0, sizeof(opts)); + opts.add_title = !message; + opts.shortlog_len = shortlog_len; + + ret = fmt_merge_msg(&input, &output, &opts); if (ret) return ret; write_in_full(STDOUT_FILENO, output.buf, output.len); -- cgit v1.2.1 From 895680f044ebe9df600e6c9e042d40d9d953bc9b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 4 Nov 2011 21:06:30 -0700 Subject: fmt-merge-msg: Add contents of merged tag in the merge message When a contributor asks the integrator to merge her history, a signed tag can be a good vehicle to communicate the authenticity of the request while conveying other information such as the purpose of the topic. E.g. a signed tag "for-linus" can be created, and the integrator can run: $ git pull git://example.com/work.git/ for-linus This would allow the integrator to run "git verify-tag FETCH_HEAD" to validate the signed tag. Update fmt-merge-msg so that it pre-fills the merge message template with the body (but not signature) of the tag object to help the integrator write a better merge message, in the same spirit as the existing merge.log summary lines. The message that comes from GPG signature validation is also included in the merge message template to help the integrator verify it, but they are prefixed with "#" to make them comments. Signed-off-by: Junio C Hamano --- builtin/fmt-merge-msg.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) (limited to 'builtin/fmt-merge-msg.c') diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c index 3ff9564e7..7dae846c5 100644 --- a/builtin/fmt-merge-msg.c +++ b/builtin/fmt-merge-msg.c @@ -5,6 +5,7 @@ #include "revision.h" #include "tag.h" #include "string-list.h" +#include "gpg-interface.h" static const char * const fmt_merge_msg_usage[] = { "git fmt-merge-msg [-m ] [--log[=]|--no-log] [--file ]", @@ -262,6 +263,70 @@ static void fmt_merge_msg_title(struct strbuf *out, strbuf_addf(out, " into %s\n", current_branch); } +static void fmt_tag_signature(struct strbuf *tagbuf, + struct strbuf *sig, + const char *buf, + unsigned long len) +{ + const char *tag_body = strstr(buf, "\n\n"); + if (tag_body) { + tag_body += 2; + strbuf_add(tagbuf, tag_body, buf + len - tag_body); + } + strbuf_complete_line(tagbuf); + strbuf_add_lines(tagbuf, "# ", sig->buf, sig->len); +} + +static void fmt_merge_msg_sigs(struct strbuf *out) +{ + int i, tag_number = 0, first_tag = 0; + struct strbuf tagbuf = STRBUF_INIT; + + for (i = 0; i < origins.nr; i++) { + unsigned char *sha1 = origins.items[i].util; + enum object_type type; + unsigned long size, len; + char *buf = read_sha1_file(sha1, &type, &size); + struct strbuf sig = STRBUF_INIT; + + if (!buf || type != OBJ_TAG) + goto next; + len = parse_signature(buf, size); + + if (size == len) + ; /* merely annotated */ + else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig)) { + if (!sig.len) + strbuf_addstr(&sig, "gpg verification failed.\n"); + } + + if (!tag_number++) { + fmt_tag_signature(&tagbuf, &sig, buf, len); + first_tag = i; + } else { + if (tag_number == 2) { + struct strbuf tagline = STRBUF_INIT; + strbuf_addf(&tagline, "\n# %s\n", + origins.items[first_tag].string); + strbuf_insert(&tagbuf, 0, tagline.buf, + tagline.len); + strbuf_release(&tagline); + } + strbuf_addf(&tagbuf, "\n# %s\n", + origins.items[i].string); + fmt_tag_signature(&tagbuf, &sig, buf, len); + } + strbuf_release(&sig); + next: + free(buf); + } + if (tagbuf.len) { + strbuf_addch(out, '\n'); + strbuf_addbuf(out, &tagbuf); + } + strbuf_release(&tagbuf); +} + int fmt_merge_msg(struct strbuf *in, struct strbuf *out, struct fmt_merge_msg_opts *opts) { @@ -293,6 +358,9 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out, if (opts->add_title && srcs.nr) fmt_merge_msg_title(out, current_branch); + if (origins.nr) + fmt_merge_msg_sigs(out); + if (opts->shortlog_len) { struct commit *head; struct rev_info rev; @@ -310,8 +378,8 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out, shortlog(origins.items[i].string, origins.items[i].util, head, &rev, opts->shortlog_len, out); } - if (out->len && out->buf[out->len-1] != '\n') - strbuf_addch(out, '\n'); + + strbuf_complete_line(out); return 0; } -- cgit v1.2.1