Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
| 3 | #include "object.h" |
| 4 | #include "commit.h" |
Junio C Hamano | b614e3d | 2005-07-28 14:33:17 -0700 | [diff] [blame] | 5 | #include "tag.h" |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 6 | |
| 7 | /* refs */ |
| 8 | static FILE *info_ref_fp; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 9 | |
| 10 | static int add_info_ref(const char *path, const unsigned char *sha1) |
| 11 | { |
Junio C Hamano | f6b42a8 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 12 | struct object *o = parse_object(sha1); |
| 13 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 14 | fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path); |
Junio C Hamano | f6b42a8 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 15 | if (o->type == tag_type) { |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 16 | o = deref_tag(o, path, 0); |
| 17 | if (o) |
| 18 | fprintf(info_ref_fp, "%s %s^{}\n", |
| 19 | sha1_to_hex(o->sha1), path); |
Junio C Hamano | f6b42a8 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 20 | } |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | static int update_info_refs(int force) |
| 25 | { |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 26 | char *path0 = strdup(git_path("info/refs")); |
| 27 | int len = strlen(path0); |
| 28 | char *path1 = xmalloc(len + 2); |
| 29 | |
| 30 | strcpy(path1, path0); |
| 31 | strcpy(path1 + len, "+"); |
| 32 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 33 | safe_create_leading_directories(path0); |
| 34 | info_ref_fp = fopen(path1, "w"); |
| 35 | if (!info_ref_fp) |
| 36 | return error("unable to update %s", path0); |
| 37 | for_each_ref(add_info_ref); |
| 38 | fclose(info_ref_fp); |
| 39 | rename(path1, path0); |
| 40 | free(path0); |
| 41 | free(path1); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /* packs */ |
Linus Torvalds | 4d8fa91 | 2005-08-01 12:11:53 -0700 | [diff] [blame] | 46 | static struct pack_info { |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 47 | struct packed_git *p; |
| 48 | int old_num; |
| 49 | int new_num; |
| 50 | int nr_alloc; |
| 51 | int nr_heads; |
| 52 | unsigned char (*head)[20]; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 53 | } **info; |
| 54 | static int num_pack; |
| 55 | static const char *objdir; |
| 56 | static int objdirlen; |
| 57 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 58 | static struct pack_info *find_pack_by_name(const char *name) |
| 59 | { |
| 60 | int i; |
| 61 | for (i = 0; i < num_pack; i++) { |
| 62 | struct packed_git *p = info[i]->p; |
| 63 | /* skip "/pack/" after ".git/objects" */ |
| 64 | if (!strcmp(p->pack_name + objdirlen + 6, name)) |
| 65 | return info[i]; |
| 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 70 | /* Returns non-zero when we detect that the info in the |
| 71 | * old file is useless. |
| 72 | */ |
| 73 | static int parse_pack_def(const char *line, int old_cnt) |
| 74 | { |
| 75 | struct pack_info *i = find_pack_by_name(line + 2); |
| 76 | if (i) { |
| 77 | i->old_num = old_cnt; |
| 78 | return 0; |
| 79 | } |
| 80 | else { |
Junio C Hamano | 6f42f89 | 2005-12-04 22:52:19 -0800 | [diff] [blame] | 81 | /* The file describes a pack that is no longer here */ |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 82 | return 1; |
| 83 | } |
| 84 | } |
| 85 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 86 | /* Returns non-zero when we detect that the info in the |
| 87 | * old file is useless. |
| 88 | */ |
| 89 | static int read_pack_info_file(const char *infofile) |
| 90 | { |
| 91 | FILE *fp; |
| 92 | char line[1000]; |
| 93 | int old_cnt = 0; |
| 94 | |
| 95 | fp = fopen(infofile, "r"); |
| 96 | if (!fp) |
| 97 | return 1; /* nonexisting is not an error. */ |
| 98 | |
| 99 | while (fgets(line, sizeof(line), fp)) { |
| 100 | int len = strlen(line); |
| 101 | if (line[len-1] == '\n') |
Junio C Hamano | 8ac4838 | 2005-12-21 13:48:47 -0800 | [diff] [blame] | 102 | line[--len] = 0; |
| 103 | |
| 104 | if (!len) |
| 105 | continue; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 106 | |
| 107 | switch (line[0]) { |
| 108 | case 'P': /* P name */ |
| 109 | if (parse_pack_def(line, old_cnt++)) |
| 110 | goto out_stale; |
| 111 | break; |
Junio C Hamano | 6f42f89 | 2005-12-04 22:52:19 -0800 | [diff] [blame] | 112 | case 'D': /* we used to emit D but that was misguided. */ |
| 113 | goto out_stale; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 114 | break; |
Junio C Hamano | 3e15c67 | 2005-12-04 23:12:36 -0800 | [diff] [blame] | 115 | case 'T': /* we used to emit T but nobody uses it. */ |
| 116 | goto out_stale; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | error("unrecognized: %s", line); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | fclose(fp); |
| 124 | return 0; |
| 125 | out_stale: |
| 126 | fclose(fp); |
| 127 | return 1; |
| 128 | } |
| 129 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 130 | static int compare_info(const void *a_, const void *b_) |
| 131 | { |
| 132 | struct pack_info * const* a = a_; |
| 133 | struct pack_info * const* b = b_; |
| 134 | |
| 135 | if (0 <= (*a)->old_num && 0 <= (*b)->old_num) |
| 136 | /* Keep the order in the original */ |
| 137 | return (*a)->old_num - (*b)->old_num; |
| 138 | else if (0 <= (*a)->old_num) |
| 139 | /* Only A existed in the original so B is obviously newer */ |
| 140 | return -1; |
| 141 | else if (0 <= (*b)->old_num) |
| 142 | /* The other way around. */ |
| 143 | return 1; |
| 144 | |
Junio C Hamano | d5eac49 | 2005-12-04 23:02:54 -0800 | [diff] [blame] | 145 | /* then it does not matter but at least keep the comparison stable */ |
Junio C Hamano | 2dee581 | 2005-12-08 17:29:11 -0800 | [diff] [blame] | 146 | if ((*a)->p == (*b)->p) |
| 147 | return 0; |
| 148 | else if ((*a)->p < (*b)->p) |
| 149 | return -1; |
| 150 | else |
| 151 | return 1; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void init_pack_info(const char *infofile, int force) |
| 155 | { |
| 156 | struct packed_git *p; |
| 157 | int stale; |
| 158 | int i = 0; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 159 | |
| 160 | objdir = get_object_directory(); |
| 161 | objdirlen = strlen(objdir); |
| 162 | |
| 163 | prepare_packed_git(); |
| 164 | for (p = packed_git; p; p = p->next) { |
| 165 | /* we ignore things on alternate path since they are |
| 166 | * not available to the pullers in general. |
| 167 | */ |
Junio C Hamano | f13d7db | 2005-12-05 10:39:17 -0800 | [diff] [blame] | 168 | if (!p->pack_local) |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 169 | continue; |
| 170 | i++; |
| 171 | } |
| 172 | num_pack = i; |
| 173 | info = xcalloc(num_pack, sizeof(struct pack_info *)); |
| 174 | for (i = 0, p = packed_git; p; p = p->next) { |
Junio C Hamano | f13d7db | 2005-12-05 10:39:17 -0800 | [diff] [blame] | 175 | if (!p->pack_local) |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 176 | continue; |
Junio C Hamano | 6f42f89 | 2005-12-04 22:52:19 -0800 | [diff] [blame] | 177 | info[i] = xcalloc(1, sizeof(struct pack_info)); |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 178 | info[i]->p = p; |
| 179 | info[i]->old_num = -1; |
| 180 | i++; |
| 181 | } |
| 182 | |
| 183 | if (infofile && !force) |
| 184 | stale = read_pack_info_file(infofile); |
| 185 | else |
| 186 | stale = 1; |
| 187 | |
| 188 | for (i = 0; i < num_pack; i++) { |
| 189 | if (stale) { |
| 190 | info[i]->old_num = -1; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 191 | info[i]->nr_heads = 0; |
| 192 | } |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Junio C Hamano | 6f42f89 | 2005-12-04 22:52:19 -0800 | [diff] [blame] | 195 | /* renumber them */ |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 196 | qsort(info, num_pack, sizeof(info[0]), compare_info); |
| 197 | for (i = 0; i < num_pack; i++) |
| 198 | info[i]->new_num = i; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void write_pack_info_file(FILE *fp) |
| 202 | { |
Junio C Hamano | 3e15c67 | 2005-12-04 23:12:36 -0800 | [diff] [blame] | 203 | int i; |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 204 | for (i = 0; i < num_pack; i++) |
| 205 | fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6); |
Junio C Hamano | 21b1ace | 2005-12-21 12:09:17 -0800 | [diff] [blame] | 206 | fputc('\n', fp); |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int update_info_packs(int force) |
| 210 | { |
| 211 | char infofile[PATH_MAX]; |
| 212 | char name[PATH_MAX]; |
| 213 | int namelen; |
| 214 | FILE *fp; |
| 215 | |
| 216 | namelen = sprintf(infofile, "%s/info/packs", get_object_directory()); |
| 217 | strcpy(name, infofile); |
| 218 | strcpy(name + namelen, "+"); |
| 219 | |
| 220 | init_pack_info(infofile, force); |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 221 | |
| 222 | safe_create_leading_directories(name); |
| 223 | fp = fopen(name, "w"); |
| 224 | if (!fp) |
| 225 | return error("cannot open %s", name); |
| 226 | write_pack_info_file(fp); |
| 227 | fclose(fp); |
| 228 | rename(name, infofile); |
| 229 | return 0; |
| 230 | } |
| 231 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 232 | /* public */ |
| 233 | int update_server_info(int force) |
| 234 | { |
| 235 | /* We would add more dumb-server support files later, |
| 236 | * including index of available pack files and their |
| 237 | * intended audiences. |
| 238 | */ |
| 239 | int errs = 0; |
| 240 | |
| 241 | errs = errs | update_info_refs(force); |
| 242 | errs = errs | update_info_packs(force); |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 243 | |
Junio C Hamano | 6f42f89 | 2005-12-04 22:52:19 -0800 | [diff] [blame] | 244 | /* remove leftover rev-cache file if there is any */ |
| 245 | unlink(git_path("info/rev-cache")); |
| 246 | |
Junio C Hamano | 8f3f9b0 | 2005-07-23 17:54:41 -0700 | [diff] [blame] | 247 | return errs; |
| 248 | } |