# [PATCH 1/3] sequencer: add "reset_almost_hard()" and related functions

3 messages from 2009-08-03 to 2009-08-04. Participants: Christian Couder, Johannes Schindelin.
Thread: https://gitlist.dev/t/20351

## Christian Couder, 2009-08-03 02:40

Subject: [PATCH 1/3] sequencer: add "reset_almost_hard()" and related functions
Message-ID: <20090803024023.3794.90748.chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/20090803024023.3794.90748.chriscool%40tuxfamily.org

```
From: Stephan Beyer <s-beyer@gmx.net>

This patch adds some code that comes as is from the sequencer GSoC
project:

git://repo.or.cz/git/sbeyer.git

(commit e7b8dab0c2a73ade92017a52bb1405ea1534ef20)

It adds some static variables and the following functions:

	- parse_and_init_tree_desc()
	- reset_index_file()
	- reset_almost_hard()
	- set_verbosity()

Mentored-by: Daniel Barkalow <barkalow@iabervon.org>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin-sequencer--helper.c |   94 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/builtin-sequencer--helper.c b/builtin-sequencer--helper.c
index 1dda525..82a830d 100644
--- a/builtin-sequencer--helper.c
+++ b/builtin-sequencer--helper.c
@@ -2,16 +2,95 @@
 #include "cache.h"
 #include "parse-options.h"
 #include "run-command.h"
+#include "refs.h"
+#include "diff.h"
+#include "unpack-trees.h"
 
 #define SEQ_DIR "rebase-merge"
 
 #define PATCH_FILE	git_path(SEQ_DIR "/patch")
 
+static char *reflog;
+
+static int allow_dirty = 0, verbosity = 1, advice = 1;
+
+static unsigned char head_sha1[20];
+
 static const char * const git_sequencer_helper_usage[] = {
 	"git sequencer--helper --make-patch <commit>",
 	NULL
 };
 
+static int parse_and_init_tree_desc(const unsigned char *sha1,
+				    struct tree_desc *desc)
+{
+	struct tree *tree = parse_tree_indirect(sha1);
+	if (!tree)
+		return 1;
+	init_tree_desc(desc, tree->buffer, tree->size);
+	return 0;
+}
+
+static int reset_index_file(const unsigned char *sha1, int update, int dirty)
+{
+	int nr = 1;
+	int newfd;
+	struct tree_desc desc[2];
+	struct unpack_trees_options opts;
+	struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
+
+	memset(&opts, 0, sizeof(opts));
+	opts.head_idx = 1;
+	opts.src_index = &the_index;
+	opts.dst_index = &the_index;
+	opts.reset = 1; /* ignore unmerged entries and overwrite wt files */
+	opts.merge = 1;
+	opts.fn = oneway_merge;
+	if (verbosity > 2)
+		opts.verbose_update = 1;
+	if (update) /* update working tree */
+		opts.update = 1;
+
+	newfd = hold_locked_index(lock, 1);
+
+	read_cache_unmerged();
+
+	if (dirty) {
+		if (get_sha1("HEAD", head_sha1))
+			return error("You do not have a valid HEAD.");
+		if (parse_and_init_tree_desc(head_sha1, desc))
+			return error("Failed to find tree of HEAD.");
+		nr++;
+		opts.fn = twoway_merge;
+	}
+
+	if (parse_and_init_tree_desc(sha1, desc + nr - 1))
+		return error("Failed to find tree of %s.", sha1_to_hex(sha1));
+	if (unpack_trees(nr, desc, &opts))
+		return -1;
+	if (write_cache(newfd, active_cache, active_nr) ||
+	    commit_locked_index(lock))
+		return error("Could not write new index file.");
+
+	return 0;
+}
+
+/*
+ * Realize reset --hard behavior.
+ * If allow_dirty is set and there is a dirty working tree,
+ * then the changes are to be kept.
+ */
+static int reset_almost_hard(const unsigned char *sha)
+{
+	int err = allow_dirty ?
+		(reset_index_file(sha, 1, 1) || reset_index_file(sha, 0, 0)) :
+		reset_index_file(sha, 1, 0);
+	if (err)
+		return error("Could not reset index.");
+
+	return update_ref(reflog, "HEAD", sha, NULL, 0, MSG_ON_ERR);
+}
+
 /* Generate purely informational patch file */
 static void make_patch(struct commit *commit)
 {
@@ -78,6 +157,21 @@ static struct commit *get_commit(const char *arg)
 	return lookup_commit_reference(sha1);
 }
 
+static int set_verbosity(int verbose)
+{
+	char tmp[] = "0";
+	verbosity = verbose;
+	if (verbosity <= 0) {
+		verbosity = 0;
+		advice = 0;
+	} else if (verbosity > 5)
+		verbosity = 5;
+	/* Git does not run on EBCDIC, so we rely on ASCII: */
+	tmp[0] += verbosity;
+	setenv("GIT_MERGE_VERBOSITY", tmp, 1);
+	return 0;
+}
+
 int cmd_sequencer__helper(int argc, const char **argv, const char *prefix)
 {
 	char *commit = NULL;
-- 
1.6.4.133.g8a5c8

```

