Skip to content

Commit

Permalink
index: make the index file format extensible.
Browse files Browse the repository at this point in the history
... and move the cache-tree data into it.

Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
Junio C Hamano committed Apr 25, 2006
1 parent dd0c34c commit bad68ec
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 150 deletions.
11 changes: 2 additions & 9 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#include "quote.h"
#include "blob.h"

static unsigned char active_cache_sha1[20];
static struct cache_tree *active_cache_tree;


// --check turns on checking that the working tree matches the
// files that are being modified, but doesn't apply the patch
// --stat does just a diffstat, and doesn't actually apply
Expand Down Expand Up @@ -1919,9 +1915,8 @@ static int apply_patch(int fd, const char *filename)
if (write_index)
newfd = hold_index_file_for_update(&cache_file, get_index_file());
if (check_index) {
if (read_cache_1(active_cache_sha1) < 0)
if (read_cache() < 0)
die("unable to read index file");
active_cache_tree = read_cache_tree(active_cache_sha1);
}

if ((check || apply) && check_patch_list(list) < 0)
Expand All @@ -1931,11 +1926,9 @@ static int apply_patch(int fd, const char *filename)
write_out_results(list, skipped_patch);

if (write_index) {
if (write_cache_1(newfd, active_cache, active_nr,
active_cache_sha1) ||
if (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file))
die("Unable to write new cachefile");
write_cache_tree(active_cache_sha1, active_cache_tree);
}

if (show_index_info)
Expand Down
108 changes: 26 additions & 82 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ struct cache_tree *cache_tree(void)
return it;
}

void cache_tree_free(struct cache_tree *it)
void cache_tree_free(struct cache_tree **it_p)
{
int i;
struct cache_tree *it = *it_p;

if (!it)
return;
for (i = 0; i < it->subtree_nr; i++)
cache_tree_free(it->down[i]->cache_tree);
cache_tree_free(&it->down[i]->cache_tree);
free(it->down);
free(it);
*it_p = NULL;
}

static struct cache_tree_sub *find_subtree(struct cache_tree *it,
Expand Down Expand Up @@ -78,7 +80,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
break;
}
if (i < it->subtree_nr) {
cache_tree_free(it->down[i]->cache_tree);
cache_tree_free(&it->down[i]->cache_tree);
free(it->down[i]);
/* 0 1 2 3 4 5
* ^ ^subtree_nr = 6
Expand Down Expand Up @@ -159,13 +161,27 @@ static void discard_unused_subtrees(struct cache_tree *it)
if (s->used)
down[dst++] = s;
else {
cache_tree_free(s->cache_tree);
cache_tree_free(&s->cache_tree);
free(s);
it->subtree_nr--;
}
}
}

int cache_tree_fully_valid(struct cache_tree *it)
{
int i;
if (!it)
return 0;
if (it->entry_count < 0 || !has_sha1_file(it->sha1))
return 0;
for (i = 0; i < it->subtree_nr; i++) {
if (!cache_tree_fully_valid(it->down[i]->cache_tree))
return 0;
}
return 1;
}

static int update_one(struct cache_tree *it,
struct cache_entry **cache,
int entries,
Expand Down Expand Up @@ -354,19 +370,15 @@ static void *write_one(struct cache_tree *it,
return buffer;
}

static void *cache_tree_write(const unsigned char *cache_sha1,
struct cache_tree *root,
unsigned long *offset_p)
void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
{
char path[PATH_MAX];
unsigned long size = 8192;
char *buffer = xmalloc(size);

/* the cache checksum of the corresponding index file. */
memcpy(buffer, cache_sha1, 20);
*offset_p = 20;
*size_p = 0;
path[0] = 0;
return write_one(root, path, 0, buffer, &size, offset_p);
return write_one(root, path, 0, buffer, &size, size_p);
}

static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
Expand Down Expand Up @@ -439,81 +451,13 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
return it;

free_return:
cache_tree_free(it);
cache_tree_free(&it);
return NULL;
}

