]> git.ipfire.org Git - thirdparty/git.git/blobdiff - contrib/scalar/scalar.c
scalar: implement `scalar diagnose`
[thirdparty/git.git] / contrib / scalar / scalar.c
index 6d58c7a69805a2e1be7a3c4878fcf1095d987c45..a1e05a21468832208795f9fe73344c4abc49486e 100644 (file)
@@ -11,6 +11,7 @@
 #include "dir.h"
 #include "packfile.h"
 #include "help.h"
+#include "archive.h"
 
 /*
  * Remove the deepest subdirectory in the provided path string. Path must not
@@ -260,6 +261,47 @@ static int unregister_dir(void)
        return res;
 }
 
+static int add_directory_to_archiver(struct strvec *archiver_args,
+                                         const char *path, int recurse)
+{
+       int at_root = !*path;
+       DIR *dir = opendir(at_root ? "." : path);
+       struct dirent *e;
+       struct strbuf buf = STRBUF_INIT;
+       size_t len;
+       int res = 0;
+
+       if (!dir)
+               return error_errno(_("could not open directory '%s'"), path);
+
+       if (!at_root)
+               strbuf_addf(&buf, "%s/", path);
+       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;
+
+               strbuf_setlen(&buf, len);
+               strbuf_addstr(&buf, e->d_name);
+
+               if (e->d_type == DT_REG)
+                       strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
+               else if (e->d_type != 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;
+       }
+
+       closedir(dir);
+       strbuf_release(&buf);
+       return res;
+}
+
 /* printf-style interface, expects `<key>=<value>` argument */
 static int set_config(const char *fmt, ...)
 {
@@ -500,6 +542,107 @@ cleanup:
        return res;
 }
 
+static int cmd_diagnose(int argc, const char **argv)
+{
+       struct option options[] = {
+               OPT_END(),
+       };
+       const char * const usage[] = {
+               N_("scalar diagnose [<enlistment>]"),
+               NULL
+       };
+       struct strbuf zip_path = STRBUF_INIT;
+       struct strvec archiver_args = STRVEC_INIT;
+       char **argv_copy = NULL;
+       int stdout_fd = -1, archiver_fd = -1;
+       time_t now = time(NULL);
+       struct tm tm;
+       struct strbuf path = STRBUF_INIT, buf = STRBUF_INIT;
+       int res = 0;
+
+       argc = parse_options(argc, argv, NULL, options,
+                            usage, 0);
+
+       setup_enlistment_directory(argc, argv, usage, options, &zip_path);
+
+       strbuf_addstr(&zip_path, "/.scalarDiagnostics/scalar_");
+       strbuf_addftime(&zip_path,
+                       "%Y%m%d_%H%M%S", localtime_r(&now, &tm), 0, 0);
+       strbuf_addstr(&zip_path, ".zip");
+       switch (safe_create_leading_directories(zip_path.buf)) {
+       case SCLD_EXISTS:
+       case SCLD_OK:
+               break;
+       default:
+               error_errno(_("could not create directory for '%s'"),
+                           zip_path.buf);
+               goto diagnose_cleanup;
+       }
+       stdout_fd = dup(1);
+       if (stdout_fd < 0) {
+               res = error_errno(_("could not duplicate stdout"));
+               goto diagnose_cleanup;
+       }
+
+       archiver_fd = xopen(zip_path.buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
+       if (archiver_fd < 0 || dup2(archiver_fd, 1) < 0) {
+               res = error_errno(_("could not redirect output"));
+               goto diagnose_cleanup;
+       }
+
+       init_zip_archiver();
+       strvec_pushl(&archiver_args, "scalar-diagnose", "--format=zip", NULL);
+
+       strbuf_reset(&buf);
+       strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
+       get_version_info(&buf, 1);
+
+       strbuf_addf(&buf, "Enlistment root: %s\n", the_repository->worktree);
+       write_or_die(stdout_fd, buf.buf, buf.len);
+       strvec_pushf(&archiver_args,
+                    "--add-virtual-file=diagnostics.log:%.*s",
+                    (int)buf.len, buf.buf);
+
+       if ((res = add_directory_to_archiver(&archiver_args, ".git", 0)) ||
+           (res = add_directory_to_archiver(&archiver_args, ".git/hooks", 0)) ||
+           (res = add_directory_to_archiver(&archiver_args, ".git/info", 0)) ||
+           (res = add_directory_to_archiver(&archiver_args, ".git/logs", 1)) ||
+           (res = add_directory_to_archiver(&archiver_args, ".git/objects/info", 0)))
+               goto diagnose_cleanup;
+
+       strvec_pushl(&archiver_args, "--prefix=",
+                    oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
+
+       /* `write_archive()` modifies the `argv` passed to it. Let it. */
+       argv_copy = xmemdupz(archiver_args.v,
+                            sizeof(char *) * archiver_args.nr);
+       res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
+                           the_repository, NULL, 0);
+       if (res) {
+               error(_("failed to write archive"));
+               goto diagnose_cleanup;
+       }
+
+       if (!res)
+               fprintf(stderr, "\n"
+                      "Diagnostics complete.\n"
+                      "All of the gathered info is captured in '%s'\n",
+                      zip_path.buf);
+
+diagnose_cleanup:
+       if (archiver_fd >= 0) {
+               close(1);
+               dup2(stdout_fd, 1);
+       }
+       free(argv_copy);
+       strvec_clear(&archiver_args);
+       strbuf_release(&zip_path);
+       strbuf_release(&path);
+       strbuf_release(&buf);
+
+       return res;
+}
+
 static int cmd_list(int argc, const char **argv)
 {
        if (argc != 1)
@@ -801,6 +944,7 @@ static struct {
        { "reconfigure", cmd_reconfigure },
        { "delete", cmd_delete },
        { "version", cmd_version },
+       { "diagnose", cmd_diagnose },
        { NULL, NULL},
 };