Skip to content

Commit a900d05

Browse files
derrickstoleedscho
authored andcommitted
Merge pull request git-for-windows#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 85b7a01 + 41d1372 commit a900d05

File tree

3 files changed

+80
-40
lines changed

3 files changed

+80
-40
lines changed

cache.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,30 @@ void set_git_work_tree(const char *tree);
607607
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
608608

609609
void setup_work_tree(void);
610+
611+
/*
612+
* discover_git_directory_reason() is similar to discover_git_directory(),
613+
* except it returns an enum value instead. It is important to note that
614+
* a zero-valued return here is actually GIT_DIR_NONE, which is different
615+
* from discover_git_directory.
616+
*/
617+
enum discovery_result {
618+
GIT_DIR_NONE = 0,
619+
GIT_DIR_EXPLICIT,
620+
GIT_DIR_DISCOVERED,
621+
GIT_DIR_BARE,
622+
/* these are errors */
623+
GIT_DIR_HIT_CEILING = -1,
624+
GIT_DIR_HIT_MOUNT_POINT = -2,
625+
GIT_DIR_INVALID_GITFILE = -3,
626+
GIT_DIR_INVALID_OWNERSHIP = -4,
627+
GIT_DIR_DISALLOWED_BARE = -5,
628+
GIT_DIR_INVALID_FORMAT = -6,
629+
GIT_DIR_CWD_FAILURE = -7,
630+
};
631+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
632+
struct strbuf *gitdir);
633+
610634
/*
611635
* Find the commondir and gitdir of the repository that contains the current
612636
* working directory, without changing the working directory or other global
@@ -615,8 +639,12 @@ void setup_work_tree(void);
615639
* both have the same result appended to the buffer. The return value is
616640
* either 0 upon success and non-zero if no repository was found.
617641
*/
618-
int discover_git_directory(struct strbuf *commondir,
619-
struct strbuf *gitdir);
642+
static inline int discover_git_directory(struct strbuf *commondir,
643+
struct strbuf *gitdir)
644+
{
645+
return discover_git_directory_reason(commondir, gitdir) <= 0;
646+
}
647+
620648
const char *setup_git_directory_gently(int *);
621649
const char *setup_git_directory(void);
622650
char *prefix_path(const char *prefix, int len, const char *path);

scalar.c

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

