diff options
Diffstat (limited to 'scalar.c')
| -rw-r--r-- | scalar.c | 154 |
1 files changed, 106 insertions, 48 deletions
@@ -2,7 +2,8 @@ * The Scalar command-line interface. */ -#include "cache.h" +#include "git-compat-util.h" +#include "abspath.h" #include "gettext.h" #include "parse-options.h" #include "config.h" @@ -14,6 +15,8 @@ #include "dir.h" #include "packfile.h" #include "help.h" +#include "setup.h" +#include "trace2.h" static void setup_enlistment_directory(int argc, const char **argv, const char * const *usagestr, @@ -69,21 +72,18 @@ static void setup_enlistment_directory(int argc, const char **argv, static int run_git(const char *arg, ...) { - struct strvec argv = STRVEC_INIT; + struct child_process cmd = CHILD_PROCESS_INIT; va_list args; const char *p; - int res; va_start(args, arg); - strvec_push(&argv, arg); + strvec_push(&cmd.args, arg); while ((p = va_arg(args, const char *))) - strvec_push(&argv, p); + strvec_push(&cmd.args, p); va_end(args); - res = run_command_v_opt(argv.v, RUN_GIT_CMD); - - strvec_clear(&argv); - return res; + cmd.git_cmd = 1; + return run_command(&cmd); } struct scalar_config { @@ -146,6 +146,7 @@ static int set_recommended_config(int reconfigure) { "credential.validate", "false", 1 }, /* GCM4W-only */ { "gc.auto", "0", 1 }, { "gui.GCWarning", "false", 1 }, + { "index.skipHash", "false", 1 }, { "index.threads", "true", 1 }, { "index.version", "4", 1 }, { "merge.stat", "false", 1 }, @@ -207,7 +208,10 @@ static int set_recommended_config(int reconfigure) static int toggle_maintenance(int enable) { - return run_git("maintenance", enable ? "start" : "unregister", NULL); + return run_git("maintenance", + enable ? "start" : "unregister", + enable ? NULL : "--force", + NULL); } static int add_or_remove_enlistment(int add) @@ -261,7 +265,7 @@ static int register_dir(void) return error(_("could not set recommended config")); if (toggle_maintenance(1)) - return error(_("could not turn on maintenance")); + warning(_("could not turn on maintenance")); if (have_fsmonitor_support() && start_fsmonitor_daemon()) { return error(_("could not start the FSMonitor daemon")); @@ -404,7 +408,8 @@ void load_builtin_commands(const char *prefix, struct cmdnames *cmds) static int cmd_clone(int argc, const char **argv) { const char *branch = NULL; - int full_clone = 0, single_branch = 0; + int full_clone = 0, single_branch = 0, show_progress = isatty(2); + int src = 1; struct option clone_options[] = { OPT_STRING('b', "branch", &branch, N_("<branch>"), N_("branch to checkout after clone")), @@ -413,10 +418,13 @@ static int cmd_clone(int argc, const char **argv) OPT_BOOL(0, "single-branch", &single_branch, N_("only download metadata for the branch that will " "be checked out")), + OPT_BOOL(0, "src", &src, + N_("create repository within 'src' directory")), OPT_END(), }; const char * const clone_usage[] = { - N_("scalar clone [<options>] [--] <repo> [<dir>]"), + N_("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n" + "\t[--[no-]src] <url> [<enlistment>]"), NULL }; const char *url; @@ -452,7 +460,10 @@ static int cmd_clone(int argc, const char **argv) if (is_directory(enlistment)) die(_("directory '%s' exists already"), enlistment); - dir = xstrfmt("%s/src", enlistment); + if (src) + dir = xstrfmt("%s/src", enlistment); + else + dir = xstrdup(enlistment); strbuf_reset(&buf); if (branch) @@ -499,7 +510,9 @@ static int cmd_clone(int argc, const char **argv) if (set_recommended_config(0)) return error(_("could not configure '%s'"), dir); - if ((res = run_git("fetch", "--quiet", "origin", NULL))) { + if ((res = run_git("fetch", "--quiet", + show_progress ? "--progress" : "--no-progress", + "origin", NULL))) { warning(_("partial clone failed; attempting full clone")); if (set_config("remote.origin.promisor") || @@ -508,7 +521,9 @@ static int cmd_clone(int argc, const char **argv) goto cleanup; } - if ((res = run_git("fetch", "--quiet", "origin", NULL))) + if ((res = run_git("fetch", "--quiet", + show_progress ? "--progress" : "--no-progress", + "origin", NULL))) goto cleanup; } @@ -558,7 +573,7 @@ static int cmd_diagnose(int argc, const char **argv) return res; } -static int cmd_list(int argc, const char **argv) +static int cmd_list(int argc, const char **argv UNUSED) { if (argc != 1) die(_("`scalar list` does not take arguments")); @@ -586,7 +601,9 @@ static int cmd_register(int argc, const char **argv) return register_dir(); } -static int get_scalar_repos(const char *key, const char *value, void *data) +static int get_scalar_repos(const char *key, const char *value, + const struct config_context *ctx UNUSED, + void *data) { struct string_list *list = data; @@ -596,6 +613,24 @@ static int get_scalar_repos(const char *key, const char *value, void *data) return 0; } +static int remove_deleted_enlistment(struct strbuf *path) +{ + int res = 0; + strbuf_realpath_forgiving(path, path->buf, 1); + + if (run_git("config", "--global", + "--unset", "--fixed-value", + "scalar.repo", path->buf, NULL) < 0) + res = -1; + + if (run_git("config", "--global", + "--unset", "--fixed-value", + "maintenance.repo", path->buf, NULL) < 0) + res = -1; + + return res; +} + static int cmd_reconfigure(int argc, const char **argv) { int all = 0; @@ -629,26 +664,67 @@ static int cmd_reconfigure(int argc, const char **argv) git_config(get_scalar_repos, &scalar_repos); for (i = 0; i < scalar_repos.nr; i++) { + int succeeded = 0; const char *dir = scalar_repos.items[i].string; strbuf_reset(&commondir); strbuf_reset(&gitdir); if (chdir(dir) < 0) { - warning_errno(_("could not switch to '%s'"), dir); - res = -1; - } else if (discover_git_directory(&commondir, &gitdir) < 0) { - warning_errno(_("git repository gone in '%s'"), dir); - res = -1; - } else { - git_config_clear(); + struct strbuf buf = STRBUF_INIT; + + if (errno != ENOENT) { + warning_errno(_("could not switch to '%s'"), dir); + goto loop_end; + } + + strbuf_addstr(&buf, dir); + if (remove_deleted_enlistment(&buf)) + error(_("could not remove stale " + "scalar.repo '%s'"), dir); + else { + warning(_("removed stale scalar.repo '%s'"), + dir); + succeeded = 1; + } + strbuf_release(&buf); + goto loop_end; + } - the_repository = &r; - r.commondir = commondir.buf; - r.gitdir = gitdir.buf; + switch (discover_git_directory_reason(&commondir, &gitdir)) { + case GIT_DIR_INVALID_OWNERSHIP: + warning(_("repository at '%s' has different owner"), dir); + goto loop_end; - if (set_recommended_config(1) < 0) - res = -1; + case GIT_DIR_INVALID_GITFILE: + case GIT_DIR_INVALID_FORMAT: + warning(_("repository at '%s' has a format issue"), dir); + goto loop_end; + + case GIT_DIR_DISCOVERED: + succeeded = 1; + break; + + default: + warning(_("repository not found in '%s'"), dir); + break; + } + + git_config_clear(); + + the_repository = &r; + r.commondir = commondir.buf; + r.gitdir = gitdir.buf; + + if (set_recommended_config(1) >= 0) + succeeded = 1; + +loop_end: + if (!succeeded) { + res = -1; + warning(_("to unregister this repository from Scalar, run\n" + "\tgit config --global --unset --fixed-value scalar.repo \"%s\""), + dir); } } @@ -722,24 +798,6 @@ static int cmd_run(int argc, const char **argv) return 0; } -static int remove_deleted_enlistment(struct strbuf *path) -{ - int res = 0; - strbuf_realpath_forgiving(path, path->buf, 1); - - if (run_git("config", "--global", - "--unset", "--fixed-value", - "scalar.repo", path->buf, NULL) < 0) - res = -1; - - if (run_git("config", "--global", - "--unset", "--fixed-value", - "maintenance.repo", path->buf, NULL) < 0) - res = -1; - - return res; -} - static int cmd_unregister(int argc, const char **argv) { struct option options[] = { |
