Skip to content

Commit 0ed5b13

Browse files
dschogitster
authored andcommitted
scalar diagnose: include disk space information
When analyzing problems with large worktrees/repositories, it is useful to know how close to a "full disk" situation Scalar/Git operates. Let's include this information. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa5c79a commit 0ed5b13

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

contrib/scalar/scalar.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,58 @@ static int add_directory_to_archiver(struct strvec *archiver_args,
302302
return res;
303303
}
304304

305+
#ifndef WIN32
306+
#include <sys/statvfs.h>
307+
#endif
308+
309+
static int get_disk_info(struct strbuf *out)
310+
{
311+
#ifdef WIN32
312+
struct strbuf buf = STRBUF_INIT;
313+
char volume_name[MAX_PATH], fs_name[MAX_PATH];
314+
DWORD serial_number, component_length, flags;
315+
ULARGE_INTEGER avail2caller, total, avail;
316+
317+
strbuf_realpath(&buf, ".", 1);
318+
if (!GetDiskFreeSpaceExA(buf.buf, &avail2caller, &total, &avail)) {
319+
error(_("could not determine free disk size for '%s'"),
320+
buf.buf);
321+
strbuf_release(&buf);
322+
return -1;
323+
}
324+
325+
strbuf_setlen(&buf, offset_1st_component(buf.buf));
326+
if (!GetVolumeInformationA(buf.buf, volume_name, sizeof(volume_name),
327+
&serial_number, &component_length, &flags,
328+
fs_name, sizeof(fs_name))) {
329+
error(_("could not get info for '%s'"), buf.buf);
330+
strbuf_release(&buf);
331+
return -1;
332+
}
333+
strbuf_addf(out, "Available space on '%s': ", buf.buf);
334+
strbuf_humanise_bytes(out, avail2caller.QuadPart);
335+
strbuf_addch(out, '\n');
336+
strbuf_release(&buf);
337+
#else
338+
struct strbuf buf = STRBUF_INIT;
339+
struct statvfs stat;
340+
341+
strbuf_realpath(&buf, ".", 1);
342+
if (statvfs(buf.buf, &stat) < 0) {
343+
error_errno(_("could not determine free disk size for '%s'"),
344+
buf.buf);
345+
strbuf_release(&buf);
346+
return -1;
347+
}
348+
349+
strbuf_addf(out, "Available space on '%s': ", buf.buf);
350+
strbuf_humanise_bytes(out, st_mult(stat.f_bsize, stat.f_bavail));
351+
strbuf_addf(out, " (mount flags 0x%lx)\n", stat.f_flag);
352+
strbuf_release(&buf);
353+
#endif
354+
return 0;
355+
}
356+
305357
/* printf-style interface, expects `<key>=<value>` argument */
306358
static int set_config(const char *fmt, ...)
307359
{
@@ -598,6 +650,7 @@ static int cmd_diagnose(int argc, const char **argv)
598650
get_version_info(&buf, 1);
599651

600652
strbuf_addf(&buf, "Enlistment root: %s\n", the_repository->worktree);
653+
get_disk_info(&buf);
601654
write_or_die(stdout_fd, buf.buf, buf.len);
602655
strvec_pushf(&archiver_args,
603656
"--add-virtual-file=diagnostics.log:%.*s",

contrib/scalar/t/t9099-scalar.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ SQ="'"
102102
test_expect_success UNZIP 'scalar diagnose' '
103103
scalar clone "file://$(pwd)" cloned --single-branch &&
104104
scalar diagnose cloned >out 2>err &&
105+
grep "Available space" out &&
105106
sed -n "s/.*$SQ\\(.*\\.zip\\)$SQ.*/\\1/p" <err >zip_path &&
106107
zip_path=$(cat zip_path) &&
107108
test -n "$zip_path" &&

0 commit comments

Comments
 (0)