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 <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paul Tan committed
Aug 4, 2015 at 21:51 UTC
d23a5117f821b91e1d72745238a4c085a089864e
2 files changed
+18
-12
cache-tree.c
+17
-12
@@ -592,7 +592,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
592
return it;
593
}
594
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)
596
{
597
int entries, was_valid, newfd;
598
struct lock_file *lock_file;
@@ -603,23 +603,23 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
603
*/
604
lock_file = xcalloc(1, sizeof(struct lock_file));
605
606
- newfd = hold_locked_index(lock_file, 1);
606
+ newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);
607
608
- entries = read_cache();
608
+ entries = read_index_from(index_state, index_path);
609
if (entries < 0)
610
return WRITE_TREE_UNREADABLE_INDEX;
611
if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
612
- cache_tree_free(&(active_cache_tree));
612
+ cache_tree_free(&index_state->cache_tree);
613
614
- if (!active_cache_tree)
615
- active_cache_tree = cache_tree();
614
+ if (!index_state->cache_tree)
615
+ index_state->cache_tree = cache_tree();
616
617
- was_valid = cache_tree_fully_valid(active_cache_tree);
617
+ was_valid = cache_tree_fully_valid(index_state->cache_tree);
618
if (!was_valid) {
619
- if (cache_tree_update(&the_index, flags) < 0)
619
+ if (cache_tree_update(index_state, flags) < 0)
620
return WRITE_TREE_UNMERGED_INDEX;
621
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))
623
newfd = -1;
624
}
625
/* Not being able to write is fine -- we are only interested
@@ -631,14 +631,14 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
631
}
632
633
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);
636
if (!subtree)
637
return WRITE_TREE_PREFIX_ERROR;
638
hashcpy(sha1, subtree->sha1);
639
}
640
else
641
- hashcpy(sha1, active_cache_tree->sha1);
641
+ hashcpy(sha1, index_state->cache_tree->sha1);
642
643
if (0 <= newfd)
644
rollback_lock_file(lock_file);
@@ -646,6 +646,11 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
646
return 0;
647
}
648
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
+
654
static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
655
{
656
struct tree_desc desc;
cache-tree.h
+1
@@ -46,6 +46,7 @@ int update_main_cache_tree(int);
46
#define WRITE_TREE_UNMERGED_INDEX (-2)
47
#define WRITE_TREE_PREFIX_ERROR (-3)
48
49
+int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix);
50
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix);
51
void prime_cache_tree(struct index_state *, struct tree *);
52