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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed May 28, 2022 at 16:11 UTC 0ed5b13f2450fd26c0e52cef775deea477e4cdb4
2 files changed +54
contrib/scalar/scalar.c
+53
@@ -302,6 +302,58 @@ static int add_directory_to_archiver(struct strvec *archiver_args,
302 return res;
303 }
304
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 +
357 /* printf-style interface, expects `<key>=<value>` argument */
358 static int set_config(const char *fmt, ...)
359 {
@@ -598,6 +650,7 @@ static int cmd_diagnose(int argc, const char **argv)
650 get_version_info(&buf, 1);
651
652 strbuf_addf(&buf, "Enlistment root: %s\n", the_repository->worktree);
653 + get_disk_info(&buf);
654 write_or_die(stdout_fd, buf.buf, buf.len);
655 strvec_pushf(&archiver_args,
656 "--add-virtual-file=diagnostics.log:%.*s",
contrib/scalar/t/t9099-scalar.sh
+1
@@ -102,6 +102,7 @@ SQ="'"
102 test_expect_success UNZIP 'scalar diagnose' '
103 scalar clone "file://$(pwd)" cloned --single-branch &&
104 scalar diagnose cloned >out 2>err &&
105 + grep "Available space" out &&
106 sed -n "s/.*$SQ\\(.*\\.zip\\)$SQ.*/\\1/p" <err >zip_path &&
107 zip_path=$(cat zip_path) &&
108 test -n "$zip_path" &&