Merging with D/F conflicts
cover Merging with D/F conflicts
I have been working on this on-and-off and it still is not finished, but I thought it would be a good place to stop, especially since this will not be part of the upcoming 1.6.5 anyway.
The first three patches are preparatory clean-ups.
The fourth patch roughly corresponds to Linus's 880386c (Prepare 'traverse_trees()' for D/F conflict lookahead, 2009-09-06), but it does implement multi-entry lookahead. It reveals why lookahead on the tree side alone is not sufficient by breaking a few tests.
The fifth one starts to compensate for the change in the tree side by preparing the side that walks the index for a similar lookahead mechanism, but it does not actually implement the lookahead yet.
The last one is a debugging patch.
This change has to break the output order (but not content) of the diff-index somewhat. If you had this:
Index Tree
b b-2/c
b-2 b/dthe expected output order from diff-index is b, b-2, b-2/c, then b/d. But if you walk the tree and the index in parallel, we would end up showing b, b/d, b-2 and then b-2/c. A sad part of the story is that diff-index always emits D/F conflicted entries as two independent records, so it is rather a bad match to the unpack_trees() framework to begin with.
The patches are designed to apply on 79b4fde (Merge branch 'maint', 2009-09-03).
Junio C Hamano (6):
diff-lib.c: fix misleading comments on oneway_diff()
unpack-trees: typofix
unpack_callback(): use unpack_failed() consistently traverse_trees(): handle D/F conflict case sanely unpack-trees.c: prepare for looking ahead in the index read-tree --debug-unpack
builtin-read-tree.c | 36 +++++++ cache.h | 2 + diff-lib.c | 20 +---- tree-walk.c | 277 +++++++++++++++++++++++++++++++++++++++++++-------- unpack-trees.c | 275 ++++++++++++++++++++++++++++++++++++++------------ unpack-trees.h | 3 +- 6 files changed, 484 insertions(+), 129 deletions(-)
PATCH 1/6 diff-lib.c: fix misleading comments on oneway_diff()
20a16eb (unpack_trees(): fix diff-index regression., 2008-03-10) adjusted diff-index to the new world order since 34110cd (Make 'unpack_trees()' have a separate source and destination index, 2008-03-06). Callbacks are expected to return anything non-negative as "success", and instead of reporting how many index entries they have processed, they are expected to advance o->pos themselves. The code did so, but a stale comment was left behind.
--- diff-lib.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff-lib.c +1 -1
@@ -383,7 +383,7 @@ static inline void skip_same_name(struct cache_entry *ce, struct unpack_trees_op* For diffing, the index is more important, and we only have a * single tree. * -* We're supposed to return how many index entries we want to skip. +* We're supposed to advance o->pos to skip what we have already processed. * * This wrapper makes it all more readable, and takes care of all * the fairly complex unpack_trees() semantic requirements, including
Signature · 1 line
1.6.5.rc1.90.ga3b1b
PATCH 2/6 unpack-trees: typofix
In reply to Junio C HamanoI am not good at subject-verb concordance.
--- unpack-trees.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
unpack-trees.c +1 -1
@@ -617,7 +617,7 @@ static int verify_absent(struct cache_entry *ce, const char *action,* found "foo/." in the working tree. * This is tricky -- if we have modified * files that are in "foo/" we would lose -* it. +* them. */ ret =verify_clean_subdirectory (ce, action, o);if (ret <0 )
Signature · 1 line
1.6.5.rc1.90.ga3b1b
PATCH 3/6 unpack_callback(): use unpack_failed() consistently
In reply to Junio C HamanoWhen unpack_index_entry() failed, consistently call unpack_failed(), instead of silently returning -1.
--- unpack-trees.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
unpack-trees.c +12 -12
@@ -277,6 +277,17 @@ static int unpack_nondirectories(int n, unsigned long mask,return 0 ; } +static int unpack_failed (struct unpack_trees_options *o,const char *message) +{ +discard_index (&o->result); +if (!o->gently) { +if (message) +return error ("%s" , message); +return -1 ; + } +return -1 ; +} +static int unpack_callback (int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) {struct cache_entry *src[MAX_UNPACK_TREES +1 ] = {NULL , };
@@ -294,7 +305,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, strint cmp =compare_entry (ce, info, p);if (cmp <0 ) {if (unpack_index_entry (ce, o) <0 ) -return -1 ; +return unpack_failed (o,NULL );continue ; }if (!cmp) {
@@ -352,17 +363,6 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, strreturn mask; } -static int unpack_failed (struct unpack_trees_options *o,const char *message) -{ -discard_index (&o->result); -if (!o->gently) { -if (message) -return error ("%s" , message); -return -1 ; - } -return -1 ; -} -/* * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the * resulting index, -2 on failure to reflect the changes to the work tree.
Signature · 1 line
1.6.5.rc1.90.ga3b1b
PATCH 4/6 traverse_trees(): handle D/F conflict case sanely
In reply to Junio C Hamanotraverse_trees() is supposed to call its callback with all the matching entries from the given trees. The current algorithm keeps a pointer to each of the tree being traversed, and feeds the entry with the earliest name to the callback.
This breaks down if the trees being traversed looks like this:
A B
t-1 t
t-2 u
t/a vWhen we are currently looking at an entry "t-1" in tree A, and tree B has returned "t", feeding "t" from the B and not feeding anything from A, only because "t-1" sorts later than "t", will miss an entry for a subtree "t" behind the current entry in tree A.
This introduces extended_entry_extract() helper function that gives what name is expected from the tree, and implements a mechanism to look-ahead in the tree object using it, to make sure such a case is handled sanely. Traversal in tree A in the above example will first return "t" to match that of B, and then the next request for an entry to A then returns "t-1".
This roughly corresponds to what Linus's "prepare for one-entry lookahead" wanted to do, but because this does implement look ahead, t6035 and t7003 reveal that the approach would not work without adjusting the side that walks the index in unpack_trees() as well.
--- tree-walk.c | 277 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 234 insertions(+), 43 deletions(-)
tree-walk.c +234 -43
@@ -60,13 +60,6 @@ void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)return buf; } -static int entry_compare (struct name_entry *a,struct name_entry *b) -{ -return df_name_compare ( - a->path,tree_entry_len (a->path, a->sha1), a->mode, - b->path,tree_entry_len (b->path, b->sha1), b->mode); -} -static void entry_clear (struct name_entry *a) {memset (a,0 ,sizeof (*a));
@@ -138,66 +131,264 @@ char *make_traverse_path(char *path, const struct traverse_info *info, const strreturn path; } +struct tree_desc_skip { +struct tree_desc_skip *prev; +const void *ptr; +}; + +struct tree_desc_x { +struct tree_desc d; +struct tree_desc_skip *skip; +}; + +static int name_compare (const char *a,int a_len, +const char *b,int b_len) +{ +int len = (a_len < b_len) ? a_len : b_len; +int cmp =memcmp (a, b, len); +if (cmp) +return cmp; +return (a_len - b_len); +} + +static int check_entry_match (const char *a,int a_len,const char *b,int b_len) +{ +/* +* The caller wants to pick *a* from a tree or nothing. +* We are looking at *b* in a tree. +* +* (0) If a and b are the same name, we are trivially happy. +* +* There are three possibilities where *a* could be hiding +* behind *b*. +* +* (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no +* matter what. +* (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree; +* (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree. +* +* Otherwise we know *a* won't appear in the tree without +* scanning further. +*/ + +int cmp =name_compare (a, a_len, b, b_len); + +/* Most common case first -- reading sync'd trees */ +if (!cmp) +return cmp; + +if (0 < cmp) { +/* a comes after b; it does not matter if it is case (3) +if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/') +return 1; +*/ +return 1 ;/* keep looking */ + } + +/* b comes after a; are we looking at case (2)? */ +if (a_len < b_len && !memcmp (a, b, a_len) && b[a_len] <'/' ) +return 1 ;/* keep looking */ + +return -1 ;/* a cannot appear in the tree */ +} + +/* +* From the extended tree_desc, extract the first name entry, while +* paying attention to the candidate "first" name. Most importantly, +* when looking for an entry, if there are entries that sorts earlier +* in the tree object representation than that name, skip them and +* process the named entry first. We will remember that we haven't +* processed the first entry yet, and in the later call skip the +* entry we processed early when update_extended_entry() is called. +* +* E.g. if the underlying tree object has these entries: +* +* blob "t-1" +* blob "t-2" +* tree "t" +* blob "t=1" +* +* and the "first" asks for "t", remember that we still need to +* process "t-1" and "t-2" but extract "t". After processing the +* entry "t" from this call, the caller will let us know by calling +* update_extended_entry() that we can remember "t" has been processed +* already. +*/ + +static void extended_entry_extract (struct tree_desc_x *t, +struct name_entry *a, +const char *first, +int first_len) +{ +const char *path; +int len; +struct tree_desc probe; +struct tree_desc_skip *skip; + +/* +* Extract the first entry from the tree_desc, but skip the +* ones that we already returned in earlier rounds. +*/ +while (1 ) { +if (!t->d.size) { +entry_clear (a); +break ;/* not found */ + } +entry_extract (&t->d, a); +for (skip = t->skip; skip; skip = skip->prev) +if (a->path == skip->ptr) +break ;/* found */ +if (!skip) +break ; +/* We have processed this entry already. */ +update_tree_entry (&t->d); + } + +if (!first || !a->path) +return ; + +/* +* The caller wants "first" from this tree, or nothing. +*/ + path = a->path; + len =tree_entry_len (a->path, a->sha1); +switch (check_entry_match (first, first_len, path, len)) { +case -1 : +entry_clear (a); +case 0 : +return ; +default : +break ; + } + +/* +* We need to look-ahead -- we suspect that a subtree whose +* name is "first" may be hiding behind the current entry "path". +*/ + probe = t->d; +while (probe.size) { +entry_extract (&probe, a); + path = a->path; + len =tree_entry_len (a->path, a->sha1); +switch (check_entry_match (first, first_len, path, len)) { +case -1 : +entry_clear (a); +case 0 : +return ; +default : +update_tree_entry (&probe); +break ; + } +/* keep looking */ + } +entry_clear (a); +} + +static void update_extended_entry (struct tree_desc_x *t,struct name_entry *a) +{ +if (t->d.entry.path == a->path) { +update_tree_entry (&t->d); + }else { +/* we have returned this entry early */ +struct tree_desc_skip *skip =xmalloc (sizeof (*skip)); + skip->ptr = a->path; + skip->prev = t->skip; + t->skip = skip; + } +} + +static void free_extended_entry (struct tree_desc_x *t) +{ +struct tree_desc_skip *p, *s; + +for (s = t->skip; s; s = p) { + p = s->prev; +free (s); + } +} +int traverse_trees (int n,struct tree_desc *t,struct traverse_info *info) {int ret =0 ;struct name_entry *entry =xmalloc (n*sizeof (*entry)); +int i; +struct tree_desc_x *tx =xcalloc (n,sizeof (*tx)); + +for (i =0 ; i < n; i++) + tx[i].d = t[i];for (;;) { -unsigned long mask =0 ; -unsigned long dirmask =0 ; -int i, last; +unsigned long mask, dirmask; +const char *first =NULL ; +int first_len =0 ; +struct name_entry *e; +int len; - last =-1 ;for (i =0 ; i < n; i++) { -if (!t[i].size) + e = entry + i; +extended_entry_extract (tx + i, e,NULL ,0 ); + } + +/* +* A tree may have "t-2" at the current location even +* though it may have "t" that is a subtree behind it, +* and another tree may return "t". We want to grab +* all "t" from all trees to match in such a case. +*/ +for (i =0 ; i < n; i++) { + e = entry + i; +if (!e->path)continue ; -entry_extract (t+i, entry+i); -if (last >=0 ) { -int cmp =entry_compare (entry+i, entry+last); - -/* -* Is the new name bigger than the old one? -* Ignore it -*/ -if (cmp >0 ) + len =tree_entry_len (e->path, e->sha1); +if (!first) { + first = e->path; + first_len = len; +continue ; + } +if (name_compare (e->path, len, first, first_len) <0 ) { + first = e->path; + first_len = len; + } + } + +if (first) { +for (i =0 ; i < n; i++) { + e = entry + i; +extended_entry_extract (tx + i, e, first, first_len); +/* Cull the ones that are not the earliest */ +if (!e->path)continue ; -/* -* Is the new name smaller than the old one? -* Ignore all old ones -*/ -if (cmp <0 ) - mask =0 ; + len =tree_entry_len (e->path, e->sha1); +if (name_compare (e->path, len, first, first_len)) +entry_clear (e); } + } + +/* Now we have in entry[i] the earliest name from the trees */ + mask =0 ; + dirmask =0 ; +for (i =0 ; i < n; i++) { +if (!entry[i].path) +continue ; mask |=1ul << i;if (S_ISDIR (entry[i].mode)) dirmask |=1ul << i; - last = i; }if (!mask)break ; - dirmask &= mask; - -/* -* Clear all the unused name-entries. -*/ -for (i =0 ; i < n; i++) { -if (mask & (1ul << i)) -continue ; -entry_clear (entry + i); - } ret = info->fn (n, mask, dirmask, entry, info);if (ret <0 )break ; -if (ret) - mask &= ret; + mask &= ret; ret =0 ; -for (i =0 ; i < n; i++) { +for (i =0 ; i < n; i++)if (mask & (1ul << i)) -update_tree_entry (t + i); - } +update_extended_entry (tx + i, entry + i); }free (entry); +for (i =0 ; i < n; i++) +free_extended_entry (tx + i); +free (tx);return ret; }
Signature · 1 line
1.6.5.rc1.90.ga3b1b
PATCH 5/6 unpack-trees.c: prepare for looking ahead in the index
In reply to Junio C HamanoThis prepares but does not yet implement a look-ahead in the index entries when traverse-trees.c decides to give us tree entries in an order that does not match what is in the index.
A case where a look-ahead in the index is necessary happens when merging branch B into branch A while the index matches the current branch A, using a tree O as their common ancestor, and these three trees looks like this:
O A B
t t
t-i t-i t-i
t-j t-j
t/1
t/2The traverse_trees() function gets "t", "t-i" and "t" from trees O, A and B first, and notices that A may have a matching "t" behind "t-i" and "t-j" (indeed it does), and tells A to give that entry instead. After unpacking blob "t" from tree B (as it hasn't changed since O in B and A removed it, it will result in its removal), it descends into directory "t/".
The side that walked index in parallel to the tree traversal used to be implemented with one pointer, o->pos, that points at the next index entry to be processed. When this happens, the pointer o->pos still points at "t-i" that is the first entry. We should be able to skip "t-i" and "t-j" and locate "t/1" from the index while the recursive invocation of traverse_trees() walks and match entries found there, and later come back to process "t-i".
While that look-ahead is not implemented yet, this adds a flag bit, CE_UNPACKED, to mark the entries in the index that has already been processed. o->pos pointer has been renamed to o->cache_bottom and it points at the first entry that may still need to be processed.
---
* t6035 is still broken with this patch, although t7003 is fixed.
cache.h | 2 + diff-lib.c | 18 ----- unpack-trees.c | 214 ++++++++++++++++++++++++++++++++++++++++++-------------- unpack-trees.h | 2 +- 4 files changed, 164 insertions(+), 72 deletions(-)
cache.h +2 -0
@@ -177,6 +177,8 @@ struct cache_entry {#define CE_HASHED (0x100000)#define CE_UNHASHED (0x200000) +#define CE_UNPACKED (0x400000) +/* * Extended on-disk flags */
diff-lib.c +0 -18
@@ -359,21 +359,6 @@ static void do_oneway_diff(struct unpack_trees_options *o,show_modified (revs, tree, idx,1 , cached, match_missing); } -static inline void skip_same_name (struct cache_entry *ce,struct unpack_trees_options *o) -{ -int len =ce_namelen (ce); -const struct index_state *index = o->src_index; - -while (o->pos < index->cache_nr) { -struct cache_entry *next = index->cache[o->pos]; -if (len !=ce_namelen (next)) -break ; -if (memcmp (ce->name, next->name, len)) -break ; - o->pos++; - } -} -/* * The unpack_trees() interface is designed for merging, so * the different source entries are designed primarily for
@@ -395,9 +380,6 @@ static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)struct cache_entry *tree = src[1 ];struct rev_info *revs = o->unpack_data; -if (idx &&ce_stage (idx)) -skip_same_name (idx, o); -/* * Unpack-trees generates a DF/conflict entry if * there was a directory in the index and a tree
unpack-trees.c +161 -53
310 lines not on this page · raw message
unpack-trees.h +1 -1
@@ -30,7 +30,7 @@ struct unpack_trees_options { diff_index_cached, gently;const char *prefix; -int pos; +int cache_bottom;struct dir_struct *dir;merge_fn_t fn;struct unpack_trees_error_msgs msgs;
Signature · 1 line
1.6.5.rc1.90.ga3b1b
PATCH 6/6 read-tree --debug-unpack
In reply to Junio C HamanoDebugging patch.
--- builtin-read-tree.c | 36 ++++++++++++++++++++++++++++++++++++ unpack-trees.c | 35 +++++++++++++++++++++++++++++++++++ unpack-trees.h | 1 + 3 files changed, 72 insertions(+), 0 deletions(-)
builtin-read-tree.c +36 -0
@@ -64,6 +64,34 @@ static int exclude_per_directory_cb(const struct option *opt, const char *arg,return 0 ; } +static void debug_stage (const char *label,struct cache_entry *ce, +struct unpack_trees_options *o) +{ +printf ("%s " , label); +if (!ce) +printf ("(missing)\n" ); +else if (ce == o->df_conflict_entry) +printf ("(conflict)\n" ); +else +printf ("%06o #%d %s %.8s\n" , + ce->ce_mode,ce_stage (ce), ce->name, +sha1_to_hex (ce->sha1)); +} + +static int debug_merge (struct cache_entry **stages,struct unpack_trees_options *o) +{ +int i; + +printf ("* %d-way merge\n" , o->merge_size); +debug_stage ("index" , stages[0 ], o); +for (i =1 ; i <= o->merge_size; i++) { +char buf[24 ]; +sprintf (buf,"ent#%d" , i); +debug_stage (buf, stages[i], o); + } +return 0 ; +} +static struct lock_file lock_file;int cmd_read_tree (int argc,const char **argv,const char *unused_prefix)
@@ -98,6 +126,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) PARSE_OPT_NONEG, exclude_per_directory_cb }, OPT_SET_INT('i', NULL, &opts.index_only, "don't check the working tree after merging", 1), + OPT_SET_INT(0, "debug-unpack", &opts.debug_unpack, + "debug unpack-trees", 1), OPT_END() };
@@ -165,6 +195,9 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) opts.head_idx =1 ; } +if (opts.debug_unpack) + opts.fn = debug_merge; +cache_tree_free (&active_cache_tree);for (i =0 ; i < nr_trees; i++) {struct tree *tree = trees[i];
@@ -174,6 +207,9 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)if (unpack_trees (nr_trees, t, &opts))return 128 ; +if (opts.debug_unpack) +return 0 ;/* do not write the index out */ +/* * When reading only one tree (either the most basic form, * "-m ent" or "--reset ent" form), we can obtain a fully
unpack-trees.c +35 -0
@@ -393,6 +393,38 @@ static int unpack_failed(struct unpack_trees_options *o, const char *message)return -1 ; } +static void debug_path (struct traverse_info *info) +{ +if (info->prev) { +debug_path (info->prev); +if (*info->prev->name.path) +putchar ('/' ); + } +printf ("%s" , info->name.path); +} + +static void debug_name_entry (int i,struct name_entry *n) +{ +printf ("ent#%d %06o %s\n" , i, + n->path ? n->mode :0 , + n->path ? n->path :"(missing)" ); +} + +static void debug_unpack_callback (int n, +unsigned long mask, +unsigned long dirmask, +struct name_entry *names, +struct traverse_info *info) +{ +int i; +printf ("* unpack mask %lu, dirmask %lu, cnt %d " , + mask, dirmask, n); +debug_path (info); +putchar ('\n' ); +for (i =0 ; i < n; i++) +debug_name_entry (i, names + i); +} +static int unpack_callback (int n,unsigned long mask,unsigned long dirmask,struct name_entry *names,struct traverse_info *info) {struct cache_entry *src[MAX_UNPACK_TREES +1 ] = {NULL , };
@@ -403,6 +435,9 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, strwhile (!p->mode) p++; +if (o->debug_unpack) +debug_unpack_callback (n, mask, dirmask, names, info); +/* Are we supposed to look at the index too? */ if (o->merge) {while (1 ) {
unpack-trees.h +1 -0
@@ -28,6 +28,7 @@ struct unpack_trees_options { skip_unmerged, initial_checkout, diff_index_cached, + debug_unpack, gently;const char *prefix;int cache_bottom;
Signature · 1 line
1.6.5.rc1.90.ga3b1b