Skip to content

Commit 1ba930f

Browse files
derrickstoleevdye
authored andcommitted
Merge pull request #508: scalar reconfigure: help users remove buggy 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.
2 parents 3a9bb0e + fa573c5 commit 1ba930f

File tree

3 files changed

+80
-40
lines changed

3 files changed

+80
-40
lines changed

scalar.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ static int cmd_reconfigure(int argc, const char **argv)
10321032
git_config(get_scalar_repos, &scalar_repos);
10331033

10341034
for (i = 0; i < scalar_repos.nr; i++) {
1035+
int failed = 0;
10351036
const char *dir = scalar_repos.items[i].string;
10361037

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

10431044
if (errno != ENOENT) {
10441045
warning_errno(_("could not switch to '%s'"), dir);
1045-
res = -1;
1046-
continue;
1046+
failed = -1;
1047+
goto loop_end;
10471048
}
10481049

10491050
strbuf_addstr(&buf, dir);
10501051
if (remove_deleted_enlistment(&buf))
1051-
res = error(_("could not remove stale "
1052-
"scalar.repo '%s'"), dir);
1052+
failed = error(_("could not remove stale "
1053+
"scalar.repo '%s'"), dir);
10531054
else
1054-
warning(_("removing stale scalar.repo '%s'"),
1055+
warning(_("removed stale scalar.repo '%s'"),
10551056
dir);
10561057
strbuf_release(&buf);
1057-
} else if (discover_git_directory(&commondir, &gitdir) < 0) {
1058-
warning_errno(_("git repository gone in '%s'"), dir);
1059-
res = -1;
1060-
} else {
1061-
git_config_clear();
1062-
1063-
the_repository = &r;
1064-
r.commondir = commondir.buf;
1065-
r.gitdir = gitdir.buf;
1066-
1067-
if (set_recommended_config(1) < 0)
1068-
res = -1;
1058+
goto loop_end;
1059+
}
1060+
1061+
switch (discover_git_directory_reason(&commondir, &gitdir)) {
1062+
case GIT_DIR_INVALID_OWNERSHIP:
1063+
warning(_("repository at '%s' has different owner"), dir);
1064+
failed = -1;
1065+
goto loop_end;
1066+
1067+
case GIT_DIR_DISCOVERED:
1068+
break;
1069+
1070+
default:
1071+
warning(_("repository not found in '%s'"), dir);
1072+
failed = -1;
1073+
break;
1074+
}
1075+
1076+
git_config_clear();
1077+
1078+
the_repository = &r;
1079+
r.commondir = commondir.buf;
1080+
r.gitdir = gitdir.buf;
1081+
1082+
if (set_recommended_config(1) < 0)
1083+
failed = -1;
1084+
1085+
loop_end:
1086+
if (failed) {
1087+
res = failed;
1088+
warning(_("to unregister this repository from Scalar, run\n"
1089+
"\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
1090+
dir);
10691091
}
10701092
}
10711093

setup.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,19 +1212,6 @@ static const char *allowed_bare_repo_to_string(
12121212
return NULL;
12131213
}
12141214

1215-
enum discovery_result {
1216-
GIT_DIR_NONE = 0,
1217-
GIT_DIR_EXPLICIT,
1218-
GIT_DIR_DISCOVERED,
1219-
GIT_DIR_BARE,
1220-
/* these are errors */
1221-
GIT_DIR_HIT_CEILING = -1,
1222-
GIT_DIR_HIT_MOUNT_POINT = -2,
1223-
GIT_DIR_INVALID_GITFILE = -3,
1224-
GIT_DIR_INVALID_OWNERSHIP = -4,
1225-
GIT_DIR_DISALLOWED_BARE = -5,
1226-
};
1227-
12281215
/*
12291216
* We cannot decide in this function whether we are in the work tree or
12301217
* not, since the config can only be read _after_ this function was called.
@@ -1376,21 +1363,22 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
13761363
}
13771364
}
13781365

1379-
int discover_git_directory(struct strbuf *commondir,
1380-
struct strbuf *gitdir)
1366+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
1367+
struct strbuf *gitdir)
13811368
{
13821369
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
13831370
size_t gitdir_offset = gitdir->len, cwd_len;
13841371
size_t commondir_offset = commondir->len;
13851372
struct repository_format candidate = REPOSITORY_FORMAT_INIT;
1373+
enum discovery_result result;
13861374

13871375
if (strbuf_getcwd(&dir))
1388-
return -1;
1376+
return GIT_DIR_CWD_FAILURE;
13891377

13901378
cwd_len = dir.len;
1391-
if (setup_git_directory_gently_1(&dir, gitdir, NULL, 0) <= 0) {
1379+
if ((result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0)) <= 0) {
13921380
strbuf_release(&dir);
1393-
return -1;
1381+
return result;
13941382
}
13951383

13961384
/*
@@ -1420,7 +1408,7 @@ int discover_git_directory(struct strbuf *commondir,
14201408
strbuf_setlen(commondir, commondir_offset);
14211409
strbuf_setlen(gitdir, gitdir_offset);
14221410
clear_repository_format(&candidate);
1423-
return -1;
1411+
return GIT_DIR_INVALID_FORMAT;
14241412
}
14251413

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

14311419
clear_repository_format(&candidate);
1432-
return 0;
1420+
return result;
14331421
}
14341422

14351423
const char *setup_git_directory_gently(int *nongit_ok)
@@ -1521,9 +1509,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
15211509
*nongit_ok = 1;
15221510
break;
15231511
case GIT_DIR_NONE:
1512+
case GIT_DIR_CWD_FAILURE:
1513+
case GIT_DIR_INVALID_FORMAT:
15241514
/*
15251515
* As a safeguard against setup_git_directory_gently_1 returning
1526-
* this value, fallthrough to BUG. Otherwise it is possible to
1516+
* these values, fallthrough to BUG. Otherwise it is possible to
15271517
* set startup_info->have_repository to 1 when we did nothing to
15281518
* find a repository.
15291519
*/

setup.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
4242
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)
4343

