index: make the index file format extensible.
... and move the cache-tree data into it. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 24, 2006 at 21:18 UTC
bad68ec92410cf47dd001aa9b95d0f24c5f4bf77
10 files changed
+147
-150
apply.c
+2
-9
@@ -12,10 +12,6 @@
12
#include "quote.h"
13
#include "blob.h"
14
15
-static unsigned char active_cache_sha1[20];
16
-static struct cache_tree *active_cache_tree;
17
-
18
-
15
// --check turns on checking that the working tree matches the
16
// files that are being modified, but doesn't apply the patch
17
// --stat does just a diffstat, and doesn't actually apply
@@ -1919,9 +1915,8 @@ static int apply_patch(int fd, const char *filename)
1915
if (write_index)
1916
newfd = hold_index_file_for_update(&cache_file, get_index_file());
1917
if (check_index) {
1922
- if (read_cache_1(active_cache_sha1) < 0)
1918
+ if (read_cache() < 0)
1919
die("unable to read index file");
1924
- active_cache_tree = read_cache_tree(active_cache_sha1);
1920
}
1921
1922
if ((check || apply) && check_patch_list(list) < 0)
@@ -1931,11 +1926,9 @@ static int apply_patch(int fd, const char *filename)
1926
write_out_results(list, skipped_patch);
1927
1928
if (write_index) {
1934
- if (write_cache_1(newfd, active_cache, active_nr,
1935
- active_cache_sha1) ||
1929
+ if (write_cache(newfd, active_cache, active_nr) ||
1930
commit_index_file(&cache_file))
1931
die("Unable to write new cachefile");
1938
- write_cache_tree(active_cache_sha1, active_cache_tree);
1932
}
1933
1934
if (show_index_info)
cache-tree.c
+26
-82
@@ -11,16 +11,18 @@ struct cache_tree *cache_tree(void)
11
return it;
12
}
13
14
-void cache_tree_free(struct cache_tree *it)
14
+void cache_tree_free(struct cache_tree **it_p)
15
{
16
int i;
17
+ struct cache_tree *it = *it_p;
18
19
if (!it)
20
return;
21
for (i = 0; i < it->subtree_nr; i++)
21
- cache_tree_free(it->down[i]->cache_tree);
22
+ cache_tree_free(&it->down[i]->cache_tree);
23
free(it->down);
24
free(it);
25
+ *it_p = NULL;
26
}
27
28
static struct cache_tree_sub *find_subtree(struct cache_tree *it,
@@ -78,7 +80,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
80
break;
81
}
82
if (i < it->subtree_nr) {
81
- cache_tree_free(it->down[i]->cache_tree);
83
+ cache_tree_free(&it->down[i]->cache_tree);
84
free(it->down[i]);
85
/* 0 1 2 3 4 5
86
* ^ ^subtree_nr = 6
@@ -159,13 +161,27 @@ static void discard_unused_subtrees(struct cache_tree *it)
161
if (s->used)
162
down[dst++] = s;
163
else {
162
- cache_tree_free(s->cache_tree);
164
+ cache_tree_free(&s->cache_tree);
165
free(s);
166
it->subtree_nr--;
167
}
168
}
169
}
170
171
+int cache_tree_fully_valid(struct cache_tree *it)
172
+{
173
+ int i;
174
+ if (!it)
175
+ return 0;
176
+ if (it->entry_count < 0 || !has_sha1_file(it->sha1))
177
+ return 0;
178
+ for (i = 0; i < it->subtree_nr; i++) {
179
+ if (!cache_tree_fully_valid(it->down[i]->cache_tree))
180
+ return 0;
181
+ }
182
+ return 1;
183
+}
184
+
185
static int update_one(struct cache_tree *it,
186
struct cache_entry **cache,
187
int entries,
@@ -354,19 +370,15 @@ static void *write_one(struct cache_tree *it,
370
return buffer;
371
}
372
357
-static void *cache_tree_write(const unsigned char *cache_sha1,
358
- struct cache_tree *root,
359
- unsigned long *offset_p)
373
+void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
374
{
375
char path[PATH_MAX];
376
unsigned long size = 8192;
377
char *buffer = xmalloc(size);
378
365
- /* the cache checksum of the corresponding index file. */
366
- memcpy(buffer, cache_sha1, 20);
367
- *offset_p = 20;
379
+ *size_p = 0;
380
path[0] = 0;
369
- return write_one(root, path, 0, buffer, &size, offset_p);
381
+ return write_one(root, path, 0, buffer, &size, size_p);
382
}
383
384
static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
@@ -439,81 +451,13 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
451
return it;
452
453
free_return:
442
- cache_tree_free(it);
454
+ cache_tree_free(&it);
455
return NULL;
456
}
457
446
-static struct cache_tree *cache_tree_read(unsigned char *sha1,
447
- const char *buffer,
448
- unsigned long size)
458
+struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
459
{
450
- /* check the cache-tree matches the index */
451
- if (memcmp(buffer, sha1, 20))
452
- return NULL; /* checksum mismatch */
453
- if (buffer[20])
460
+ if (buffer[0])
461
return NULL; /* not the whole tree */
455
- buffer += 20;
456
- size -= 20;
462
return read_one(&buffer, &size);
463
}
459
-
460
-struct cache_tree *read_cache_tree(unsigned char *sha1)
461
-{
462
- int fd;
463
- struct stat st;
464
- char path[PATH_MAX];
465
- unsigned long size = 0;
466
- void *map;
467
- struct cache_tree *it;
468
-
469
- sprintf(path, "%s.aux", get_index_file());
470
- fd = open(path, O_RDONLY);
471
- if (fd < 0)
472
- return cache_tree();
473
-
474
- if (fstat(fd, &st))
475
- return cache_tree();
476
- size = st.st_size;
477
- map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
478
- close(fd);
479
- if (map == MAP_FAILED)
480
- return cache_tree();
481
- it = cache_tree_read(sha1, map, size);
482
- munmap(map, size);
483
- if (!it)
484
- return cache_tree();
485
- return it;
486
-}
487
-
488
-int write_cache_tree(const unsigned char *sha1, struct cache_tree *root)
489
-{
490
- char path[PATH_MAX];
491
- unsigned long size = 0;
492
- void *buf, *buffer;
493
- int fd, ret = -1;
494
-
495
- sprintf(path, "%s.aux", get_index_file());
496
- if (!root) {
497
- unlink(path);
498
- return -1;
499
- }
500
- fd = open(path, O_WRONLY|O_CREAT, 0666);
501
- if (fd < 0)
502
- return -1;
503
- buffer = buf = cache_tree_write(sha1, root, &size);
504
- while (size) {
505
- int written = xwrite(fd, buf, size);
506
- if (written <= 0)
507
- goto fail;
508
- buf += written;
509
- size -= written;
510
- }
511
- ret = 0;
512
-
513
- fail:
514
- close(fd);
515
- free(buffer);
516
- if (ret)
517
- unlink(path);
518
- return ret;
519
-}
cache-tree.h
+5
-4
@@ -18,12 +18,13 @@ struct cache_tree {
18
};
19
20
struct cache_tree *cache_tree(void);
21
-void cache_tree_free(struct cache_tree *);
21
+void cache_tree_free(struct cache_tree **);
22
void cache_tree_invalidate_path(struct cache_tree *, const char *);
23
24
-int write_cache_tree(const unsigned char *, struct cache_tree *);
25
-struct cache_tree *read_cache_tree(unsigned char *);
26
-int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
24
+void *cache_tree_write(struct cache_tree *root, unsigned long *size_p);
25
+struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
26
27
+int cache_tree_fully_valid(struct cache_tree *);
28
+int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
29
30
#endif
cache.h
+2
-4
@@ -114,6 +114,7 @@ static inline unsigned int create_ce_mode(unsigned int mode)
114
115
extern struct cache_entry **active_cache;
116
extern unsigned int active_nr, active_alloc, active_cache_changed;
117
+extern struct cache_tree *active_cache_tree;
118
119
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
120
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
@@ -138,11 +139,8 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path
139
#define alloc_nr(x) (((x)+16)*3/2)
140
141
/* Initialize and use the cache information */
141
-extern int read_cache_1(unsigned char *);
142
-extern int write_cache_1(int, struct cache_entry **, int, unsigned char *);
142
extern int read_cache(void);
144
-extern int write_cache(int, struct cache_entry **, int);
145
-
143
+extern int write_cache(int newfd, struct cache_entry **cache, int entries);
144
extern int cache_name_pos(const char *name, int namelen);
145
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
146
#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
checkout-index.c
+1
@@ -39,6 +39,7 @@
39
#include "cache.h"
40
#include "strbuf.h"
41
#include "quote.h"
42
+#include "cache-tree.h"
43
44
#define CHECKOUT_ALL 4
45
static const char *prefix;
dump-cache-tree.c
+3
-5
@@ -2,12 +2,11 @@
2
#include "tree.h"
3
#include "cache-tree.h"
4
5
-static unsigned char active_cache_sha1[20];
6
-static struct cache_tree *active_cache_tree;
7
-
5
static void dump_cache_tree(struct cache_tree *it, const char *pfx)
6
{
7
int i;
8
+ if (!it)
9
+ return;
10
if (it->entry_count < 0)
11
printf("%-40s %s\n", "invalid", pfx);
12
else
@@ -24,9 +23,8 @@ static void dump_cache_tree(struct cache_tree *it, const char *pfx)
23
24
int main(int ac, char **av)
25
{
27
- if (read_cache_1(active_cache_sha1) < 0)
26
+ if (read_cache() < 0)
27
die("unable to read index file");
29
- active_cache_tree = read_cache_tree(active_cache_sha1);
28
dump_cache_tree(active_cache_tree, "");
29
return 0;
30
}
read-cache.c
+79
-26
@@ -4,11 +4,26 @@
4
* Copyright (C) Linus Torvalds, 2005
5
*/
6
#include "cache.h"
7
+#include "cache-tree.h"
8
+
9
+/* Index extensions.
10
+ *
11
+ * The first letter should be 'A'..'Z' for extensions that are not
12
+ * necessary for a correct operation (i.e. optimization data).
13
+ * When new extensions are added that _needs_ to be understood in
14
+ * order to correctly interpret the index file, pick character that
15
+ * is outside the range, to cause the reader to abort.
16
+ */
17
+
18
+#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
19
+#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
20
21
struct cache_entry **active_cache = NULL;
22
static time_t index_file_timestamp;
23
unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
24
25
+struct cache_tree *active_cache_tree = NULL;
26
+
27
/*
28
* This only updates the "non-critical" parts of the directory
29
* cache, ie the parts that aren't tracked by GIT, and only used
@@ -496,12 +511,10 @@ int add_cache_entry(struct cache_entry *ce, int option)
511
return 0;
512
}
513
499
-static int verify_hdr(struct cache_header *hdr, unsigned long size, unsigned char *sha1)
514
+static int verify_hdr(struct cache_header *hdr, unsigned long size)
515
{
516
SHA_CTX c;
502
- unsigned char sha1_buf[20];
503
- if (!sha1)
504
- sha1 = sha1_buf;
517
+ unsigned char sha1[20];
518
519
if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
520
return error("bad signature");
@@ -515,7 +528,23 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size, unsigned cha
528
return 0;
529
}
530
518
-int read_cache_1(unsigned char *cache_sha1)
531
+static int read_index_extension(const char *ext, void *data, unsigned long sz)
532
+{
533
+ switch (CACHE_EXT(ext)) {
534
+ case CACHE_EXT_TREE:
535
+ active_cache_tree = cache_tree_read(data, sz);
536
+ break;
537
+ default:
538
+ if (*ext < 'A' || 'Z' < *ext)
539
+ return error("index uses %.4s extension, which we do not understand",
540
+ ext);
541
+ fprintf(stderr, "ignoring %.4s extension\n", ext);
542
+ break;
543
+ }
544
+ return 0;
545
+}
546
+
547
+int read_cache(void)
548
{
549
int fd, i;
550
struct stat st;
@@ -549,7 +578,7 @@ int read_cache_1(unsigned char *cache_sha1)
578
die("index file mmap failed (%s)", strerror(errno));
579
580
hdr = map;
552
- if (verify_hdr(hdr, size, cache_sha1) < 0)
581
+ if (verify_hdr(hdr, size) < 0)
582
goto unmap;
583
584
active_nr = ntohl(hdr->hdr_entries);
@@ -563,6 +592,22 @@ int read_cache_1(unsigned char *cache_sha1)
592
active_cache[i] = ce;
593
}
594
index_file_timestamp = st.st_mtime;
595
+ while (offset <= size - 20 - 8) {
596
+ /* After an array of active_nr index entries,
597
+ * there can be arbitrary number of extended
598
+ * sections, each of which is prefixed with
599
+ * extension name (4-byte) and section length
600
+ * in 4-byte network byte order.
601
+ */
602
+ unsigned long extsize;
603
+ memcpy(&extsize, map + offset + 4, 4);
604
+ extsize = ntohl(extsize);
605
+ if (read_index_extension(map + offset,
606
+ map + offset + 8, extsize) < 0)
607
+ goto unmap;
608
+ offset += 8;
609
+ offset += extsize;
610
+ }
611
return active_nr;
612
613
unmap:
@@ -597,7 +642,18 @@ static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
642
return 0;
643
}
644
600
-static int ce_flush(SHA_CTX *context, int fd, unsigned char *sha1)
645
+static int write_index_ext_header(SHA_CTX *context, int fd,
646
+ unsigned long ext, unsigned long sz)
647
+{
648
+ ext = htonl(ext);
649
+ sz = htonl(sz);
650
+ if ((ce_write(context, fd, &ext, 4) < 0) ||
651
+ (ce_write(context, fd, &sz, 4) < 0))
652
+ return -1;
653
+ return 0;
654
+}
655
+
656
+static int ce_flush(SHA_CTX *context, int fd)
657
{
658
unsigned int left = write_buffer_len;
659
@@ -614,8 +670,7 @@ static int ce_flush(SHA_CTX *context, int fd, unsigned char *sha1)
670
}
671
672
/* Append the SHA1 signature at the end */
617
- SHA1_Final(sha1, context);
618
- memcpy(write_buffer + left, sha1, 20);
673
+ SHA1_Final(write_buffer + left, context);
674
left += 20;
675
if (write(fd, write_buffer, left) != left)
676
return -1;
@@ -666,14 +721,11 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
721
}
722
}
723
669
-int write_cache_1(int newfd, struct cache_entry **cache, int entries,
670
- unsigned char *cache_sha1)
724
+int write_cache(int newfd, struct cache_entry **cache, int entries)
725
{
726
SHA_CTX c;
727
struct cache_header hdr;
728
int i, removed;
675
- int status;
676
- unsigned char sha1[20];
729
730
for (i = removed = 0; i < entries; i++)
731
if (!cache[i]->ce_mode)
@@ -697,18 +749,19 @@ int write_cache_1(int newfd, struct cache_entry **cache, int entries,
749
if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
750
return -1;
751
}
700
- status = ce_flush(&c, newfd, sha1);
701
- if (cache_sha1)
702
- memcpy(cache_sha1, sha1, 20);
703
- return status;
704
-}
752
706
-int read_cache(void)
707
-{
708
- return read_cache_1(NULL);
709
-}
710
-
711
-int write_cache(int newfd, struct cache_entry **cache, int entries)
712
-{
713
- return write_cache_1(newfd, cache, entries, NULL);
753
+ /* Write extension data here */
754
+ if (active_cache_tree) {
755
+ unsigned long sz;
756
+ void *data = cache_tree_write(active_cache_tree, &sz);
757
+ if (data &&
758
+ !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&
759
+ !ce_write(&c, newfd, data, sz))
760
+ ;
761
+ else {
762
+ free(data);
763
+ return -1;
764
+ }
765
+ }
766
+ return ce_flush(&c, newfd);
767
}
read-tree.c
+2
@@ -9,6 +9,7 @@
9
10
#include "object.h"
11
#include "tree.h"
12
+#include "cache-tree.h"
13
#include <sys/time.h>
14
#include <signal.h>
15
@@ -828,6 +829,7 @@ int main(int argc, char **argv)
829
}
830
831
unpack_trees(fn);
832
+ cache_tree_free(&active_cache_tree);
833
if (write_cache(newfd, active_cache, active_nr) ||
834
commit_index_file(&cache_file))
835
die("unable to write new index file");
update-index.c
+2
-9
@@ -6,12 +6,8 @@
6
#include "cache.h"
7
#include "strbuf.h"
8
#include "quote.h"
9
-#include "tree.h"
9
#include "cache-tree.h"
10
12
-static unsigned char active_cache_sha1[20];
13
-static struct cache_tree *active_cache_tree;
14
-
11
/*
12
* Default to not allowing changes to the list of files. The
13
* tool doesn't actually care, but this makes it harder to add
@@ -501,10 +497,9 @@ int main(int argc, const char **argv)
497
if (newfd < 0)
498
die("unable to create new cachefile");
499
504
- entries = read_cache_1(active_cache_sha1);
500
+ entries = read_cache();
501
if (entries < 0)
502
die("cache corrupted");
507
- active_cache_tree = read_cache_tree(active_cache_sha1);
503
504
for (i = 1 ; i < argc; i++) {
505
const char *path = argv[i];
@@ -630,11 +625,9 @@ int main(int argc, const char **argv)
625
}
626
}
627
if (active_cache_changed) {
633
- if (write_cache_1(newfd, active_cache, active_nr,
634
- active_cache_sha1) ||
628
+ if (write_cache(newfd, active_cache, active_nr) ||
629
commit_index_file(&cache_file))
630
die("Unable to write new cachefile");
637
- write_cache_tree(active_cache_sha1, active_cache_tree);
631
}
632
633
return has_errors ? 1 : 0;
write-tree.c
+25
-11
@@ -7,21 +7,20 @@
7
#include "tree.h"
8
#include "cache-tree.h"
9
10
-static unsigned char active_cache_sha1[20];
11
-static struct cache_tree *active_cache_tree;
12
-
10
static int missing_ok = 0;
11
12
static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
13
14
+static struct cache_file cache_file;
15
+
16
int main(int argc, char **argv)
17
{
19
- int entries;
18
+ int entries, was_valid, newfd;
19
20
setup_git_directory();
21
23
- entries = read_cache_1(active_cache_sha1);
24
- active_cache_tree = read_cache_tree(active_cache_sha1);
22
+ newfd = hold_index_file_for_update(&cache_file, get_index_file());
23
+ entries = read_cache();
24
if (argc == 2) {
25
if (!strcmp(argv[1], "--missing-ok"))
26
missing_ok = 1;
@@ -35,11 +34,26 @@ int main(int argc, char **argv)
34
if (entries < 0)
35
die("git-write-tree: error reading cache");
36
38
- if (cache_tree_update(active_cache_tree, active_cache, active_nr,
39
- missing_ok))
40
- die("git-write-tree: error building trees");
41
- write_cache_tree(active_cache_sha1, active_cache_tree);
42
-
37
+ if (!active_cache_tree)
38
+ active_cache_tree = cache_tree();
39
+
40
+ was_valid = cache_tree_fully_valid(active_cache_tree);
41
+ if (!was_valid) {
42
+ if (cache_tree_update(active_cache_tree,
43
+ active_cache, active_nr,
44
+ missing_ok) < 0)
45
+ die("git-write-tree: error building trees");
46
+ if (0 <= newfd) {
47
+ if (!write_cache(newfd, active_cache, active_nr))
48
+ commit_index_file(&cache_file);
49
+ }
50
+ /* Not being able to write is fine -- we are only interested
51
+ * in updating the cache-tree part, and if the next caller
52
+ * ends up using the old index with unupdated cache-tree part
53
+ * it misses the work we did here, but that is just a
54
+ * performance penalty and not a big deal.
55
+ */
56
+ }
57
printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
58
return 0;
59
}