Skip to content

Commit ed7a42a

Browse files
committed
commit: teach --amend to carry forward extra headers
After running "git pull $there for-linus" to merge a signed tag, the integrator may need to amend the resulting merge commit to fix typoes in it. Teach --amend option to read the existing extra headers, and carry them forward. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fab47d0 commit ed7a42a

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

builtin/commit.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
13821382
int allow_fast_forward = 1;
13831383
struct wt_status s;
13841384
struct commit *current_head = NULL;
1385+
struct commit_extra_header *extra = NULL;
13851386

13861387
if (argc == 2 && !strcmp(argv[1], "-h"))
13871388
usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1483,12 +1484,16 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14831484
exit(1);
14841485
}
14851486

1486-
if (commit_tree(sb.buf, active_cache_tree->sha1, parents, sha1,
1487-
author_ident.buf)) {
1487+
if (amend)
1488+
extra = read_commit_extra_headers(current_head);
1489+
1490+
if (commit_tree_extended(sb.buf, active_cache_tree->sha1, parents, sha1,
1491+
author_ident.buf, extra)) {
14881492
rollback_index_files();
14891493
die(_("failed to write commit object"));
14901494
}
14911495
strbuf_release(&author_ident);
1496+
free_commit_extra_headers(extra);
14921497

14931498
ref_lock = lock_any_ref_for_update("HEAD",
14941499
!current_head

commit.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,72 @@ static void add_extra_header(struct strbuf *buffer,
894894
struct commit_extra_header *extra)
895895
{
896896
strbuf_addstr(buffer, extra->key);
897-
strbuf_add_lines(buffer, " ", extra->value, extra->len);
897+
if (extra->len)
898+
strbuf_add_lines(buffer, " ", extra->value, extra->len);
899+
else
900+
strbuf_addch(buffer, '\n');
901+
}
902+
903+
struct commit_extra_header *read_commit_extra_headers(struct commit *commit)
904+
{
905+
struct commit_extra_header *extra = NULL;
906+
unsigned long size;
907+
enum object_type type;
908+
char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
909+
if (buffer && type == OBJ_COMMIT)
910+
extra = read_commit_extra_header_lines(buffer, size);
911+
free(buffer);
912+
return extra;
913+
}
914+
915+
static inline int standard_header_field(const char *field, size_t len)
916+
{
917+
return ((len == 4 && !memcmp(field, "tree ", 5)) ||
918+
(len == 6 && !memcmp(field, "parent ", 7)) ||
919+
(len == 6 && !memcmp(field, "author ", 7)) ||
920+
(len == 9 && !memcmp(field, "committer ", 10)) ||
921+
(len == 8 && !memcmp(field, "encoding ", 9)));
922+
}
923+
924+
struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size)
925+
{
926+
struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
927+
const char *line, *next, *eof, *eob;
928+
struct strbuf buf = STRBUF_INIT;
929+
930+
for (line = buffer, eob = line + size;
931+
line < eob && *line != '\n';
932+
line = next) {
933+
next = memchr(line, '\n', eob - line);
934+
next = next ? next + 1 : eob;
935+
if (*line == ' ') {
936+
/* continuation */
937+
if (it)
938+
strbuf_add(&buf, line + 1, next - (line + 1));
939+
continue;
940+
}
941+
if (it)
942+
it->value = strbuf_detach(&buf, &it->len);
943+
strbuf_reset(&buf);
944+
it = NULL;
945+
946+
eof = strchr(line, ' ');
947+
if (next <= eof)
948+
eof = next;
949+
950+
if (standard_header_field(line, eof - line))
951+
continue;
952+
953+
it = xcalloc(1, sizeof(*it));
954+
it->key = xmemdupz(line, eof-line);
955+
*tail = it;
956+
tail = &it->next;
957+
if (eof + 1 < next)
958+
strbuf_add(&buf, eof + 1, next - (eof + 1));
959+
}
960+
if (it)
961+
it->value = strbuf_detach(&buf, &it->len);
962+
return extra;
898963
}
899964

900965
void free_commit_extra_headers(struct commit_extra_header *extra)

commit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ extern int commit_tree_extended(const char *msg, unsigned char *tree,
200200
const char *author,
201201
struct commit_extra_header *);
202202

203+
extern struct commit_extra_header *read_commit_extra_headers(struct commit *);
204+
extern struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len);
205+
203206
extern void free_commit_extra_headers(struct commit_extra_header *extra);
204207

205208
struct merge_remote_desc {

0 commit comments

Comments
 (0)