summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2022-09-21 15:27:01 -0700
committerJunio C Hamano <gitster@pobox.com>2022-09-21 15:27:01 -0700
commit86c108a8a2c66ee38ab92a35c9acc3206377e264 (patch)
treec61fdf7d94c473a0ef5fadafda66713fe2e01e18
parent370d3a06a3ecb6950e10158c77ac32eb01ae0a88 (diff)
parentd956fa8082e1f8fb0fb26493113c1b98fee19fe2 (diff)
Merge branch 'vd/scalar-generalize-diagnose'
Portability fix. * vd/scalar-generalize-diagnose: builtin/diagnose.c: don't translate the two mode values diagnose.c: refactor to safely use 'd_type'
-rw-r--r--builtin/diagnose.c2
-rw-r--r--diagnose.c70
-rwxr-xr-xt/t0092-diagnose.sh12
3 files changed, 69 insertions, 15 deletions
diff --git a/builtin/diagnose.c b/builtin/diagnose.c
index cd260c2015..576e0e8e38 100644
--- a/builtin/diagnose.c
+++ b/builtin/diagnose.c
@@ -22,7 +22,7 @@ int cmd_diagnose(int argc, const char **argv, const char *prefix)
N_("specify a destination for the diagnostics archive")),
OPT_STRING('s', "suffix", &option_suffix, N_("format"),
N_("specify a strftime format suffix for the filename")),
- OPT_CALLBACK_F(0, "mode", &mode, N_("(stats|all)"),
+ OPT_CALLBACK_F(0, "mode", &mode, "(stats|all)",
N_("specify the content of the diagnostic archive"),
PARSE_OPT_NONEG, option_parse_diagnose),
OPT_END()
diff --git a/diagnose.c b/diagnose.c
index beb0a8741b..8f26569896 100644
--- a/diagnose.c
+++ b/diagnose.c
@@ -66,17 +66,53 @@ static int dir_file_stats(struct object_directory *object_dir, void *data)
return 0;
}
-static int count_files(char *path)
+/*
+ * Get the d_type of a dirent. If the d_type is unknown, derive it from
+ * stat.st_mode.
+ *
+ * Note that 'path' is assumed to have a trailing slash. It is also modified
+ * in-place during the execution of the function, but is then reverted to its
+ * original value before returning.
+ */
+static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
{
- DIR *dir = opendir(path);
+ struct stat st;
+ unsigned char dtype = DTYPE(e);
+ size_t base_path_len;
+
+ if (dtype != DT_UNKNOWN)
+ return dtype;
+
+ /* d_type unknown in dirent, try to fall back on lstat results */
+ base_path_len = path->len;
+ strbuf_addstr(path, e->d_name);
+ if (lstat(path->buf, &st))
+ goto cleanup;
+
+ /* determine d_type from st_mode */
+ if (S_ISREG(st.st_mode))
+ dtype = DT_REG;
+ else if (S_ISDIR(st.st_mode))
+ dtype = DT_DIR;
+ else if (S_ISLNK(st.st_mode))
+ dtype = DT_LNK;
+
+cleanup:
+ strbuf_setlen(path, base_path_len);
+ return dtype;
+}
+
+static int count_files(struct strbuf *path)
+{
+ DIR *dir = opendir(path->buf);
struct dirent *e;
int count = 0;
if (!dir)
return 0;
- while ((e = readdir(dir)) != NULL)
- if (!is_dot_or_dotdot(e->d_name) && e->d_type == DT_REG)
+ while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
+ if (get_dtype(e, path) == DT_REG)
count++;
closedir(dir);
@@ -104,13 +140,13 @@ static void loose_objs_stats(struct strbuf *buf, const char *path)
strbuf_addch(&count_path, '/');
base_path_len = count_path.len;
- while ((e = readdir(dir)) != NULL)
- if (!is_dot_or_dotdot(e->d_name) &&
- e->d_type == DT_DIR && strlen(e->d_name) == 2 &&
+ while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
+ if (get_dtype(e, &count_path) == DT_DIR &&
+ strlen(e->d_name) == 2 &&
!hex_to_bytes(&c, e->d_name, 1)) {
strbuf_setlen(&count_path, base_path_len);
- strbuf_addstr(&count_path, e->d_name);
- total += (count = count_files(count_path.buf));
+ strbuf_addf(&count_path, "%s/", e->d_name);
+ total += (count = count_files(&count_path));
strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
}
@@ -144,22 +180,28 @@ static int add_directory_to_archiver(struct strvec *archiver_args,
len = buf.len;
strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
- while (!res && (e = readdir(dir))) {
- if (!strcmp(".", e->d_name) || !strcmp("..", e->d_name))
- continue;
+ while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
+ struct strbuf abspath = STRBUF_INIT;
+ unsigned char dtype;
+
+ strbuf_add_absolute_path(&abspath, at_root ? "." : path);
+ strbuf_addch(&abspath, '/');
+ dtype = get_dtype(e, &abspath);
strbuf_setlen(&buf, len);
strbuf_addstr(&buf, e->d_name);
- if (e->d_type == DT_REG)
+ if (dtype == DT_REG)
strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
- else if (e->d_type != DT_DIR)
+ else if (dtype != DT_DIR)
warning(_("skipping '%s', which is neither file nor "
"directory"), buf.buf);
else if (recurse &&
add_directory_to_archiver(archiver_args,
buf.buf, recurse) < 0)
res = -1;
+
+ strbuf_release(&abspath);
}
closedir(dir);
diff --git a/t/t0092-diagnose.sh b/t/t0092-diagnose.sh
index fca9b58489..133e5747d6 100755
--- a/t/t0092-diagnose.sh
+++ b/t/t0092-diagnose.sh
@@ -28,12 +28,23 @@ test_expect_success UNZIP 'creates diagnostics zip archive' '
! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
'
+test_expect_success UNZIP 'counts loose objects' '
+ test_commit A &&
+
+ # After committing, should have non-zero loose objects
+ git diagnose -o test-count -s 1 >out &&
+ zip_path=test-count/git-diagnostics-1.zip &&
+ "$GIT_UNZIP" -p "$zip_path" objects-local.txt >out &&
+ grep "^Total: [1-9][0-9]* loose objects" out
+'
+
test_expect_success UNZIP '--mode=stats excludes .git dir contents' '
test_when_finished rm -rf report &&
git diagnose -o report -s test --mode=stats >out &&
# Includes pack quantity/size info
+ zip_path=report/git-diagnostics-test.zip &&
"$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
grep ".git/objects" out &&
@@ -47,6 +58,7 @@ test_expect_success UNZIP '--mode=all includes .git dir contents' '
git diagnose -o report -s test --mode=all >out &&
# Includes pack quantity/size info
+ zip_path=report/git-diagnostics-test.zip &&
"$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
grep ".git/objects" out &&