git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH 2/5] revert: refactor merge recursive code into its own function

From
Christian Couder <chriscool@tuxfamily.org>
Date
Mar 25, 2010, 04:58 UTC
Message-ID
<20100325045818.14832.94770.chriscool@tuxfamily.org>
In-Reply-To
<20100325045519.14832.65964.chriscool@tuxfamily.org>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/revert.c |  101 +++++++++++++++++++++++++++++------------------------
 1 files changed, 55 insertions(+), 46 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index 3cd62ae..054f442 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -271,17 +271,67 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from)
 	return write_ref_sha1(ref_lock, to, "cherry-pick");
 }
 
+static void do_recursive_merge(struct commit *base, struct commit *next,
+			       unsigned char *head, char *oneline,
+			       struct strbuf *msgbuf, char *defmsg)
+{
+	struct merge_options o;
+	struct tree *result, *next_tree, *base_tree, *head_tree;
+	int clean, index_fd;
+	static struct lock_file index_lock;
+
+	index_fd = hold_locked_index(&index_lock, 1);
+
+	read_cache();
+	init_merge_options(&o);
+	o.branch1 = "HEAD";
+	o.branch2 = oneline;
+
+	head_tree = parse_tree_indirect(head);
+	next_tree = next ? next->tree : empty_tree();
+	base_tree = base ? base->tree : empty_tree();
+
+	clean = merge_trees(&o,
+			    head_tree,
+			    next_tree, base_tree, &result);
+
+	if (active_cache_changed &&
+	    (write_cache(index_fd, active_cache, active_nr) ||
+	     commit_locked_index(&index_lock)))
+		die("%s: Unable to write new index file", me);
+	rollback_lock_file(&index_lock);
+
+	if (!clean) {
+		int i;
+		strbuf_addstr(msgbuf, "\nConflicts:\n\n");
+		for (i = 0; i < active_nr;) {
+			struct cache_entry *ce = active_cache[i++];
+			if (ce_stage(ce)) {
+				strbuf_addch(msgbuf, '\t');
+				strbuf_addstr(msgbuf, ce->name);
+				strbuf_addch(msgbuf, '\n');
+				while (i < active_nr && !strcmp(ce->name,
+						active_cache[i]->name))
+					i++;
+			}
+		}
+		write_message(msgbuf, defmsg);
+		fprintf(stderr, "Automatic %s failed.%s\n",
+			me, help_msg(commit_name));
+		rerere(allow_rerere_auto);
+		exit(1);
+	}
+	write_message(msgbuf, defmsg);
+	fprintf(stderr, "Finished one %s.\n", me);
+}
+
 static int revert_or_cherry_pick(int argc, const char **argv)
 {
 	unsigned char head[20];
 	struct commit *base, *next, *parent;
-	int i, index_fd, clean;
 	char *oneline, *reencoded_message = NULL;
 	const char *message, *encoding;
 	char *defmsg = git_pathdup("MERGE_MSG");
-	struct merge_options o;
-	struct tree *result, *next_tree, *base_tree, *head_tree;
-	static struct lock_file index_lock;
 	struct strbuf msgbuf = STRBUF_INIT;
 
 	git_config(git_default_config, NULL);
@@ -382,8 +432,6 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 
 	oneline = get_oneline(message);
 
-	index_fd = hold_locked_index(&index_lock, 1);
-
 	if (action == REVERT) {
 		char *oneline_body = strchr(oneline, ' ');
 
@@ -411,46 +459,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 		}
 	}
 
-	read_cache();
-	init_merge_options(&o);
-	o.branch1 = "HEAD";
-	o.branch2 = oneline;
-
-	head_tree = parse_tree_indirect(head);
-	next_tree = next ? next->tree : empty_tree();
-	base_tree = base ? base->tree : empty_tree();
-
-	clean = merge_trees(&o,
-			    head_tree,
-			    next_tree, base_tree, &result);
-
-	if (active_cache_changed &&
-	    (write_cache(index_fd, active_cache, active_nr) ||
-	     commit_locked_index(&index_lock)))
-		die("%s: Unable to write new index file", me);
-	rollback_lock_file(&index_lock);
-
-	if (!clean) {
-		strbuf_addstr(&msgbuf, "\nConflicts:\n\n");
-		for (i = 0; i < active_nr;) {
-			struct cache_entry *ce = active_cache[i++];
-			if (ce_stage(ce)) {
-				strbuf_addch(&msgbuf, '\t');
-				strbuf_addstr(&msgbuf, ce->name);
-				strbuf_addch(&msgbuf, '\n');
-				while (i < active_nr && !strcmp(ce->name,
-						active_cache[i]->name))
-					i++;
-			}
-		}
-		write_message(&msgbuf, defmsg);
-		fprintf(stderr, "Automatic %s failed.%s\n",
-			me, help_msg(commit_name));
-		rerere(allow_rerere_auto);
-		exit(1);
-	}
-	write_message(&msgbuf, defmsg);
-	fprintf(stderr, "Finished one %s.\n", me);
+	do_recursive_merge(base, next, head, oneline, &msgbuf, defmsg);
 
 	/*
 	 *
-- 
1.7.0.2.398.g89bc8ce
Previous: Christian CouderNext: Christian Couder
Message 3 of 6 in “add a --strategy option to "git cherry-pick"”
  1. 0/5 add a --strategy option to "git cherry-pick"Christian Couder, Mar 25, 2010
  2. 1/5 revert: use strbuf to refactor the code that writes the merge messageChristian Couder, Mar 25, 2010
  3. 2/5 revert: refactor merge recursive code into its own functionChristian Couder, Mar 25, 2010
  4. 3/5 merge: refactor code that calls "git merge-STRATEGY"Christian Couder, Mar 25, 2010
  5. 4/5 merge: make function try_merge_command non staticChristian Couder, Mar 25, 2010
  6. 5/5 revert: add "--strategy" option to choose merge strategyChristian Couder, Mar 25, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.