Skip to content

Commit 59e43fa

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 8d6adba + db8dd2d commit 59e43fa

File tree

3 files changed

+76
-36
lines changed

3 files changed

+76
-36
lines changed

cache.h

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

633633
void setup_work_tree(void);
634+
635+
/*
636+
* discover_git_directory_reason() is similar to discover_git_directory(),
637+
* except it returns an enum value instead. It is important to note that
638+
* a zero-valued return here is actually GIT_DIR_NONE, which is different
639+
* from discover_git_directory.
640+
*/
641+
enum discovery_result {
642+
GIT_DIR_NONE = 0,
643+
GIT_DIR_EXPLICIT,
644+
GIT_DIR_DISCOVERED,
645+
GIT_DIR_BARE,
646+
/* these are errors */
647+
GIT_DIR_HIT_CEILING = -1,
648+
GIT_DIR_HIT_MOUNT_POINT = -2,
649+
GIT_DIR_INVALID_GITFILE = -3,
650+
GIT_DIR_INVALID_OWNERSHIP = -4,
651+
GIT_DIR_DISALLOWED_BARE = -5,
652+
GIT_DIR_INVALID_FORMAT = -6,
653+
GIT_DIR_CWD_FAILURE = -7,
654+
};
655+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
656+
struct strbuf *gitdir);
657+
634658
/*
635659
* Find the commondir and gitdir of the repository that contains the current
636660
* working directory, without changing the working directory or other global
@@ -639,8 +663,12 @@ void setup_work_tree(void);
639663
* both have the same result appended to the buffer. The return value is
640664
* either 0 upon success and non-zero if no repository was found.
641665
*/
642-
int discover_git_directory(struct strbuf *commondir,
643-
struct strbuf *gitdir);
666+
static inline int discover_git_directory(struct strbuf *commondir,
667+
struct strbuf *gitdir)
668+
{
669+
return discover_git_directory_reason(commondir, gitdir) <= 0;
670+
}
671+
644672
const char *setup_git_directory_gently(int *);
645673
const char *setup_git_directory(void);
646674
char *prefix_path(const char *prefix, int len, const char *path);

scalar.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -992,26 +992,48 @@ static int cmd_reconfigure(int argc, const char **argv)
992992
git_config(get_scalar_repos, &scalar_repos);
993993

994994
for (i = 0; i < scalar_repos.nr; i++) {
995+
int failed = 0;
995996
const char *dir = scalar_repos.items[i].string;
996997

997998
strbuf_reset(&commondir);
998999
strbuf_reset(&gitdir);
9991000

10001001
if (chdir(dir) < 0) {
10011002
warning_errno(_("could not switch to '%s'"), dir);
1002-
res = -1;
1003-
} else if (discover_git_directory(&commondir, &gitdir) < 0) {
1004-
warning_errno(_("git repository gone in '%s'"), dir);
1005-
res = -1;
1006-
} else {
1007-
git_config_clear();
1008-
1009-
the_repository = &r;
1010-
r.commondir = commondir.buf;
1011-
r.gitdir = gitdir.buf;
1012-
1013-
if (set_recommended_config(1) < 0)
1014-
res = -1;
1003+
failed = -1;
1004+
goto loop_end;
1005+
}
1006+
1007+
switch (discover_git_directory_reason(&commondir, &gitdir)) {
1008+
case GIT_DIR_INVALID_OWNERSHIP:
1009+
warning(_("repository at '%s' has different owner"), dir);
1010+
failed = -1;
1011+
goto loop_end;
1012+
1013+
case GIT_DIR_DISCOVERED:
1014+
break;
1015+
1016+
default:
1017+
warning(_("repository not found in '%s'"), dir);
1018+
failed = -1;
1019+
break;
1020+
}
1021+
1022+
git_config_clear();
1023+
1024+
the_repository = &r;
1025+
r.commondir = commondir.buf;
1026+
r.gitdir = gitdir.buf;
1027+
1028+
if (set_recommended_config(1) < 0)
1029+
failed = -1;
1030+
1031+
loop_end:
1032+
if (failed) {
1033+
res = failed;
1034+
warning(_("to unregister this repository from Scalar, run\n"
1035+
"\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
1036+
dir);
10151037
}
10161038
}
10171039

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)