Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 0b05ab6

Browse files
chriscoolgitster
authored andcommitted
replace: remove signature when using --graft
It could be misleading to keep a signature in a replacement commit, so let's remove it. Note that there should probably be a way to sign the replacement commit created when using --graft, but this can be dealt with in another commit or patch series. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0ab2b7 commit 0b05ab6

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

builtin/replace.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ static int create_graft(int argc, const char **argv, int force)
344344

345345
replace_parents(&buf, argc - 1, &argv[1]);
346346

347+
if (remove_signature(&buf)) {
348+
warning(_("the original commit '%s' has a gpg signature."), old_ref);
349+
warning(_("the signature will be removed in the replacement commit!"));
350+
}
351+
347352
if (write_sha1_file(buf.buf, buf.len, commit_type, new))
348353
die(_("could not write replacement commit for: '%s'"), old_ref);
349354

commit.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,40 @@ int parse_signed_commit(const struct commit *commit,
11781178
return saw_signature;
11791179
}
11801180

1181+
int remove_signature(struct strbuf *buf)
1182+
{
1183+
const char *line = buf->buf;
1184+
const char *tail = buf->buf + buf->len;
1185+
int in_signature = 0;
1186+
const char *sig_start = NULL;
1187+
const char *sig_end = NULL;
1188+
1189+
while (line < tail) {
1190+
const char *next = memchr(line, '\n', tail - line);
1191+
next = next ? next + 1 : tail;
1192+
1193+
if (in_signature && line[0] == ' ')
1194+
sig_end = next;
1195+
else if (starts_with(line, gpg_sig_header) &&
1196+
line[gpg_sig_header_len] == ' ') {
1197+
sig_start = line;
1198+
sig_end = next;
1199+
in_signature = 1;
1200+
} else {
1201+
if (*line == '\n')
1202+
/* dump the whole remainder of the buffer */
1203+
next = tail;
1204+
in_signature = 0;
1205+
}
1206+
line = next;
1207+
}
1208+
1209+
if (sig_start)
1210+
strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1211+
1212+
return sig_start != NULL;
1213+
}
1214+
11811215
static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
11821216
{
11831217
struct merge_remote_desc *desc;

commit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ struct commit *get_merge_parent(const char *name);
332332

333333
extern int parse_signed_commit(const struct commit *commit,
334334
struct strbuf *message, struct strbuf *signature);
335+
extern int remove_signature(struct strbuf *buf);
336+
335337
extern void print_commit_list(struct commit_list *list,
336338
const char *format_cur,
337339
const char *format_last);

0 commit comments

Comments
 (0)