Skip to content

Commit 41a76f7

Browse files
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 2ed6b9a + 277956a commit 41a76f7

File tree

3 files changed

+73
-33
lines changed

3 files changed

+73
-33
lines changed

cache.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,29 @@ 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_INVALID_FORMAT = -5,
652+
GIT_DIR_CWD_FAILURE = -6,
653+
};
654+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
655+
struct strbuf *gitdir);
656+
634657
/*
635658
* Find the commondir and gitdir of the repository that contains the current
636659
* working directory, without changing the working directory or other global
@@ -639,8 +662,12 @@ void setup_work_tree(void);
639662
* both have the same result appended to the buffer. The return value is
640663
* either 0 upon success and non-zero if no repository was found.
641664
*/
642-
int discover_git_directory(struct strbuf *commondir,
643-
struct strbuf *gitdir);
665+
static inline int discover_git_directory(struct strbuf *commondir,
666+
struct strbuf *gitdir)
667+
{
668+
return discover_git_directory_reason(commondir, gitdir) <= 0;
669+
}
670+
644671
const char *setup_git_directory_gently(int *);
645672
const char *setup_git_directory(void);
646673
char *prefix_path(const char *prefix, int len, const char *path);

contrib/scalar/scalar.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,26 +1349,48 @@ static int cmd_reconfigure(int argc, const char **argv)
13491349
git_config(get_scalar_repos, &scalar_repos);
13501350

13511351
for (i = 0; i < scalar_repos.nr; i++) {
1352+
int failed = 0;
13521353
const char *dir = scalar_repos.items[i].string;
13531354

13541355
strbuf_reset(&commondir);
13551356
strbuf_reset(&gitdir);
13561357

13571358
if (chdir(dir) < 0) {
13581359
warning_errno(_("could not switch to '%s'"), dir);
1359-
res = -1;
1360-
} else if (discover_git_directory(&commondir, &gitdir) < 0) {
1361-
warning_errno(_("git repository gone in '%s'"), dir);
1362-
res = -1;
1363-
} else {
1364-
git_config_clear();
1360+
failed = -1;
1361+
goto loop_end;
1362+
}
1363+
1364+
switch (discover_git_directory_reason(&commondir, &gitdir)) {
1365+
case GIT_DIR_INVALID_OWNERSHIP:
1366+
warning(_("repository at '%s' has different owner"), dir);
1367+
failed = -1;
1368+
goto loop_end;
1369+
1370+
case GIT_DIR_DISCOVERED:
1371+
break;
1372+
1373+
default:
1374+
warning(_("repository not found in '%s'"), dir);
1375+
failed = -1;
1376+
break;
1377+
}
1378+
1379+
git_config_clear();
1380+
1381+
the_repository = &r;
1382+
r.commondir = commondir.buf;
1383+
r.gitdir = gitdir.buf;
13651384

1366-
the_repository = &r;
1367-
r.commondir = commondir.buf;
1368-
r.gitdir = gitdir.buf;
1385+
if (set_recommended_config(1) < 0)
1386+
failed = -1;
13691387

1370-
if (set_recommended_config(1) < 0)
1371-
res = -1;
1388+
loop_end:
1389+
if (failed) {
1390+
res = failed;
1391+
warning(_("to unregister this repository from Scalar, run\n"
1392+
"\tgit config --global --unset --fixed-value scalar.repo \"%s\""),
1393+
dir);
13721394
}
13731395
}
13741396

setup.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,18 +1133,6 @@ static int ensure_valid_ownership(const char *path)
11331133
return data.is_safe;
11341134
}
11351135

1136-
enum discovery_result {
1137-
GIT_DIR_NONE = 0,
1138-
GIT_DIR_EXPLICIT,
1139-
GIT_DIR_DISCOVERED,
1140-
GIT_DIR_BARE,
1141-
/* these are errors */
1142-
GIT_DIR_HIT_CEILING = -1,
1143-
GIT_DIR_HIT_MOUNT_POINT = -2,
1144-
GIT_DIR_INVALID_GITFILE = -3,
1145-
GIT_DIR_INVALID_OWNERSHIP = -4
1146-
};
1147-
11481136
/*
11491137
* We cannot decide in this function whether we are in the work tree or
11501138
* not, since the config can only be read _after_ this function was called.
@@ -1260,21 +1248,22 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
12601248
}
12611249
}
12621250

1263-
int discover_git_directory(struct strbuf *commondir,
1264-
struct strbuf *gitdir)
1251+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
1252+
struct strbuf *gitdir)
12651253
{
12661254
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
12671255
size_t gitdir_offset = gitdir->len, cwd_len;
12681256
size_t commondir_offset = commondir->len;
12691257
struct repository_format candidate = REPOSITORY_FORMAT_INIT;
1258+
enum discovery_result result;
12701259

12711260
if (strbuf_getcwd(&dir))
1272-
return -1;
1261+
return GIT_DIR_CWD_FAILURE;
12731262

12741263
cwd_len = dir.len;
1275-
if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
1264+
if ((result = setup_git_directory_gently_1(&dir, gitdir, 0)) <= 0) {
12761265
strbuf_release(&dir);
1277-
return -1;
1266+
return result;
12781267
}
12791268

12801269
/*
@@ -1304,7 +1293,7 @@ int discover_git_directory(struct strbuf *commondir,
13041293
strbuf_setlen(commondir, commondir_offset);
13051294
strbuf_setlen(gitdir, gitdir_offset);
13061295
clear_repository_format(&candidate);
1307-
return -1;
1296+
return GIT_DIR_INVALID_FORMAT;
13081297
}
13091298

13101299
/* take ownership of candidate.partial_clone */
@@ -1313,7 +1302,7 @@ int discover_git_directory(struct strbuf *commondir,
13131302
candidate.partial_clone = NULL;
13141303

13151304
clear_repository_format(&candidate);
1316-
return 0;
1305+
return result;
13171306
}
13181307

13191308
const char *setup_git_directory_gently(int *nongit_ok)
@@ -1394,9 +1383,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
13941383
*nongit_ok = 1;
13951384
break;
13961385
case GIT_DIR_NONE:
1386+
case GIT_DIR_CWD_FAILURE:
1387+
case GIT_DIR_INVALID_FORMAT:
13971388
/*
13981389
* As a safeguard against setup_git_directory_gently_1 returning
1399-
* this value, fallthrough to BUG. Otherwise it is possible to
1390+
* these values, fallthrough to BUG. Otherwise it is possible to
14001391
* set startup_info->have_repository to 1 when we did nothing to
14011392
* find a repository.
14021393
*/

0 commit comments

Comments
 (0)