Skip to content

Commit 5439144

Browse files
nasamuffingitster
authored andcommitted
bugreport: summarize contents of alternates file
In some cases, it could be that the user is having a problem with an object which isn't present in their normal object directory. We can get a hint that that might be the case by examining the list of alternates where their object may be stored instead. Since paths to alternates may be sensitive, we'll instead count how many alternates have been specified and note how many of them exist or are broken. While object-cache.h describes a function "foreach_alt_odb()", this function does not provide information on broken alternates, which are skipped over in "link_alt_odb_entry()". Since the goal is to identify missing alternates, we can gather the contents of .git/objects/info/alternates manually. Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1cdb5e commit 5439144

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Documentation/git-bugreport.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The following information is captured automatically:
3535
- The number of loose objects in the repository
3636
- The number of packs and packed objects in the repository
3737
- A list of the contents of .git/objects/info (or equivalent)
38+
- The number of valid and invalid alternates
3839

3940
This tool is invoked via the typical Git setup process, which means that in some
4041
cases, it might not be able to launch - for example, if a relevant config file

bugreport.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,48 @@ static void get_object_info_summary(struct strbuf *obj_info, int nongit)
255255
strbuf_release(&dirpath);
256256
}
257257

258+
static void get_alternates_summary(struct strbuf *alternates_info, int nongit)
259+
{
260+
struct strbuf alternates_path = STRBUF_INIT;
261+
struct strbuf alternate = STRBUF_INIT;
262+
FILE *file;
263+
size_t exists = 0, broken = 0;
264+
265+
if (nongit) {
266+
strbuf_addstr(alternates_info,
267+
"not run from a git repository - alternates unavailable\n");
268+
return;
269+
}
270+
271+
strbuf_addstr(&alternates_path, get_object_directory());
272+
strbuf_complete(&alternates_path, '/');
273+
strbuf_addstr(&alternates_path, "info/alternates");
274+
275+
file = fopen(alternates_path.buf, "r");
276+
if (!file) {
277+
strbuf_addstr(alternates_info, "No alternates file found.\n");
278+
strbuf_release(&alternates_path);
279+
return;
280+
}
281+
282+
while (strbuf_getline(&alternate, file) != EOF) {
283+
if (!access(alternate.buf, F_OK))
284+
exists++;
285+
else
286+
broken++;
287+
}
288+
289+
strbuf_addf(alternates_info,
290+
"%zd alternates found (%zd working, %zd broken)\n",
291+
exists + broken,
292+
exists,
293+
broken);
294+
295+
fclose(file);
296+
strbuf_release(&alternate);
297+
strbuf_release(&alternates_path);
298+
}
299+
258300
static const char * const bugreport_usage[] = {
259301
N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"),
260302
NULL
@@ -355,6 +397,9 @@ int cmd_main(int argc, const char **argv)
355397
get_header(&buffer, "Object Info Summary");
356398
get_object_info_summary(&buffer, nongit_ok);
357399

400+
get_header(&buffer, "Alternates");
401+
get_alternates_summary(&buffer, nongit_ok);
402+
358403
report = fopen_for_writing(report_path.buf);
359404

360405
if (report == NULL) {

0 commit comments

Comments
 (0)