10221022
for (i = 0; i < scalar_repos.nr; i++) {
1023+
int failed = 0;
10231024
const char *dir = scalar_repos.items[i].string;
10241025

10251026
strbuf_reset(&commondir);
@@ -1030,30 +1031,51 @@ static int cmd_reconfigure(int argc, const char **argv)
10301031

10311032
if (errno != ENOENT) {
10321033
warning_errno(_("could not switch to '%s'"), dir);
1033-
res = -1;
1034-
continue;
1034+
failed = -1;
1035+
goto loop_end;
10351036
}
10361037

10371038
strbuf_addstr(&buf, dir);
10381039
if (remove_deleted_enlistment(&buf))
1039-
res = error(_("could not remove stale "
1040-
"scalar.repo '%s'"), dir);
1040+
failed = error(_("could not remove stale "
1041+
"scalar.repo '%s'"), dir);
10411042
else
1042-
warning(_("removing stale scalar.repo '%s'"),
1043+
warning(_("removed stale scalar.repo '%s'"),
10431044
dir);
10441045
strbuf_release(&buf);
1045-
} else if (discover_git_directory(&commondir, &gitdir) < 0) {
1046-
warning_errno(_("git repository gone in '%s'"), dir);
1047-
res = -1;
1048-
} else {
1049-
git_config_clear();
1050-
1051-
the_repository = &r;
1052-
r.commondir = commondir.buf;
1053-
r.gitdir = gitdir.buf;
1054-
1055-
if (set_recommended_config(1) < 0)
1056-
res = -1;
1046+
goto loop_end;
1047+
}
1048+
1049+
switch (discover_git_directory_reason(&commondir, &gitdir)) {
1050+
case GIT_DIR_INVALID_OWNERSHIP:
1051+
warning(_("repository at '%s' has different owner"), dir);
1052+
failed = -1;
1053+
goto loop_end;
1054+
1055+
case GIT_DIR_DISCOVERED:
1056+
break;
1057+
1058+
default:
1059+
warning(_("repository not found in '%s'"), dir);
1060+
failed = -1;
1061+
break;
1062+
}
1063+
1064+
git_config_clear();
1065+
1066+
the_repository = &r;
1067+
r.commondir = commondir.buf;
1068+
r.gitdir = gitdir.buf;
1069+
1070+
if (set_recommended_config(1) < 0)
1071+
failed = -1;
1072+
1073+
loop_end:
1074+
if (failed) {
1075+
res = failed;
1076+
warning(_("to unregister this repository from Scalar, run\n"
1077+
"\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
1078+
dir);
10571079
}
10581080
}
10591081

setup.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,19 +1205,6 @@ static const char *allowed_bare_repo_to_string(
12051205
return NULL;
12061206
}
12071207

1208-
enum discovery_result {
1209-
GIT_DIR_NONE = 0,
1210-
GIT_DIR_EXPLICIT,
1211-
GIT_DIR_DISCOVERED,
1212-
GIT_DIR_BARE,
1213-
/* these are errors */
1214-
GIT_DIR_HIT_CEILING = -1,
1215-
GIT_DIR_HIT_MOUNT_POINT = -2,
1216-
GIT_DIR_INVALID_GITFILE = -3,
1217-
GIT_DIR_INVALID_OWNERSHIP = -4,
1218-
GIT_DIR_DISALLOWED_BARE = -5,
1219-
};
1220-
12211208
/*
12221209
* We cannot decide in this function whether we are in the work tree or
12231210
* not, since the config can only be read _after_ this function was called.
@@ -1368,21 +1355,22 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
13681355
}
13691356
}
13701357

1371-
int discover_git_directory(struct strbuf *commondir,
1372-
struct strbuf *gitdir)
1358+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
1359+
struct strbuf *gitdir)
13731360
{
13741361
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
13751362
size_t gitdir_offset = gitdir->len, cwd_len;
13761363
size_t commondir_offset = commondir->len;
13771364
struct repository_format candidate = REPOSITORY_FORMAT_INIT;
1365+
enum discovery_result result;
13781366

13791367
if (strbuf_getcwd(&dir))
1380-
return -1;
1368+
return GIT_DIR_CWD_FAILURE;
13811369

13821370
cwd_len = dir.len;
1383-
if (setup_git_directory_gently_1(&dir, gitdir, NULL, 0) <= 0) {
1371+
if ((result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0)) <= 0) {
13841372
strbuf_release(&dir);
1385-
return -1;
1373+
return result;
13861374
}
13871375

13881376
/*
@@ -1412,7 +1400,7 @@ int discover_git_directory(struct strbuf *commondir,
14121400
strbuf_setlen(commondir, commondir_offset);
14131401
strbuf_setlen(gitdir, gitdir_offset);
14141402
clear_repository_format(&candidate);
1415-
return -1;
1403+
return GIT_DIR_INVALID_FORMAT;
14161404
}
14171405

14181406
/* take ownership of candidate.partial_clone */
@@ -1421,7 +1409,7 @@ int discover_git_directory(struct strbuf *commondir,
14211409
candidate.partial_clone = NULL;
14221410

14231411
clear_repository_format(&candidate);
1424-
return 0;
1412+
return result;
14251413
}
14261414

14271415
const char *setup_git_directory_gently(int *nongit_ok)
@@ -1513,9 +1501,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
15131501
*nongit_ok = 1;
15141502
break;
15151503
case GIT_DIR_NONE:
1504+
case GIT_DIR_CWD_FAILURE:
1505+
case GIT_DIR_INVALID_FORMAT:
15161506
/*
15171507
* As a safeguard against setup_git_directory_gently_1 returning
1518-
* this value, fallthrough to BUG. Otherwise it is possible to
1508+
* these values, fallthrough to BUG. Otherwise it is possible to
15191509
* set startup_info->have_repository to 1 when we did nothing to
15201510
* find a repository.
15211511
*/

0 commit comments

Comments
 (0)