## Johannes Schindelin, 2009-08-04 12:45

Subject: Re: [PATCH 1/3] sequencer: add "reset_almost_hard()" and related functions
Message-ID: <alpine.DEB.1.00.0908041443310.8306@pacific.mpi-cbg.de>
URL: https://gitlist.dev/e/alpine.DEB.1.00.0908041443310.8306%40pacific.mpi-cbg.de
In-Reply-To: <20090803024023.3794.90748.chriscool@tuxfamily.org>

```
Hi,

On Mon, 3 Aug 2009, Christian Couder wrote:

> diff --git a/builtin-sequencer--helper.c b/builtin-sequencer--helper.c
> index 1dda525..82a830d 100644
> --- a/builtin-sequencer--helper.c
> +++ b/builtin-sequencer--helper.c
> @@ -2,16 +2,95 @@

I do not have that file in my tree.  Yet your 1/3 already expects it to be 
there.

Well, I guess I will work on finishing my rebase-i-p branch next week, and 
then see what is there in Stephan's repository, and ask him why he went so 
silent (after all, his code should have been polished  enough to be 
included 11 months ago).

Ciao,
Dscho

```

## Christian Couder, 2009-08-04 13:03

Subject: Re: [PATCH 1/3] sequencer: add "reset_almost_hard()" and related functions
Message-ID: <c07716ae0908040603t12daa900l18178770a17780b1@mail.gmail.com>
URL: https://gitlist.dev/e/c07716ae0908040603t12daa900l18178770a17780b1%40mail.gmail.com
In-Reply-To: <alpine.DEB.1.00.0908041443310.8306@pacific.mpi-cbg.de>

```
Hi,

On Tue, Aug 4, 2009 at 2:45 PM, Johannes
Schindelin<Johannes.Schindelin@gmx.de> wrote:
> Hi,
>
> On Mon, 3 Aug 2009, Christian Couder wrote:
>
>> diff --git a/builtin-sequencer--helper.c b/builtin-sequencer--helper.c
>> index 1dda525..82a830d 100644
>> --- a/builtin-sequencer--helper.c
>> +++ b/builtin-sequencer--helper.c
>> @@ -2,16 +2,95 @@
>
> I do not have that file in my tree.  Yet your 1/3 already expects it to be
> there.

This file is in pu. One of my previous patch added it.

> Well, I guess I will work on finishing my rebase-i-p branch next week, and
> then see what is there in Stephan's repository, and ask him why he went so
> silent (after all, his code should have been polished  enough to be
> included 11 months ago).

Stephan started to work again on his repository a few days ago. He
integrated some of the changes I posted to this list and some
information about what is left to do to complete his work. That will
make it easier for me or anyone who wants to work on this. Thanks
Stephan!

Best regards,
Christian.

```
