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 <gitster@pobox.com>

Junio C Hamano committed Nov 8, 2011 at 15:38 UTC ed7a42a0752fd71cf04273b8e691966dd2c33ebe
3 files changed +76 -3
builtin/commit.c
+7 -2
@@ -1382,6 +1382,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1382 int allow_fast_forward = 1;
1383 struct wt_status s;
1384 struct commit *current_head = NULL;
1385 + struct commit_extra_header *extra = NULL;
1386
1387 if (argc == 2 && !strcmp(argv[1], "-h"))
1388 usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1483,12 +1484,16 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1484 exit(1);
1485 }
1486
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)) {
1492 rollback_index_files();
1493 die(_("failed to write commit object"));
1494 }
1495 strbuf_release(&author_ident);
1496 + free_commit_extra_headers(extra);
1497
1498 ref_lock = lock_any_ref_for_update("HEAD",
1499 !current_head
commit.c
+66 -1
@@ -894,7 +894,72 @@ static void add_extra_header(struct strbuf *buffer,
894 struct commit_extra_header *extra)
895 {
896 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;
963 }
964
965 void free_commit_extra_headers(struct commit_extra_header *extra)
commit.h
+3
@@ -200,6 +200,9 @@ extern int commit_tree_extended(const char *msg, unsigned char *tree,
200 const char *author,
201 struct commit_extra_header *);
202
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 +
206 extern void free_commit_extra_headers(struct commit_extra_header *extra);
207
208 struct merge_remote_desc {