static struct cache_tree *cache_tree_read(unsigned char *sha1,
const char *buffer,
unsigned long size)
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
{
/* check the cache-tree matches the index */
if (memcmp(buffer, sha1, 20))
return NULL; /* checksum mismatch */
if (buffer[20])
if (buffer[0])
return NULL; /* not the whole tree */
buffer += 20;
size -= 20;
return read_one(&buffer, &size);
}

struct cache_tree *read_cache_tree(unsigned char *sha1)
{
int fd;
struct stat st;
char path[PATH_MAX];
unsigned long size = 0;
void *map;
struct cache_tree *it;

sprintf(path, "%s.aux", get_index_file());
fd = open(path, O_RDONLY);
if (fd < 0)
return cache_tree();

if (fstat(fd, &st))
return cache_tree();
size = st.st_size;
map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (map == MAP_FAILED)
return cache_tree();
it = cache_tree_read(sha1, map, size);
munmap(map, size);
if (!it)
return cache_tree();
return it;
}

int write_cache_tree(const unsigned char *sha1, struct cache_tree *root)
{
char path[PATH_MAX];
unsigned long size = 0;
void *buf, *buffer;
int fd, ret = -1;

sprintf(path, "%s.aux", get_index_file());
if (!root) {
unlink(path);
return -1;
}
fd = open(path, O_WRONLY|O_CREAT, 0666);
if (fd < 0)
return -1;
buffer = buf = cache_tree_write(sha1, root, &size);
while (size) {
int written = xwrite(fd, buf, size);
if (written <= 0)
goto fail;
buf += written;
size -= written;
}
ret = 0;

fail:
close(fd);
free(buffer);
if (ret)
unlink(path);
return ret;
}
9 changes: 5 additions & 4 deletions cache-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ struct cache_tree {
};

struct cache_tree *cache_tree(void);
void cache_tree_free(struct cache_tree *);
void cache_tree_free(struct cache_tree **);
void cache_tree_invalidate_path(struct cache_tree *, const char *);

int write_cache_tree(const unsigned char *, struct cache_tree *);
struct cache_tree *read_cache_tree(unsigned char *);
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
void *cache_tree_write(struct cache_tree *root, unsigned long *size_p);
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);

int cache_tree_fully_valid(struct cache_tree *);
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);

#endif
6 changes: 2 additions & 4 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static inline unsigned int create_ce_mode(unsigned int mode)

extern struct cache_entry **active_cache;
extern unsigned int active_nr, active_alloc, active_cache_changed;
extern struct cache_tree *active_cache_tree;

#define GIT_DIR_ENVIRONMENT "GIT_DIR"
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
Expand All @@ -138,11 +139,8 @@ extern const char *prefix_filename(const char *prefix, int len, const char *path
#define alloc_nr(x) (((x)+16)*3/2)

/* Initialize and use the cache information */
extern int read_cache_1(unsigned char *);
extern int write_cache_1(int, struct cache_entry **, int, unsigned char *);
extern int read_cache(void);
extern int write_cache(int, struct cache_entry **, int);

extern int write_cache(int newfd, struct cache_entry **cache, int entries);
extern int cache_name_pos(const char *name, int namelen);
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
#define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
Expand Down
1 change: 1 addition & 0 deletions checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "cache.h"
#include "strbuf.h"
#include "quote.h"
#include "cache-tree.h"

#define CHECKOUT_ALL 4
static const char *prefix;
Expand Down
8 changes: 3 additions & 5 deletions dump-cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
#include "tree.h"
#include "cache-tree.h"

static unsigned char active_cache_sha1[20];
static struct cache_tree *active_cache_tree;

static void dump_cache_tree(struct cache_tree *it, const char *pfx)
{
int i;
if (!it)
return;
if (it->entry_count < 0)
printf("%-40s %s\n", "invalid", pfx);
else
Expand All @@ -24,9 +23,8 @@ static void dump_cache_tree(struct cache_tree *it, const char *pfx)

int main(int ac, char **av)
{
if (read_cache_1(active_cache_sha1) < 0)
if (read_cache() < 0)
die("unable to read index file");
active_cache_tree = read_cache_tree(active_cache_sha1);
dump_cache_tree(active_cache_tree, "");
return 0;
}
Loading

0 comments on commit bad68ec

Please sign in to comment.