Skip to content

Commit 724dd76

Browse files
newrengitster
authored andcommitted
cache-tree: share code between functions writing an index as a tree
write_tree_from_memory() appeared to be a merge-recursive special that basically duplicated write_index_as_tree(). The two have a different signature, but the bigger difference was just that write_index_as_tree() would always unconditionally read the index off of disk instead of working on the current in-memory index. So: * split out common code into write_index_as_tree_internal() * rename write_tree_from_memory() to write_inmemory_index_as_tree(), make it call write_index_as_tree_internal(), and move it to cache-tree.c Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 345480d commit 724dd76

File tree

5 files changed

+67
-58
lines changed

5 files changed

+67
-58
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
760760
*/
761761
init_merge_options(&o, the_repository);
762762
o.verbosity = 0;
763-
work = write_tree_from_memory(&o);
763+
work = write_in_core_index_as_tree(the_repository);
764764

765765
ret = reset_tree(new_tree,
766766
opts, 1,

cache-tree.c

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,66 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
608608
return it;
609609
}
610610

611+
static int write_index_as_tree_internal(struct object_id *oid,
612+
struct index_state *index_state,
613+
int cache_tree_valid,
614+
int flags,
615+
const char *prefix)
616+
{
617+
if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
618+
cache_tree_free(&index_state->cache_tree);
619+
cache_tree_valid = 0;
620+
}
621+
622+
if (!index_state->cache_tree)
623+
index_state->cache_tree = cache_tree();
624+
625+
if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
626+
return WRITE_TREE_UNMERGED_INDEX;
627+
628+
if (prefix) {
629+
struct cache_tree *subtree;
630+
subtree = cache_tree_find(index_state->cache_tree, prefix);
631+
if (!subtree)
632+
return WRITE_TREE_PREFIX_ERROR;
633+
oidcpy(oid, &subtree->oid);
634+
}
635+
else
636+
oidcpy(oid, &index_state->cache_tree->oid);
637+
638+
return 0;
639+
}
640+
641+
struct tree* write_in_core_index_as_tree(struct repository *repo) {
642+
struct object_id o;
643+
int was_valid, ret;
644+
645+
struct index_state *index_state = repo->index;
646+
was_valid = index_state->cache_tree &&
647+
cache_tree_fully_valid(index_state->cache_tree);
648+
649+
ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
650+
if (ret == WRITE_TREE_UNMERGED_INDEX) {
651+
int i;
652+
fprintf(stderr, "BUG: There are unmerged index entries:\n");
653+
for (i = 0; i < index_state->cache_nr; i++) {
654+
const struct cache_entry *ce = index_state->cache[i];
655+
if (ce_stage(ce))
656+
fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
657+
(int)ce_namelen(ce), ce->name);
658+
}
659+
BUG("unmerged index entries when writing inmemory index");
660+
}
661+
662+
return lookup_tree(repo, &index_state->cache_tree->oid);
663+
}
664+
665+
611666
int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
612667
{
613668
int entries, was_valid;
614669
struct lock_file lock_file = LOCK_INIT;
615-
int ret = 0;
670+
int ret;
616671

617672
hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
618673

@@ -621,18 +676,14 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
621676
ret = WRITE_TREE_UNREADABLE_INDEX;
622677
goto out;
623678
}
624-
if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
625-
cache_tree_free(&index_state->cache_tree);
626679

627-
if (!index_state->cache_tree)
628-
index_state->cache_tree = cache_tree();
680+
was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
681+
index_state->cache_tree &&
682+
cache_tree_fully_valid(index_state->cache_tree);
629683

630-
was_valid = cache_tree_fully_valid(index_state->cache_tree);
631-
if (!was_valid) {
632-
if (cache_tree_update(index_state, flags) < 0) {
633-
ret = WRITE_TREE_UNMERGED_INDEX;
634-
goto out;
635-
}
684+
ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
685+
prefix);
686+
if (!ret && !was_valid) {
636687
write_locked_index(index_state, &lock_file, COMMIT_LOCK);
637688
/* Not being able to write is fine -- we are only interested
638689
* in updating the cache-tree part, and if the next caller
@@ -642,18 +693,6 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
642693
*/
643694
}
644695

