Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Paul Tan authored and Junio C Hamano committed Aug 5, 2015
1 parent eb898b8 commit d23a511
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
29 changes: 17 additions & 12 deletions cache-tree.c
Expand Up @@ -592,7 +592,7 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
return it;
}

int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
int write_index_as_tree(unsigned char *sha1, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
{
int entries, was_valid, newfd;
struct lock_file *lock_file;
Expand All @@ -603,23 +603,23 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
*/
lock_file = xcalloc(1, sizeof(struct lock_file));

newfd = hold_locked_index(lock_file, 1);
newfd = hold_lock_file_for_update(lock_file, index_path, LOCK_DIE_ON_ERROR);

entries = read_cache();
entries = read_index_from(index_state, index_path);
if (entries < 0)
return WRITE_TREE_UNREADABLE_INDEX;
if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
cache_tree_free(&(active_cache_tree));
cache_tree_free(&index_state->cache_tree);

if (!active_cache_tree)
active_cache_tree = cache_tree();
if (!index_state->cache_tree)
index_state->cache_tree = cache_tree();

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

if (prefix) {
struct cache_tree *subtree =
cache_tree_find(active_cache_tree, prefix);
struct cache_tree *subtree;
subtree = cache_tree_find(index_state->cache_tree, prefix);
if (!subtree)
return WRITE_TREE_PREFIX_ERROR;
hashcpy(sha1, subtree->sha1);
}
else
hashcpy(sha1, active_cache_tree->sha1);
hashcpy(sha1, index_state->cache_tree->sha1);

if (0 <= newfd)
rollback_lock_file(lock_file);

return 0;
}

int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
{
return write_index_as_tree(sha1, &the_index, get_index_file(), flags, prefix);
}

static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
{
struct tree_desc desc;
Expand Down
1 change: 1 addition & 0 deletions cache-tree.h
Expand Up @@ -46,6 +46,7 @@ int update_main_cache_tree(int);
#define WRITE_TREE_UNMERGED_INDEX (-2)
#define WRITE_TREE_PREFIX_ERROR (-3)

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

Expand Down

0 comments on commit d23a511

Please sign in to comment.