commit 5943eee28016d0b1f6e87c25366b3a85a95a727a from: Stefan Sperling date: Tue Aug 13 14:58:34 2019 UTC strip Git GPG signatures from log messages commit - 5d844a1e3768d8c536169a754802b4bf8ebdd64c commit + 5943eee28016d0b1f6e87c25366b3a85a95a727a blob - 263bc2cb88607d7fc3ebf4d3c07422a39951de68 blob + 8c1f0a2fe706b6338dc31930faca0cb9c0d7bb2e --- got/got.c +++ got/got.c @@ -1458,9 +1458,9 @@ print_commit(struct got_commit_object *commit, struct } } - logmsg0 = strdup(got_object_commit_get_logmsg(commit)); - if (logmsg0 == NULL) - return got_error_from_errno("strdup"); + err = got_object_commit_get_logmsg(&logmsg0, commit); + if (err) + return err; logmsg = logmsg0; do { @@ -3932,19 +3932,28 @@ trim_logmsg(char *logmsg, int limit) static const struct got_error * get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit) { - const char *logmsg0 = NULL; + const struct got_error *err; + char *logmsg0 = NULL; + const char *s; - logmsg0 = got_object_commit_get_logmsg(commit); - - while (isspace((unsigned char)logmsg0[0])) - logmsg0++; + err = got_object_commit_get_logmsg(&logmsg0, commit); + if (err) + return err; - *logmsg = strdup(logmsg0); - if (*logmsg == NULL) - return got_error_from_errno("strdup"); + s = logmsg0; + while (isspace((unsigned char)s[0])) + s++; + + *logmsg = strdup(s); + if (*logmsg == NULL) { + err = got_error_from_errno("strdup"); + goto done; + } trim_logmsg(*logmsg, limit); - return NULL; +done: + free(logmsg0); + return err; } static const struct got_error * @@ -4542,7 +4551,7 @@ append_folded_commit_msg(char **new_msg, struct got_hi { const struct got_error *err; struct got_commit_object *folded_commit = NULL; - char *id_str; + char *id_str, *folded_logmsg = NULL; err = got_object_id_str(&id_str, hle->commit_id); if (err) @@ -4552,9 +4561,12 @@ append_folded_commit_msg(char **new_msg, struct got_hi if (err) goto done; + err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit); + if (err) + goto done; if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s", logmsg ? logmsg : "", logmsg ? "\n" : "", id_str, - got_object_commit_get_logmsg(folded_commit)) == -1) { + folded_logmsg) == -1) { err = got_error_from_errno("asprintf"); goto done; } @@ -4562,6 +4574,7 @@ done: if (folded_commit) got_object_commit_close(folded_commit); free(id_str); + free(folded_logmsg); return err; } @@ -4585,7 +4598,7 @@ static const struct got_error * histedit_edit_logmsg(struct got_histedit_list_entry *hle, struct got_repository *repo) { - char *logmsg_path = NULL, *id_str = NULL; + char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL; char *logmsg = NULL, *new_msg = NULL, *editor = NULL; const struct got_error *err = NULL; struct got_commit_object *commit = NULL; @@ -4616,10 +4629,12 @@ histedit_edit_logmsg(struct got_histedit_list_entry *h err = got_object_id_str(&id_str, hle->commit_id); if (err) goto done; + err = got_object_commit_get_logmsg(&orig_logmsg, commit); + if (err) + goto done; if (asprintf(&new_msg, "%s\n# original log message of commit %s: %s", - logmsg ? logmsg : "", id_str, - got_object_commit_get_logmsg(commit)) == -1) { + logmsg ? logmsg : "", id_str, orig_logmsg) == -1) { err = got_error_from_errno("asprintf"); goto done; } @@ -4645,16 +4660,14 @@ histedit_edit_logmsg(struct got_histedit_list_entry *h if (err) { if (err->code != GOT_ERR_COMMIT_MSG_EMPTY) goto done; - err = NULL; - hle->logmsg = strdup(got_object_commit_get_logmsg(commit)); - if (hle->logmsg == NULL) - err = got_error_from_errno("strdup"); + err = got_object_commit_get_logmsg(&hle->logmsg, commit); } done: if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL) err = got_error_from_errno2("unlink", logmsg_path); free(logmsg_path); free(logmsg); + free(orig_logmsg); free(editor); if (commit) got_object_commit_close(commit); blob - 505fd64b4138ac8c0f740dfc79384bdfb668fb49 blob + 2b3fe0ff8ffb25f413012f16cb24d07a309371a1 --- include/got_object.h +++ include/got_object.h @@ -138,8 +138,9 @@ time_t got_object_commit_get_committer_time(struct got /* Get a committer's timezone offset. */ time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *); -/* Get the commit log message. */ -const char *got_object_commit_get_logmsg(struct got_commit_object *); +/* Get the commit log message. The caller must dispose of it with free(3). */ +const struct got_error *got_object_commit_get_logmsg(char **, + struct got_commit_object *); /* * Attempt to open a tree object in a repository. blob - 10b843802494c5f2896fe09dc9ac6e988484f623 blob + 3a4758573cc31e0fe79549eef2a7b05ccce29621 --- lib/object_parse.c +++ lib/object_parse.c @@ -422,10 +422,65 @@ got_object_commit_get_committer_gmtoff(struct got_comm return commit->committer_gmtoff; } -const char * -got_object_commit_get_logmsg(struct got_commit_object *commit) +#define GOT_GPG_BEGIN_STR "gpgsig -----BEGIN PGP SIGNATURE-----" +#define GOT_GPG_END_STR " -----END PGP SIGNATURE-----" + +const struct got_error * +got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit) { - return commit->logmsg; + const struct got_error *err = NULL; + int gpgsig = 0; + char *msg0, *msg, *line, *s; + size_t len; + + *logmsg = NULL; + + msg0 = strdup(commit->logmsg); + if (msg0 == NULL) + return got_error_from_errno("strdup"); + + /* Copy log message line by line to strip out GPG sigs... */ + msg = msg0; + do { + line = strsep(&msg, "\n"); + + if (line) { + /* Skip over GPG signatures. */ + if (gpgsig) { + if (strcmp(line, GOT_GPG_END_STR) == 0) { + gpgsig = 0; + /* Skip empty line after sig. */ + line = strsep(&msg, "\n"); + } + continue; + } else if (strcmp(line, GOT_GPG_BEGIN_STR) == 0) { + gpgsig = 1; + continue; + } + if (asprintf(&s, "%s%s\n", + *logmsg ? *logmsg : "", line) == -1) { + err = got_error_from_errno("asprintf"); + goto done; + } + free(*logmsg); + *logmsg = s; + } + } while (line); + + /* Trim redundant trailing whitespace. */ + len = strlen(*logmsg); + while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) && + isspace((unsigned char)(*logmsg)[len - 1])) { + (*logmsg)[len - 1] = '\0'; + len--; + } +done: + free(msg0); + if (err) { + free(*logmsg); + *logmsg = NULL; + } + return err; } const struct got_error * blob - 78f6bf5af2fe8da4c28784be487f4d554d7b308e blob + 1d163b44fd009159ceb946a0b633b9078cb6a7ef --- lib/worktree.c +++ lib/worktree.c @@ -4810,13 +4810,19 @@ rebase_commit(struct got_object_id **new_commit_id, if (err) goto done; - if (new_logmsg) + if (new_logmsg) { logmsg = strdup(new_logmsg); - else - logmsg = strdup(got_object_commit_get_logmsg(orig_commit)); - if (logmsg == NULL) - return got_error_from_errno("strdup"); + if (logmsg == NULL) { + err = got_error_from_errno("strdup"); + goto done; + } + } else { + err = got_object_commit_get_logmsg(&logmsg, orig_commit); + if (err) + goto done; + } + /* NB: commit_worktree will call free(logmsg) */ err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id, worktree, got_object_commit_get_author(orig_commit), got_object_commit_get_committer(orig_commit), blob - 330cb7270dac76c013148197dd78c6c658c5b281 blob + 0119a32cf90c03a3afc72f5b5dfdbe5b3ec1e7d4 --- tog/tog.c +++ tog/tog.c @@ -1092,11 +1092,9 @@ draw_commit(struct tog_view *view, struct got_commit_o if (col > avail) goto done; - logmsg0 = strdup(got_object_commit_get_logmsg(commit)); - if (logmsg0 == NULL) { - err = got_error_from_errno("strdup"); + err = got_object_commit_get_logmsg(&logmsg0, commit); + if (err) goto done; - } logmsg = logmsg0; while (*logmsg == '\n') logmsg++; @@ -1752,7 +1750,7 @@ search_start_log_view(struct tog_view *view) static int match_commit(struct got_commit_object *commit, const char *id_str, - regex_t *regex) + const char *logmsg, regex_t *regex) { regmatch_t regmatch; @@ -1760,9 +1758,8 @@ match_commit(struct got_commit_object *commit, const c ®match, 0) == 0 || regexec(regex, got_object_commit_get_committer(commit), 1, ®match, 0) == 0 || - regexec(regex, got_object_commit_get_logmsg(commit), 1, - ®match, 0) == 0 || - regexec(regex, id_str, 1, ®match, 0) == 0) + regexec(regex, id_str, 1, ®match, 0) == 0 || + regexec(regex, logmsg, 1, ®match, 0) == 0) return 1; return 0; @@ -1804,7 +1801,7 @@ search_next_log_view(struct tog_view *view) } while (1) { - char *id_str; + char *id_str, *logmsg; if (entry == NULL) { if (s->thread_args.log_complete || view->searching == TOG_SEARCH_BACKWARD) { @@ -1827,12 +1824,17 @@ search_next_log_view(struct tog_view *view) if (err) return err; - if (match_commit(entry->commit, id_str, &view->regex)) { + err = got_object_commit_get_logmsg(&logmsg, entry->commit); + if (err) + return err; + if (match_commit(entry->commit, id_str, logmsg, &view->regex)) { + free(logmsg); view->search_next_done = 1; s->matched_entry = entry; free(id_str); break; } + free(logmsg); free(id_str); s->search_entry = entry; if (view->searching == TOG_SEARCH_FORWARD) @@ -2444,7 +2446,7 @@ write_commit_info(struct got_object_id *commit_id, const struct got_error *err = NULL; char datebuf[26]; struct got_commit_object *commit; - char *id_str = NULL; + char *id_str = NULL, *logmsg = NULL; time_t committer_time; const char *author, *committer; char *refs_str = NULL; @@ -2488,13 +2490,16 @@ write_commit_info(struct got_object_id *commit_id, err = got_error_from_errno("fprintf"); goto done; } - if (fprintf(outfile, "%s\n", - got_object_commit_get_logmsg(commit)) < 0) { + err = got_object_commit_get_logmsg(&logmsg, commit); + if (err) + goto done; + if (fprintf(outfile, "%s\n", logmsg) < 0) { err = got_error_from_errno("fprintf"); goto done; } done: free(id_str); + free(logmsg); free(refs_str); got_object_commit_close(commit); return err;