Skip to content

Commit e9ce897

Browse files
peffgitster
authored andcommitted
reset: make tree counting less confusing
Depending on whether we're in --keep mode, git-reset may feed one or two trees to unpack_trees(). We start a counter at "1" and then increment it to "2" only for the two-tree case. But that means we must always subtract one to find the correct array slot to fill with each descriptor. Instead, let's start at "0" and just increment our counter after adding each tree. This skips the extra subtraction, and will make things much easier when we start to actually free our tree buffers. While we're at it, let's make the first allocation use the slot at "desc + nr", too, even though we know "nr" is 0 at that point. It makes the two fill_tree_descriptor() calls consistent (always "desc + nr", followed by always incrementing "nr"). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c6b08d commit e9ce897

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

builtin/reset.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static inline int is_merge(void)
4444

4545
static int reset_index(const struct object_id *oid, int reset_type, int quiet)
4646
{
47-
int nr = 1;
47+
int nr = 0;
4848
struct tree_desc desc[2];
4949
struct tree *tree;
5050
struct unpack_trees_options opts;
@@ -75,14 +75,16 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet)
7575
struct object_id head_oid;
7676
if (get_oid("HEAD", &head_oid))
7777
return error(_("You do not have a valid HEAD."));
78-
if (!fill_tree_descriptor(desc, &head_oid))
78+
if (!fill_tree_descriptor(desc + nr, &head_oid))
7979
return error(_("Failed to find tree of HEAD."));
8080
nr++;
8181
opts.fn = twoway_merge;
8282
}
8383

84-
if (!fill_tree_descriptor(desc + nr - 1, oid))
84+
if (!fill_tree_descriptor(desc + nr, oid))
8585
return error(_("Failed to find tree of %s."), oid_to_hex(oid));
86+
nr++;
87+
8688
if (unpack_trees(nr, desc, &opts))
8789
return -1;
8890

0 commit comments

Comments
 (0)