Skip to content

Commit d23a511

Browse files
pyokagangitster
authored andcommitted
cache-tree: introduce write_index_as_tree()
A caller may wish to write a temporary index as a tree. However, write_cache_as_tree() assumes that the index was read from, and will write to, the default index file path. Introduce write_index_as_tree() which removes this limitation by allowing the caller to specify its own index_state and index file path. Signed-off-by: Paul Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb898b8 commit d23a511

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

cache-tree.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
592592
return it;
593593
}
594594

595-
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
595+
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
596596
{
597597
int entries, was_valid, newfd;
598598
struct lock_file *lock_file;
@@ -603,23 +603,23 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
603603
*/
604604
lock_file = xcalloc(1, sizeof(struct lock_file));
605605

606-
newfd = hold_locked_index(lock_file, 1);
606+
newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);
607607

608-
entries = read_cache();
608+
entries = read_index_from(index_state, index_path);
609609
if (entries < 0)
610610
return WRITE_TREE_UNREADABLE_INDEX;
611611
if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
612-
cache_tree_free(&(active_cache_tree));
612+
cache_tree_free(&index_state->cache_tree);
613613

614-
if (!active_cache_tree)
615-
active_cache_tree = cache_tree();
614+
if (!index_state->cache_tree)
615+
index_state->cache_tree = cache_tree();
616616

617-
was_valid = cache_tree_fully_valid(active_cache_tree);
617+
was_valid = cache_tree_fully_valid(index_state->cache_tree);
618618
if (!was_valid) {
619-
if (cache_tree_update(&the_index, flags) < 0)
619+
if (cache_tree_update(index_state, flags) < 0)
620620
return WRITE_TREE_UNMERGED_INDEX;
621621
if (0 <= newfd) {
622-
if (!write_locked_index(&the_index, lock_file, COMMIT_LOCK))
622+
if (!write_locked_index(index_state, lock_file, COMMIT_LOCK))
623623
newfd = -1;
624624
}
625625
/* Not being able to write is fine -- we are only interested
@@ -631,21 +631,26 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
631631
}
632632

633633
if (prefix) {
634-
struct cache_tree *subtree =
635-
cache_tree_find(active_cache_tree, prefix);
634+
struct cache_tree *subtree;
635+
subtree = cache_tree_find(index_state->cache_tree, prefix);
636636
if (!subtree)
637637
return WRITE_TREE_PREFIX_ERROR;
638638
hashcpy(sha1, subtree->sha1);
639639
}
640640
else
641-
hashcpy(sha1, active_cache_tree->sha1);
641+
hashcpy(sha1, index_state->cache_tree->sha1);
642642

643643
if (0 <= newfd)
644644
rollback_lock_file(lock_file);
645645

646646
return 0;
647647
}
648648

649+
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
650+
{
651+
return write_index_as_tree(sha1, &the_index, get_index_file(), flags, prefix);
652+
}
653+
649654
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
650655
{
651656
struct tree_desc desc;

cache-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int update_main_cache_tree(int);
4646
#define WRITE_TREE_UNMERGED_INDEX (-2)
4747
#define WRITE_TREE_PREFIX_ERROR (-3)
4848

49+
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix);
4950
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix);
5051
void prime_cache_tree(struct index_state *, struct tree *);
5152

0 commit comments

Comments
 (0)