[PATCH v3 13/17] commit: teach --amend to carry forward extra headers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 builtin/commit.c |    9 ++++++-
 commit.c         |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 commit.h         |    3 ++
 3 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 4688a73..fca7ea0 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1382,6 +1382,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	int allow_fast_forward = 1;
 	struct wt_status s;
 	struct commit *current_head = NULL;
+	struct commit_extra_header *extra = NULL;
 
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1483,12 +1484,16 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		exit(1);
 	}
 
-	if (commit_tree(sb.buf, active_cache_tree->sha1, parents, sha1,
-			author_ident.buf)) {
+	if (amend)
+		extra = read_commit_extra_headers(current_head);
+
+	if (commit_tree_extended(sb.buf, active_cache_tree->sha1, parents, sha1,
+				 author_ident.buf, extra)) {
 		rollback_index_files();
 		die(_("failed to write commit object"));
 	}
 	strbuf_release(&author_ident);
+	free_commit_extra_headers(extra);
 
 	ref_lock = lock_any_ref_for_update("HEAD",
 					   !current_head
diff --git a/commit.c b/commit.c
index ee12eff..1c05879 100644
--- a/commit.c
+++ b/commit.c
@@ -897,6 +897,66 @@ static void add_extra_header(struct strbuf *buffer,
 	strbuf_add_lines(buffer, " ", extra->value, extra->len);
 }
 
+struct commit_extra_header *read_commit_extra_headers(struct commit *commit)
+{
+	struct commit_extra_header *extra = NULL;
+	size_t size;
+	enum object_type type;
+	char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
+	if (buffer && type == OBJ_COMMIT)
+		extra = read_commit_extra_header_lines(buffer, size);
+	free(buffer);
+	return extra;
+}
+
+static inline int standard_header_field(const char *field, size_t len)
+{
+	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
+		(len == 6 && !memcmp(field, "parent ", 7)) ||
+		(len == 6 && !memcmp(field, "author ", 7)) ||
+		(len == 9 && !memcmp(field, "committer ", 10)) ||
+		(len == 8 && !memcmp(field, "encoding ", 9)));
+}
+
+struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size)
+{
+	struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
+	const char *line, *next, *eof, *eob;
+	struct strbuf buf = STRBUF_INIT;
+
+	for (line = buffer, eob = line + size;
+	     line < eob && *line != '\n';
+	     line = next) {
+		next = memchr(line, '\n', eob - line);
+		next = next ? next + 1 : eob;
+		if (*line == ' ') {
+			/* continuation */
+			strbuf_add(&buf, line + 1, next - (line + 1));
+			continue;
+		}
+		if (it)
+			it->value = strbuf_detach(&buf, &it->len);
+		strbuf_reset(&buf);
+		it = NULL;
+
+		eof = strchr(line, ' ');
+		if (next <= eof)
+			continue; /* malformed */
+
+		if (standard_header_field(line, eof - line))
+			continue;
+
+		it = xcalloc(1, sizeof(*it));
+		it->key = xmemdupz(line, eof-line);
+		*tail = it;
+		tail = &it->next;
+		strbuf_add(&buf, eof + 1, next - (eof + 1));
+	}
+	if (it)
+		it->value = strbuf_detach(&buf, &it->len);
+	return extra;
+}
+
 void free_commit_extra_headers(struct commit_extra_header *extra)
 {
 	while (extra) {
diff --git a/commit.h b/commit.h
index c1a723e..3745f12 100644
--- a/commit.h
+++ b/commit.h
@@ -200,6 +200,9 @@ extern int commit_tree_extended(const char *msg, unsigned char *tree,
 				const char *author,
 				struct commit_extra_header *);
 
+extern struct commit_extra_header *read_commit_extra_headers(struct commit *);
+extern struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len);
+
 extern void free_commit_extra_headers(struct commit_extra_header *extra);
 
 struct merge_remote_desc {
-- 
1.7.8.rc1.82.g90e080

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]