10 static int add_file(const char *name, const char *path);
11 static void calc_chksum(FILE *fp, uint32_t *sum);
12 static int proc_tree(git_repository *repo, git_tree *tree, const char *path);
14 static struct repo_file *files;
15 static int num_files, max_files;
16 static const char *repo_path;
18 int repo_init(const char *path)
21 git_repository *repo = 0;
26 if(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_NO_SEARCH, 0) != 0) {
27 fprintf(stderr, "repo_init: failed to open repository: %s: %s\n", path,
28 git_error_last()->message);
33 num_files = max_files = 0;
36 if(git_revparse_single(&tree, repo, "HEAD^{tree}") != 0) {
37 fprintf(stderr, "repo_init: %s\n", git_error_last()->message);
40 if(proc_tree(repo, (git_tree*)tree, 0) == -1) {
43 git_object_free(tree);
50 num_files = max_files = 0;
52 if(repo) git_repository_free(repo);
53 git_libgit2_shutdown();
57 void repo_cleanup(void)
60 for(i=0; i<num_files; i++) {
61 free(files[i].fullpath);
65 num_files = max_files = 0;
68 int repo_num_files(void)
73 struct repo_file *repo_file(int idx)
75 if(idx < 0 || idx >= num_files) return 0;
79 static int add_file(const char *name, const char *path)
81 struct repo_file *file;
86 if(num_files >= max_files) {
87 int newmax = max_files ? max_files * 2 : 16;
88 void *newfiles = realloc(files, newmax * sizeof *files);
90 fprintf(stderr, "repo_init: failed to add file\n");
97 pathlen = strlen(repo_path) + strlen(name) + 1;
99 pathlen += strlen(path) + 1;
102 file = files + num_files;
103 if(!(file->fullpath = malloc(pathlen + 1))) {
104 fprintf(stderr, "repo_init: failed to allocate file path buffer\n");
108 sprintf(file->fullpath, "%s/%s/%s", repo_path, path, name);
110 sprintf(file->fullpath, "%s/%s", repo_path, name);
112 file->path = file->fullpath + strlen(repo_path) + 1;
114 /* attempt to open the file */
115 if(!(fp = fopen(file->fullpath, "rb"))) {
116 fprintf(stderr, "repo_init: failed to open file: %s: %s\n", file->fullpath, strerror(errno));
117 free(file->fullpath);
121 /* get size & modification time */
122 fstat(fileno(fp), &st);
123 file->size = st.st_size;
124 file->mtime = st.st_mtime;
126 /* calculate md5 checksum */
127 calc_chksum(fp, file->chksum);
135 static void calc_chksum(FILE *fp, uint32_t *sum)
137 static char buf[4096];
142 while((sz = fread(buf, 1, sizeof buf, fp)) > 0) {
143 md5_msg(&md, buf, sz);
147 memcpy(sum, md.sum, sizeof md.sum);
150 static int proc_tree(git_repository *repo, git_tree *tree, const char *path)
152 int i, count = git_tree_entrycount(tree);
153 const git_tree_entry *ent;
158 for(i=0; i<count; i++) {
159 ent = git_tree_entry_byindex(tree, i);
160 type = git_tree_entry_type(ent);
161 name = git_tree_entry_name(ent);
164 case GIT_OBJECT_BLOB:
165 if(add_file(name, path) == -1) {
170 case GIT_OBJECT_TREE:
171 git_tree_entry_to_object(&obj, repo, ent);
173 char *pathbuf = malloc(strlen(path) + strlen(name) + 2);
175 fprintf(stderr, "repo_init: failed to allocate memory\n");
178 sprintf(pathbuf, "%s/%s", path, name);
179 proc_tree(repo, (git_tree*)obj, pathbuf);
182 proc_tree(repo, (git_tree*)obj, name);
184 git_object_free(obj);