9 * Create the file "path" by writing to a temporary file and renaming
10 * it into place. The contents of the file come from "generate", which
11 * should return non-zero if it encounters an error.
13 static int update_info_file(char *path
, int (*generate
)(FILE *))
15 char *tmp
= mkpathdup("%s_XXXXXX", path
);
18 FILE *fp
= NULL
, *to_close
;
20 safe_create_leading_directories(path
);
21 fd
= git_mkstemp_mode(tmp
, 0666);
24 to_close
= fp
= fdopen(fd
, "w");
34 if (adjust_shared_perm(tmp
) < 0)
36 if (rename(tmp
, path
) < 0)
42 error_errno("unable to update %s", path
);
53 static int add_info_ref(const char *path
, const struct object_id
*oid
,
54 int flag
, void *cb_data
)
57 struct object
*o
= parse_object(oid
);
61 if (fprintf(fp
, "%s %s\n", oid_to_hex(oid
), path
) < 0)
64 if (o
->type
== OBJ_TAG
) {
65 o
= deref_tag(o
, path
, 0);
67 if (fprintf(fp
, "%s %s^{}\n",
68 oid_to_hex(&o
->oid
), path
) < 0)
74 static int generate_info_refs(FILE *fp
)
76 return for_each_ref(add_info_ref
, fp
);
79 static int update_info_refs(int force
)
81 char *path
= git_pathdup("info/refs");
82 int ret
= update_info_file(path
, generate_info_refs
);
88 static struct pack_info
{
94 unsigned char (*head
)[20];
97 static const char *objdir
;
100 static struct pack_info
*find_pack_by_name(const char *name
)
103 for (i
= 0; i
< num_pack
; i
++) {
104 struct packed_git
*p
= info
[i
]->p
;
105 /* skip "/pack/" after ".git/objects" */
106 if (!strcmp(p
->pack_name
+ objdirlen
+ 6, name
))
112 /* Returns non-zero when we detect that the info in the
113 * old file is useless.
115 static int parse_pack_def(const char *line
, int old_cnt
)
117 struct pack_info
*i
= find_pack_by_name(line
+ 2);
119 i
->old_num
= old_cnt
;
123 /* The file describes a pack that is no longer here */
128 /* Returns non-zero when we detect that the info in the
129 * old file is useless.
131 static int read_pack_info_file(const char *infofile
)
137 fp
= fopen_or_warn(infofile
, "r");
139 return 1; /* nonexistent is not an error. */
141 while (fgets(line
, sizeof(line
), fp
)) {
142 int len
= strlen(line
);
143 if (len
&& line
[len
-1] == '\n')
150 case 'P': /* P name */
151 if (parse_pack_def(line
, old_cnt
++))
154 case 'D': /* we used to emit D but that was misguided. */
155 case 'T': /* we used to emit T but nobody uses it. */
158 error("unrecognized: %s", line
);
169 static int compare_info(const void *a_
, const void *b_
)
171 struct pack_info
*const *a
= a_
;
172 struct pack_info
*const *b
= b_
;
174 if (0 <= (*a
)->old_num
&& 0 <= (*b
)->old_num
)
175 /* Keep the order in the original */
176 return (*a
)->old_num
- (*b
)->old_num
;
177 else if (0 <= (*a
)->old_num
)
178 /* Only A existed in the original so B is obviously newer */
180 else if (0 <= (*b
)->old_num
)
181 /* The other way around. */
184 /* then it does not matter but at least keep the comparison stable */
185 if ((*a
)->p
== (*b
)->p
)
187 else if ((*a
)->p
< (*b
)->p
)
193 static void init_pack_info(const char *infofile
, int force
)
195 struct packed_git
*p
;
199 objdir
= get_object_directory();
200 objdirlen
= strlen(objdir
);
202 prepare_packed_git();
203 for (p
= packed_git
; p
; p
= p
->next
) {
204 /* we ignore things on alternate path since they are
205 * not available to the pullers in general.
212 info
= xcalloc(num_pack
, sizeof(struct pack_info
*));
213 for (i
= 0, p
= packed_git
; p
; p
= p
->next
) {
216 info
[i
] = xcalloc(1, sizeof(struct pack_info
));
218 info
[i
]->old_num
= -1;
222 if (infofile
&& !force
)
223 stale
= read_pack_info_file(infofile
);
227 for (i
= 0; i
< num_pack
; i
++) {
229 info
[i
]->old_num
= -1;
230 info
[i
]->nr_heads
= 0;
235 QSORT(info
, num_pack
, compare_info
);
236 for (i
= 0; i
< num_pack
; i
++)
237 info
[i
]->new_num
= i
;
240 static void free_pack_info(void)
243 for (i
= 0; i
< num_pack
; i
++)
248 static int write_pack_info_file(FILE *fp
)
251 for (i
= 0; i
< num_pack
; i
++) {
252 if (fprintf(fp
, "P %s\n", info
[i
]->p
->pack_name
+ objdirlen
+ 6) < 0)
255 if (fputc('\n', fp
) == EOF
)
260 static int update_info_packs(int force
)
262 char *infofile
= mkpathdup("%s/info/packs", get_object_directory());
265 init_pack_info(infofile
, force
);
266 ret
= update_info_file(infofile
, write_pack_info_file
);
273 int update_server_info(int force
)
275 /* We would add more dumb-server support files later,
276 * including index of available pack files and their
277 * intended audiences.
281 errs
= errs
| update_info_refs(force
);
282 errs
= errs
| update_info_packs(force
);
284 /* remove leftover rev-cache file if there is any */
285 unlink_or_warn(git_path("info/rev-cache"));