Skip to content

Commit d11b8d3

Browse files
committed
write-tree --ignore-cache-tree
This allows you to discard the cache-tree information before writing the tree out of the index (i.e. it always recomputes the tree object names for all the subtrees). This is only useful as a debug option, so I did not bother documenting it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d00e364 commit d11b8d3

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

builtin-write-tree.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static const char write_tree_usage[] =
1313

1414
int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
1515
{
16-
int missing_ok = 0, ret;
16+
int flags = 0, ret;
1717
const char *prefix = NULL;
1818
unsigned char sha1[20];
1919
const char *me = "git-write-tree";
@@ -22,9 +22,15 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
2222
while (1 < argc) {
2323
const char *arg = argv[1];
2424
if (!strcmp(arg, "--missing-ok"))
25-
missing_ok = 1;
25+
flags |= WRITE_TREE_MISSING_OK;
2626
else if (!prefixcmp(arg, "--prefix="))
2727
prefix = arg + 9;
28+
else if (!prefixcmp(arg, "--ignore-cache-tree"))
29+
/*
30+
* This is only useful for debugging, so I
31+
* do not bother documenting it.
32+
*/
33+
flags |= WRITE_TREE_IGNORE_CACHE_TREE;
2834
else
2935
usage(write_tree_usage);
3036
argc--; argv++;
@@ -33,7 +39,7 @@ int cmd_write_tree(int argc, const char **argv, const char *unused_prefix)
3339
if (argc > 2)
3440
die("too many options");
3541

36-
ret = write_cache_as_tree(sha1, missing_ok, prefix);
42+
ret = write_cache_as_tree(sha1, flags, prefix);
3743
switch (ret) {
3844
case 0:
3945
printf("%s\n", sha1_to_hex(sha1));

cache-tree.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,28 +538,32 @@ static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *pat
538538
return it;
539539
}
540540

541-
int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix)
541+
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
542542
{
543543
int entries, was_valid, newfd;
544+
struct lock_file *lock_file;
544545

545546
/*
546547
* We can't free this memory, it becomes part of a linked list
547548
* parsed atexit()
548549
*/
549-
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
550+
lock_file = xcalloc(1, sizeof(struct lock_file));
550551

551552
newfd = hold_locked_index(lock_file, 1);
552553

553554
entries = read_cache();
554555
if (entries < 0)
555556
return WRITE_TREE_UNREADABLE_INDEX;
557+
if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
558+
cache_tree_free(&(active_cache_tree));
556559

557560
if (!active_cache_tree)
558561
active_cache_tree = cache_tree();
559562

560563
was_valid = cache_tree_fully_valid(active_cache_tree);
561-
562564
if (!was_valid) {
565+
int missing_ok = flags & WRITE_TREE_MISSING_OK;
566+
563567
if (cache_tree_update(active_cache_tree,
564568
active_cache, active_nr,
565569
missing_ok, 0) < 0)

cache-tree.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3030
int cache_tree_fully_valid(struct cache_tree *);
3131
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int);
3232

33+
/* bitmasks to write_cache_as_tree flags */
34+
#define WRITE_TREE_MISSING_OK 1
35+
#define WRITE_TREE_IGNORE_CACHE_TREE 2
36+
37+
/* error return codes */
3338
#define WRITE_TREE_UNREADABLE_INDEX (-1)
3439
#define WRITE_TREE_UNMERGED_INDEX (-2)
3540
#define WRITE_TREE_PREFIX_ERROR (-3)
3641

37-
int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix);
42+
int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix);
3843
void prime_cache_tree(struct cache_tree **, struct tree *);
3944

4045
#endif

0 commit comments

Comments
 (0)