3 #include "cache-tree.h"
5 typedef struct _entry
{
10 struct cache_entry
*ce
;
13 static Hash_entry
*table
[4096];
17 static int name_hash(char *name
)
19 uintptr_t l
= (uintptr_t)name
;
28 static char *convert(char *name
)
30 static char path
[PATH_MAX
+ 1];
32 const int attic_len
= strlen("Attic");
33 size_t len
= strlen(name
);
35 if (len
>= PATH_MAX
+ strip
)
39 memcpy(path
, name
+ strip
, len
+ 1);
42 p
= strrchr(path
, '/');
44 if (len
> 2 && end
[-2] == ',' && end
[-1] == 'v') {
49 if (!p
|| p
< path
+ attic_len
)
54 if (memcmp(q
, "Attic", attic_len
) != 0)
57 if (q
== path
|| q
[-1] == '/') {
58 while ((*q
++ = *p
++) != '\0')
64 static Hash_entry
*find_node(rev_commit
*c
)
66 char *name
= c
->file
? c
->file
->name
: NULL
;
75 hash
= name_hash(name
);
76 for (entry
= table
[hash
]; entry
; entry
= entry
->next
)
77 if (entry
->cvs_name
== name
)
80 real_name
= convert(name
);
84 entry
= xcalloc(1, sizeof(Hash_entry
));
85 entry
->cvs_name
= name
;
87 len
= strlen(real_name
);
90 entry
->name
= xmalloc(len
+ 1);
91 memcpy(entry
->name
, real_name
, len
+ 1);
93 entry
->ce
= xcalloc(1, cache_entry_size(len
));
94 memcpy(entry
->ce
->name
, real_name
, len
);
95 entry
->ce
->ce_flags
= create_ce_flags(len
, 0);
97 entry
->next
= table
[hash
];
102 static int cache_broken
;
104 static void delete_file(Hash_entry
*entry
)
106 remove_file_from_cache(entry
->name
);
107 cache_tree_invalidate_path(active_cache_tree
, entry
->name
);
110 static int set_file(Hash_entry
*entry
, rev_file
*file
)
112 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
114 if (get_sha1_hex(file
->sha1
, entry
->ce
->sha1
))
115 die("corrupt sha1: %s\n", file
->sha1
);
117 entry
->ce
->ce_mode
= create_ce_mode(file
->mode
);
118 if (add_cache_entry(entry
->ce
, options
))
119 return error("can't add %s\n", entry
->name
);
121 cache_tree_invalidate_path(active_cache_tree
, entry
->name
);
126 void delete_commit(rev_commit
*c
)
128 Hash_entry
*entry
= find_node(c
);
129 if (entry
&& !cache_broken
)
133 void set_commit(rev_commit
*c
)
136 Hash_entry
*entry
= find_node(c
);
138 cache_broken
= set_file(entry
, c
->file
);
142 void reset_commits(rev_commit
**commits
, int ncommits
)
145 active_cache_tree
= cache_tree();
147 while (ncommits
-- && !cache_broken
) {
148 rev_commit
*c
= *commits
++;
150 Hash_entry
*entry
= find_node(c
);
152 cache_broken
= set_file(entry
, c
->file
);
157 rev_commit
*create_tree(rev_commit
*leader
)
159 rev_commit
*commit
= xcalloc(1, sizeof (rev_commit
));
161 commit
->date
= leader
->date
;
162 commit
->commitid
= leader
->commitid
;
163 commit
->log
= leader
->log
;
164 commit
->author
= leader
->author
;
167 if (cache_tree_update(active_cache_tree
, active_cache
,
169 cache_broken
= error("writing tree");
173 commit
->sha1
= atom(sha1_to_hex(active_cache_tree
->sha1
));
178 void init_tree(int n
)
180 git_config(git_default_config
, NULL
);
184 void discard_tree(void)
188 for (i
= 0; i
< 4096; i
++) {
189 Hash_entry
*entry
= table
[i
];
191 Hash_entry
*next
= entry
->next
;