4444
void setup_work_tree(void);
45+
46+
/*
47+
* discover_git_directory_reason() is similar to discover_git_directory(),
48+
* except it returns an enum value instead. It is important to note that
49+
* a zero-valued return here is actually GIT_DIR_NONE, which is different
50+
* from discover_git_directory.
51+
*/
52+
enum discovery_result {
53+
GIT_DIR_NONE = 0,
54+
GIT_DIR_EXPLICIT,
55+
GIT_DIR_DISCOVERED,
56+
GIT_DIR_BARE,
57+
/* these are errors */
58+
GIT_DIR_HIT_CEILING = -1,
59+
GIT_DIR_HIT_MOUNT_POINT = -2,
60+
GIT_DIR_INVALID_GITFILE = -3,
61+
GIT_DIR_INVALID_OWNERSHIP = -4,
62+
GIT_DIR_DISALLOWED_BARE = -5,
63+
GIT_DIR_INVALID_FORMAT = -6,
64+
GIT_DIR_CWD_FAILURE = -7,
65+
};
66+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
67+
struct strbuf *gitdir);
68+
4569
/*
4670
* Find the commondir and gitdir of the repository that contains the current
4771
* working directory, without changing the working directory or other global
@@ -50,8 +74,12 @@ void setup_work_tree(void);
5074
* both have the same result appended to the buffer. The return value is
5175
* either 0 upon success and non-zero if no repository was found.
5276
*/
53-
int discover_git_directory(struct strbuf *commondir,
54-
struct strbuf *gitdir);
77+
static inline int discover_git_directory(struct strbuf *commondir,
78+
struct strbuf *gitdir)
79+
{
80+
return discover_git_directory_reason(commondir, gitdir) <= 0;
81+
}
82+
5583
const char *setup_git_directory_gently(int *);
5684
const char *setup_git_directory(void);
5785
char *prefix_path(const char *prefix, int len, const char *path);

0 commit comments

Comments
 (0)