git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 6/6] builtin-reset.c: use reset_index_and_worktree()
Date: Fri,  5 Dec 2008 17:54:15 -0800	[thread overview]
Message-ID: <1228528455-3089-7-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1228528455-3089-6-git-send-email-gitster@pobox.com>

This makes "git reset --merge" to use the same underlying mechanism "git
checkout" uses to update the index and the work tree.

It is possible to make it use the 3-way merge fallback "git checkout -m"
allows, but this commit does not go there yet.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-checkout.c |    1 +
 builtin-reset.c    |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 reset.h            |   11 ++++++++
 3 files changed, 76 insertions(+), 2 deletions(-)
 create mode 100644 reset.h

diff --git a/builtin-checkout.c b/builtin-checkout.c
index a08941a..d196521 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -16,6 +16,7 @@
 #include "blob.h"
 #include "xdiff-interface.h"
 #include "ll-merge.h"
+#include "reset.h"
 
 static const char * const checkout_usage[] = {
 	"git checkout [options] <branch>",
diff --git a/builtin-reset.c b/builtin-reset.c
index c0cb915..481a1cc 100644
--- a/builtin-reset.c
+++ b/builtin-reset.c
@@ -18,6 +18,7 @@
 #include "tree.h"
 #include "branch.h"
 #include "parse-options.h"
+#include "reset.h"
 
 static const char * const git_reset_usage[] = {
 	"git reset [--mixed | --soft | --hard | --merge] [-q] [<commit>]",
@@ -52,7 +53,7 @@ static inline int is_merge(void)
 	return !access(git_path("MERGE_HEAD"), F_OK);
 }
 
-static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet)
+static int reset_index_file_via_read_tree(const unsigned char *sha1, int reset_type, int quiet)
 {
 	int i = 0;
 	const char *args[6];
@@ -77,6 +78,67 @@ static int reset_index_file(const unsigned char *sha1, int reset_type, int quiet
 	return run_command_v_opt(args, RUN_GIT_CMD);
 }
 
+static int reset_index_file(struct commit *new, int reset_type, int quiet)
+{
+	/*
+	 * SOFT reset won't even touch index nor work tree so
+	 * this function is not called.
+	 * MIXED updates the index only (should have been called
+	 * --cached), and we let "git read-tree" to do so.
+	 * HARD and MERGE corresponds to "checkout -f" and "checkout [-m]"
+	 */
+	int merge, wt_error, ret;
+	struct commit *old;
+	unsigned char head_sha1[20];
+	unsigned char *new_sha1 = new->object.sha1;
+	struct lock_file *lock_file;
+	int newfd;
+
+	/*
+	 * We play lazy and let read-tree complain if HEAD is not
+	 * readable.  Also on hard reset, HEAD does not have to be
+	 * readable.
+	 */
+	if (reset_type == MIXED ||
+	    reset_type == HARD ||
+	    get_sha1("HEAD", head_sha1) ||
+	    !(old = lookup_commit_reference_gently(head_sha1, 1)))
+		return reset_index_file_via_read_tree(new_sha1, reset_type,
+						      quiet);
+
+	lock_file = xcalloc(1, sizeof(struct lock_file));
+	newfd = hold_locked_index(lock_file, 1);
+	if (read_cache() < 0) {
+		rollback_lock_file(lock_file);
+		return reset_index_file_via_read_tree(new_sha1, reset_type,
+						      quiet);
+	}
+
+	/*
+	 * If we want "checkout -m" behaviour of falling back to
+	 * the 3-way content level merges, we could use
+	 *
+	 *	 merge = (reset_type == MERGE);
+	 *
+	 * here.
+	 */
+	merge = 0;
+
+	wt_error = 0;
+	ret = reset_index_and_worktree(0, merge, quiet, &wt_error,
+				       old, "local",
+				       new, "reset");
+	if (ret || wt_error) {
+		rollback_lock_file(lock_file);
+		return -1;
+	}
+
+	if (write_cache(newfd, active_cache, active_nr) ||
+	    commit_locked_index(lock_file))
+		return error("unable to write new index file");
+	return 0;
+}
+
 static void print_new_head_line(struct commit *commit)
 {
 	const char *hex, *body;
@@ -276,7 +338,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 		if (is_merge() || read_cache() < 0 || unmerged_cache())
 			die("Cannot do a soft reset in the middle of a merge.");
 	}
-	else if (reset_index_file(sha1, reset_type, quiet))
+	else if (reset_index_file(commit, reset_type, quiet))
 		die("Could not reset index file to revision '%s'.", rev);
 
 	/* Any resets update HEAD to the head being switched to,
diff --git a/reset.h b/reset.h
new file mode 100644
index 0000000..9c42838
--- /dev/null
+++ b/reset.h
@@ -0,0 +1,11 @@
+#ifndef RESET_H
+#define RESET_H
+
+extern 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);
+
+#endif
-- 
1.6.1.rc1.72.ga5ce6

  reply	other threads:[~2008-12-06  1:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-06  1:54 [PATCH 0/6] Reusing "checkout [-m]" logic for "reset --merge" Junio C Hamano
2008-12-06  1:54 ` [PATCH 1/6] builtin-checkout.c: check error return from read_cache() Junio C Hamano
2008-12-06  1:54   ` [PATCH 2/6] read-cache.c: typofix in comment Junio C Hamano
2008-12-06  1:54     ` [PATCH 3/6] Make reset_tree() in builtin-checkout.c a bit more library-ish Junio C Hamano
2008-12-06  1:54       ` [PATCH 4/6] builtin-checkout.c: refactor merge_working_tree() Junio C Hamano
2008-12-06  1:54         ` [PATCH 5/6] builtin-commit.c: further refactor branch switching Junio C Hamano
2008-12-06  1:54           ` Junio C Hamano [this message]
2008-12-06  2:08           ` Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1228528455-3089-7-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).