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

[PATCH 9/9] merge: use new "reset" function instead of running "git read-tree"

From
Christian Couder <chriscool@tuxfamily.org>
Date
Feb 5, 2010, 23:11 UTC
Message-ID
<20100205231112.3689.34673.chriscool@tuxfamily.org>
In-Reply-To
<20100205231028.3689.12228.chriscool@tuxfamily.org>

This simplifies "git merge" code and make it more efficient in some cases.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin-merge.c |   31 +++++++++++--------------------
 1 files changed, 11 insertions(+), 20 deletions(-)
diff --git a/builtin-merge.c b/builtin-merge.c
index 3aaec7b..de2343a 100644
--- a/builtin-merge.c
+++ b/builtin-merge.c
@@ -25,6 +25,7 @@
 #include "help.h"
 #include "merge-recursive.h"
 #include "resolve-undo.h"
+#include "reset.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -231,24 +232,14 @@ static void save_state(void)
 		die("not a valid object: %s", buffer.buf);
 }
 
-static void reset_hard(unsigned const char *sha1, int verbose)
+static void reset_hard(const char *prefix, unsigned const char *sha1, int verbose)
 {
-	int i = 0;
-	const char *args[6];
-
-	args[i++] = "read-tree";
-	if (verbose)
-		args[i++] = "-v";
-	args[i++] = "--reset";
-	args[i++] = "-u";
-	args[i++] = sha1_to_hex(sha1);
-	args[i] = NULL;
-
-	if (run_command_v_opt(args, RUN_GIT_CMD))
-		die("read-tree failed");
+	int res = reset(sha1_to_hex(sha1), prefix, HARD, !verbose, 0, 0, NULL);
+	if (res)
+		die("hard reset failed");
 }
 
-static void restore_state(void)
+static void restore_state(const char *prefix)
 {
 	struct strbuf sb = STRBUF_INIT;
 	const char *args[] = { "stash", "apply", NULL, NULL };
@@ -256,7 +247,7 @@ static void restore_state(void)
 	if (is_null_sha1(stash))
 		return;
 
-	reset_hard(head, 1);
+	reset_hard(prefix, head, 1);
 
 	args[2] = sha1_to_hex(stash);
 
@@ -970,7 +961,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 			die("%s - not something we can merge", argv[0]);
 		update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
 				DIE_ON_ERR);
-		reset_hard(remote_head->sha1, 0);
+		reset_hard(prefix, remote_head->sha1, 0);
 		return 0;
 	} else {
 		struct strbuf msg = STRBUF_INIT;
@@ -1167,7 +1158,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		int ret;
 		if (i) {
 			printf("Rewinding the tree to pristine...\n");
-			restore_state();
+			restore_state(prefix);
 		}
 		if (use_strategies_nr != 1)
 			printf("Trying merge strategy %s...\n",
@@ -1228,7 +1219,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	 * it up.
 	 */
 	if (!best_strategy) {
-		restore_state();
+		restore_state(prefix);
 		if (use_strategies_nr > 1)
 			fprintf(stderr,
 				"No merge strategy handled the merge.\n");
@@ -1240,7 +1231,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 		; /* We already have its result in the working tree. */
 	else {
 		printf("Rewinding the tree to pristine...\n");
-		restore_state();
+		restore_state(prefix);
 		printf("Using the %s to prepare resolving by hand.\n",
 			best_strategy);
 		try_merge_strategy(best_strategy, common, head_arg);
-- 
1.6.6.1.557.g77031
Previous: Christian CouderNext: Junio C Hamano
Message 16 of 20 in “add --ff option to cherry-pick”
  1. 0/9 add --ff option to cherry-pickChristian Couder, Feb 5, 2010
  2. 1/9 revert: libify cherry-pick and revert functionnalityChristian Couder, Feb 5, 2010
  3. 2/9 reset: refactor updating heads into a static functionChristian Couder, Feb 5, 2010
  4. 3/9 reset: refactor reseting in its own functionChristian Couder, Feb 5, 2010
  5. 4/9 reset: make reset function non static and declare it in "reset.h"Christian Couder, Feb 5, 2010
  6. 5/9 revert: add --ff option to allow fast forward when cherry-pickingChristian Couder, Feb 5, 2010
  7. Junio C HamanoFeb 5, 2010
  8. Junio C HamanoFeb 6, 2010
  9. Christian CouderFeb 28, 2010
  10. Paolo BonziniFeb 6, 2010
  11. Christian CouderFeb 6, 2010
  12. Paolo BonziniFeb 6, 2010
  13. 6/9 cherry-pick: add tests for new --ff optionChristian Couder, Feb 5, 2010
  14. 7/9 Documentation: describe new cherry-pick --ff optionChristian Couder, Feb 5, 2010
  15. 8/9 rebase -i: use new --ff cherry-pick optionChristian Couder, Feb 5, 2010
  16. 9/9 merge: use new "reset" function instead of running "git read-tree"Christian Couder, Feb 5, 2010
  17. Junio C HamanoFeb 6, 2010
  18. Christian CouderFeb 6, 2010
  19. Junio C HamanoFeb 5, 2010
  20. Christian CouderFeb 6, 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.