645-
if (prefix) {
646-
struct cache_tree *subtree;
647-
subtree = cache_tree_find(index_state->cache_tree, prefix);
648-
if (!subtree) {
649-
ret = WRITE_TREE_PREFIX_ERROR;
650-
goto out;
651-
}
652-
oidcpy(oid, &subtree->oid);
653-
}
654-
else
655-
oidcpy(oid, &index_state->cache_tree->oid);
656-
657696
out:
658697
rollback_lock_file(&lock_file);
659698
return ret;

cache-tree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int cache_tree_fully_valid(struct cache_tree *);
3434
int cache_tree_update(struct index_state *, int);
3535
void cache_tree_verify(struct repository *, struct index_state *);
3636

37-
/* bitmasks to write_cache_as_tree flags */
37+
/* bitmasks to write_index_as_tree flags */
3838
#define WRITE_TREE_MISSING_OK 1
3939
#define WRITE_TREE_IGNORE_CACHE_TREE 2
4040
#define WRITE_TREE_DRY_RUN 4
@@ -46,6 +46,7 @@ void cache_tree_verify(struct repository *, struct index_state *);
4646
#define WRITE_TREE_UNMERGED_INDEX (-2)
4747
#define WRITE_TREE_PREFIX_ERROR (-3)
4848

49+
struct tree* write_in_core_index_as_tree(struct repository *repo);
4950
int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix);
5051
void prime_cache_tree(struct repository *, struct index_state *, struct tree *);
5152

merge-recursive.c

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -412,37 +412,6 @@ static void unpack_trees_finish(struct merge_options *opt)
412412
clear_unpack_trees_porcelain(&opt->unpack_opts);
413413
}
414414

415-
struct tree *write_tree_from_memory(struct merge_options *opt)
416-
{
417-
struct tree *result = NULL;
418-
struct index_state *istate = opt->repo->index;
419-
420-
if (unmerged_index(istate)) {
421-
int i;
422-
fprintf(stderr, "BUG: There are unmerged index entries:\n");
423-
for (i = 0; i < istate->cache_nr; i++) {
424-
const struct cache_entry *ce = istate->cache[i];
425-
if (ce_stage(ce))
426-
fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
427-
(int)ce_namelen(ce), ce->name);
428-
}
429-
BUG("unmerged index entries in merge-recursive.c");
430-
}
431-
432-
if (!istate->cache_tree)
433-
istate->cache_tree = cache_tree();
434-
435-
if (!cache_tree_fully_valid(istate->cache_tree) &&
436-
cache_tree_update(istate, 0) < 0) {
437-
err(opt, _("error building trees"));
438-
return NULL;
439-
}
440-
441-
result = lookup_tree(opt->repo, &istate->cache_tree->oid);
442-
443-
return result;
444-
}
445-
446415
static int save_files_dirs(const struct object_id *oid,
447416
struct strbuf *base, const char *path,
448417
unsigned int mode, int stage, void *context)
@@ -3472,7 +3441,8 @@ static int merge_trees_internal(struct merge_options *opt,
34723441

34733442
unpack_trees_finish(opt);
34743443

3475-
if (opt->call_depth && !(*result = write_tree_from_memory(opt)))
3444+
if (opt->call_depth &&
3445+
!(*result = write_in_core_index_as_tree(opt->repo)))
34763446
return -1;
34773447

34783448
return clean;

merge-recursive.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ int merge_recursive_generic(struct merge_options *o,
113113

114114
void init_merge_options(struct merge_options *o,
115115
struct repository *repo);
116-
struct tree *write_tree_from_memory(struct merge_options *o);
117116

118117
int parse_merge_opt(struct merge_options *out, const char *s);
119118

0 commit comments

Comments
 (0)