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

[PATCH 5/6] builtin-commit.c: further refactor branch switching

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 6, 2008, 01:54 UTC
Message-ID
<1228528455-3089-6-git-send-email-gitster@pobox.com>
In-Reply-To
<1228528455-3089-5-git-send-email-gitster@pobox.com>

This adds reset_index_and_worktree() to further factor out the logic for switching the index and the work tree state from one commit to another to shrink merge_working_tree() function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-checkout.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 9c45c49..a08941a 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -433,6 +433,18 @@ static int switch_trees(int merge, int quiet, int *wt_error,
 	return ret;
 }
 
+int reset_index_and_worktree(int force, int merge, int quiet, int *wt_error,
+			     struct commit *old_commit, const char *old_label,
+			     struct commit *new_commit, const char *new_label)
+{
+	if (force)
+		return reset_tree(new_commit->tree, quiet, 1, wt_error);
+	else
+		return switch_trees(merge, quiet, wt_error,
+				    old_commit, old_label,
+				    new_commit, new_label);
+}
+
 struct branch_info {
 	const char *name; /* The short name used */
 	const char *path; /* The full name of a real branch */
@@ -457,14 +469,10 @@ static int merge_working_tree(struct checkout_opts *opts,
 	if (read_cache() < 0)
 		return error("corrupt index file");
 
-	if (opts->force)
-		ret = reset_tree(new->commit->tree, opts->quiet, 1,
-				 &opts->writeout_error);
-	else
-		ret = switch_trees(opts->merge, opts->quiet,
-				   &opts->writeout_error,
-				   old->commit, "local",
-				   new->commit, new->name);
+	ret = reset_index_and_worktree(opts->force, opts->merge, opts->quiet,
+				       &opts->writeout_error,
+				       old->commit, "local",
+				       new->commit, new->name);
 	if (ret)
 		return ret;
 
-- 
1.6.1.rc1.72.ga5ce6
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 8 in “Reusing "checkout [-m]" logic for "reset --merge"”
  1. 0/6 Reusing "checkout [-m]" logic for "reset --merge"Junio C Hamano, Dec 6, 2008
  2. 1/6 builtin-checkout.c: check error return from read_cache()Junio C Hamano, Dec 6, 2008
  3. 2/6 read-cache.c: typofix in commentJunio C Hamano, Dec 6, 2008
  4. 3/6 Make reset_tree() in builtin-checkout.c a bit more library-ishJunio C Hamano, Dec 6, 2008
  5. 4/6 builtin-checkout.c: refactor merge_working_tree()Junio C Hamano, Dec 6, 2008
  6. 5/6 builtin-commit.c: further refactor branch switchingJunio C Hamano, Dec 6, 2008
  7. 6/6 builtin-reset.c: use reset_index_and_worktree()Junio C Hamano, Dec 6, 2008
  8. Linus TorvaldsDec 6, 2008

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.