Skip to content

Commit

Permalink
Merge pull request #508: scalar reconfigure: help users remove buggy …
Browse files Browse the repository at this point in the history
…repos

When running 'scalar reconfigure -a', such as at install time, Scalar
has warning messages about the repository missing (or not containing a
.git directory). Failures can also happen while trying to modify the
repository-local config for that repository.

These warnings may seem confusing to users who don't understand what
they mean or how to stop them.

Add a warning that instructs the user how to remove the warning in
future installations.
  • Loading branch information
derrickstolee authored and vdye committed May 17, 2023
2 parents 9376707 + 959ba03 commit 0916f37
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 40 deletions.
56 changes: 39 additions & 17 deletions scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ 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 failed = 0;
const char *dir = scalar_repos.items[i].string;

strbuf_reset(&commondir);
Expand All @@ -1042,30 +1043,51 @@ static int cmd_reconfigure(int argc, const char **argv)

if (errno != ENOENT) {
warning_errno(_("could not switch to '%s'"), dir);
res = -1;
continue;
failed = -1;
goto loop_end;
}

strbuf_addstr(&buf, dir);
if (remove_deleted_enlistment(&buf))
res = error(_("could not remove stale "
"scalar.repo '%s'"), dir);
failed = error(_("could not remove stale "
"scalar.repo '%s'"), dir);
else
warning(_("removing stale scalar.repo '%s'"),
warning(_("removed stale scalar.repo '%s'"),
dir);
strbuf_release(&buf);
} else if (discover_git_directory(&commondir, &gitdir) < 0) {
warning_errno(_("git repository gone in '%s'"), dir);
res = -1;
} else {
git_config_clear();

the_repository = &r;
r.commondir = commondir.buf;
r.gitdir = gitdir.buf;

if (set_recommended_config(1) < 0)
res = -1;
goto loop_end;
}

switch (discover_git_directory_reason(&commondir, &gitdir)) {
case GIT_DIR_INVALID_OWNERSHIP:
warning(_("repository at '%s' has different owner"), dir);
failed = -1;
goto loop_end;

case GIT_DIR_DISCOVERED:
break;

default:
warning(_("repository not found in '%s'"), dir);
failed = -1;
break;
}

git_config_clear();

the_repository = &r;
r.commondir = commondir.buf;
r.gitdir = gitdir.buf;

if (set_recommended_config(1) < 0)
failed = -1;

loop_end:
if (failed) {
res = failed;
warning(_("to unregister this repository from Scalar, run\n"
"\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
dir);
}
}

Expand Down
32 changes: 11 additions & 21 deletions setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,19 +1212,6 @@ static const char *allowed_bare_repo_to_string(
return NULL;
}

enum discovery_result {
GIT_DIR_NONE = 0,
GIT_DIR_EXPLICIT,
GIT_DIR_DISCOVERED,
GIT_DIR_BARE,
/* these are errors */
GIT_DIR_HIT_CEILING = -1,
GIT_DIR_HIT_MOUNT_POINT = -2,
GIT_DIR_INVALID_GITFILE = -3,
GIT_DIR_INVALID_OWNERSHIP = -4,
GIT_DIR_DISALLOWED_BARE = -5,
};

/*
* We cannot decide in this function whether we are in the work tree or
* not, since the config can only be read _after_ this function was called.
Expand Down Expand Up @@ -1376,21 +1363,22 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
}
}

int discover_git_directory(struct strbuf *commondir,
struct strbuf *gitdir)
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
struct strbuf *gitdir)
{
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
size_t gitdir_offset = gitdir->len, cwd_len;
size_t commondir_offset = commondir->len;
struct repository_format candidate = REPOSITORY_FORMAT_INIT;
enum discovery_result result;

if (strbuf_getcwd(&dir))
return -1;
return GIT_DIR_CWD_FAILURE;

cwd_len = dir.len;
if (setup_git_directory_gently_1(&dir, gitdir, NULL, 0) <= 0) {
if ((result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0)) <= 0) {
strbuf_release(&dir);
return -1;
return result;
}

/*
Expand Down Expand Up @@ -1420,7 +1408,7 @@ int discover_git_directory(struct strbuf *commondir,
strbuf_setlen(commondir, commondir_offset);
strbuf_setlen(gitdir, gitdir_offset);
clear_repository_format(&candidate);
return -1;
return GIT_DIR_INVALID_FORMAT;
}

/* take ownership of candidate.partial_clone */
Expand All @@ -1429,7 +1417,7 @@ int discover_git_directory(struct strbuf *commondir,
candidate.partial_clone = NULL;

clear_repository_format(&candidate);
return 0;
return result;
}

const char *setup_git_directory_gently(int *nongit_ok)
Expand Down Expand Up @@ -1521,9 +1509,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
*nongit_ok = 1;
break;
case GIT_DIR_NONE:
case GIT_DIR_CWD_FAILURE:
case GIT_DIR_INVALID_FORMAT:
/*
* As a safeguard against setup_git_directory_gently_1 returning
* this value, fallthrough to BUG. Otherwise it is possible to
* these values, fallthrough to BUG. Otherwise it is possible to
* set startup_info->have_repository to 1 when we did nothing to
* find a repository.
*/
Expand Down
32 changes: 30 additions & 2 deletions setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)

void setup_work_tree(void);

/*
* discover_git_directory_reason() is similar to discover_git_directory(),
* except it returns an enum value instead. It is important to note that
* a zero-valued return here is actually GIT_DIR_NONE, which is different
* from discover_git_directory.
*/
enum discovery_result {
GIT_DIR_NONE = 0,
GIT_DIR_EXPLICIT,
GIT_DIR_DISCOVERED,
GIT_DIR_BARE,
/* these are errors */
GIT_DIR_HIT_CEILING = -1,
GIT_DIR_HIT_MOUNT_POINT = -2,
GIT_DIR_INVALID_GITFILE = -3,
GIT_DIR_INVALID_OWNERSHIP = -4,
GIT_DIR_DISALLOWED_BARE = -5,
GIT_DIR_INVALID_FORMAT = -6,
GIT_DIR_CWD_FAILURE = -7,
};
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
struct strbuf *gitdir);

/*
* Find the commondir and gitdir of the repository that contains the current
* working directory, without changing the working directory or other global
Expand All @@ -50,8 +74,12 @@ void setup_work_tree(void);
* both have the same result appended to the buffer. The return value is
* either 0 upon success and non-zero if no repository was found.
*/
int discover_git_directory(struct strbuf *commondir,
struct strbuf *gitdir);
static inline int discover_git_directory(struct strbuf *commondir,
struct strbuf *gitdir)
{
return discover_git_directory_reason(commondir, gitdir) <= 0;
}

const char *setup_git_directory_gently(int *);
const char *setup_git_directory(void);
char *prefix_path(const char *prefix, int len, const char *path);
Expand Down

0 comments on commit 0916f37

Please sign in to comment.