8 * Create the file "path" by writing to a temporary file and renaming
9 * it into place. The contents of the file come from "generate", which
10 * should return non-zero if it encounters an error.
12 static int update_info_file(char *path
, int (*generate
)(FILE *))
14 char *tmp
= mkpathdup("%s_XXXXXX", path
);
19 safe_create_leading_directories(path
);
31 if (adjust_shared_perm(tmp
) < 0)
33 if (rename(tmp
, path
) < 0)
39 error("unable to update %s: %s", path
, strerror(errno
));
50 static int add_info_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
53 struct object
*o
= parse_object(sha1
);
57 if (fprintf(fp
, "%s %s\n", sha1_to_hex(sha1
), path
) < 0)
60 if (o
->type
== OBJ_TAG
) {
61 o
= deref_tag(o
, path
, 0);
63 if (fprintf(fp
, "%s %s^{}\n",
64 sha1_to_hex(o
->sha1
), path
) < 0)
70 static int generate_info_refs(FILE *fp
)
72 return for_each_ref(add_info_ref
, fp
);
75 static int update_info_refs(int force
)
77 char *path
= git_pathdup("info/refs");
78 int ret
= update_info_file(path
, generate_info_refs
);
84 static struct pack_info
{
90 unsigned char (*head
)[20];
93 static const char *objdir
;
96 static struct pack_info
*find_pack_by_name(const char *name
)
99 for (i
= 0; i
< num_pack
; i
++) {
100 struct packed_git
*p
= info
[i
]->p
;
101 /* skip "/pack/" after ".git/objects" */
102 if (!strcmp(p
->pack_name
+ objdirlen
+ 6, name
))
108 /* Returns non-zero when we detect that the info in the
109 * old file is useless.
111 static int parse_pack_def(const char *line
, int old_cnt
)
113 struct pack_info
*i
= find_pack_by_name(line
+ 2);
115 i
->old_num
= old_cnt
;
119 /* The file describes a pack that is no longer here */
124 /* Returns non-zero when we detect that the info in the
125 * old file is useless.
127 static int read_pack_info_file(const char *infofile
)
133 fp
= fopen(infofile
, "r");
135 return 1; /* nonexistent is not an error. */
137 while (fgets(line
, sizeof(line
), fp
)) {
138 int len
= strlen(line
);
139 if (len
&& line
[len
-1] == '\n')
146 case 'P': /* P name */
147 if (parse_pack_def(line
, old_cnt
++))
150 case 'D': /* we used to emit D but that was misguided. */
151 case 'T': /* we used to emit T but nobody uses it. */
154 error("unrecognized: %s", line
);
165 static int compare_info(const void *a_
, const void *b_
)
167 struct pack_info
*const *a
= a_
;
168 struct pack_info
*const *b
= b_
;
170 if (0 <= (*a
)->old_num
&& 0 <= (*b
)->old_num
)
171 /* Keep the order in the original */
172 return (*a
)->old_num
- (*b
)->old_num
;
173 else if (0 <= (*a
)->old_num
)
174 /* Only A existed in the original so B is obviously newer */
176 else if (0 <= (*b
)->old_num
)
177 /* The other way around. */
180 /* then it does not matter but at least keep the comparison stable */
181 if ((*a
)->p
== (*b
)->p
)
183 else if ((*a
)->p
< (*b
)->p
)
189 static void init_pack_info(const char *infofile
, int force
)
191 struct packed_git
*p
;
195 objdir
= get_object_directory();
196 objdirlen
= strlen(objdir
);
198 prepare_packed_git();
199 for (p
= packed_git
; p
; p
= p
->next
) {
200 /* we ignore things on alternate path since they are
201 * not available to the pullers in general.
208 info
= xcalloc(num_pack
, sizeof(struct pack_info
*));
209 for (i
= 0, p
= packed_git
; p
; p
= p
->next
) {
212 info
[i
] = xcalloc(1, sizeof(struct pack_info
));
214 info
[i
]->old_num
= -1;
218 if (infofile
&& !force
)
219 stale
= read_pack_info_file(infofile
);
223 for (i
= 0; i
< num_pack
; i
++) {
225 info
[i
]->old_num
= -1;
226 info
[i
]->nr_heads
= 0;
231 qsort(info
, num_pack
, sizeof(info
[0]), compare_info
);
232 for (i
= 0; i
< num_pack
; i
++)
233 info
[i
]->new_num
= i
;
236 static void free_pack_info(void)
239 for (i
= 0; i
< num_pack
; i
++)
244 static int write_pack_info_file(FILE *fp
)
247 for (i
= 0; i
< num_pack
; i
++) {
248 if (fprintf(fp
, "P %s\n", info
[i
]->p
->pack_name
+ objdirlen
+ 6) < 0)
251 if (fputc('\n', fp
) == EOF
)
256 static int update_info_packs(int force
)
258 char *infofile
= mkpathdup("%s/info/packs", get_object_directory());
261 init_pack_info(infofile
, force
);
262 ret
= update_info_file(infofile
, write_pack_info_file
);
269 int update_server_info(int force
)
271 /* We would add more dumb-server support files later,
272 * including index of available pack files and their
273 * intended audiences.
277 errs
= errs
| update_info_refs(force
);
278 errs
= errs
| update_info_packs(force
);
280 /* remove leftover rev-cache file if there is any */
281 unlink_or_warn(git_path("info/rev-cache"));