Skip to content

Commit 07da902

Browse files
derrickstoleedscho
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 045f3ee + b2158cd commit 07da902

3 files changed

Lines changed: 73 additions & 33 deletions

File tree

cache.h

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

634634
void setup_work_tree(void);
635+
636+
/*
637+
* discover_git_directory_reason() is similar to discover_git_directory(),
638+
* except it returns an enum value instead. It is important to note that
639+
* a zero-valued return here is actually GIT_DIR_NONE, which is different
640+
* from discover_git_directory.
641+
*/
642+
enum discovery_result {
643+
GIT_DIR_NONE = 0,
644+
GIT_DIR_EXPLICIT,
645+
GIT_DIR_DISCOVERED,
646+
GIT_DIR_BARE,
647+
/* these are errors */
648+
GIT_DIR_HIT_CEILING = -1,
649+
GIT_DIR_HIT_MOUNT_POINT = -2,
650+
GIT_DIR_INVALID_GITFILE = -3,
651+
GIT_DIR_INVALID_OWNERSHIP = -4,
652+
GIT_DIR_INVALID_FORMAT = -5,
653+
GIT_DIR_CWD_FAILURE = -6,
654+
};
655+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
656+
struct strbuf *gitdir);
657+
635658
/*
636659
* Find the commondir and gitdir of the repository that contains the current
637660
* working directory, without changing the working directory or other global
@@ -640,8 +663,12 @@ void setup_work_tree(void);
640663
* both have the same result appended to the buffer. The return value is
641664
* either 0 upon success and non-zero if no repository was found.
642665
*/
643-
int discover_git_directory(struct strbuf *commondir,
644-
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+
645672
const char *setup_git_directory_gently(int *);
646673
const char *setup_git_directory(void);
647674
char *prefix_path(const char *prefix, int len, const char *path);

scalar.c

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

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

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

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

1367-
the_repository = &r;
1368-
r.commondir = commondir.buf;
1369-
r.gitdir = gitdir.buf;
1386+
if (set_recommended_config(1) < 0)
1387+
failed = -1;
13701388

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

setup.c

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

1145-
enum discovery_result {
1146-
GIT_DIR_NONE = 0,
1147-
GIT_DIR_EXPLICIT,
1148-
GIT_DIR_DISCOVERED,
1149-
GIT_DIR_BARE,
1150-
/* these are errors */
1151-
GIT_DIR_HIT_CEILING = -1,
1152-
GIT_DIR_HIT_MOUNT_POINT = -2,
1153-
GIT_DIR_INVALID_GITFILE = -3,
1154-
GIT_DIR_INVALID_OWNERSHIP = -4
1155-
};
1156-
11571145
/*
11581146
* We cannot decide in this function whether we are in the work tree or
11591147
* not, since the config can only be read _after_ this function was called.
@@ -1269,21 +1257,22 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
12691257
}
12701258
}
12711259

1272-
int discover_git_directory(struct strbuf *commondir,
1273-
struct strbuf *gitdir)
1260+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
1261+
struct strbuf *gitdir)
12741262
{
12751263
struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
12761264
size_t gitdir_offset = gitdir->len, cwd_len;
12771265
size_t commondir_offset = commondir->len;
12781266
struct repository_format candidate = REPOSITORY_FORMAT_INIT;
1267+
enum discovery_result result;
12791268

12801269
if (strbuf_getcwd(&dir))
1281-
return -1;
1270+
return GIT_DIR_CWD_FAILURE;
12821271

12831272
cwd_len = dir.len;
1284-
if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
1273+
if ((result = setup_git_directory_gently_1(&dir, gitdir, 0)) <= 0) {
12851274
strbuf_release(&dir);
1286-
return -1;
1275+
return result;
12871276
}
12881277

12891278
/*
@@ -1313,7 +1302,7 @@ int discover_git_directory(struct strbuf *commondir,
13131302
strbuf_setlen(commondir, commondir_offset);
13141303
strbuf_setlen(gitdir, gitdir_offset);
13151304
clear_repository_format(&candidate);
1316-
return -1;
1305+
return GIT_DIR_INVALID_FORMAT;
13171306
}
13181307

13191308
/* take ownership of candidate.partial_clone */
@@ -1322,7 +1311,7 @@ int discover_git_directory(struct strbuf *commondir,
13221311
candidate.partial_clone = NULL;
13231312

13241313
clear_repository_format(&candidate);
1325-
return 0;
1314+
return result;
13261315
}
13271316

13281317
const char *setup_git_directory_gently(int *nongit_ok)
@@ -1403,9 +1392,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
14031392
*nongit_ok = 1;
14041393
break;
14051394
case GIT_DIR_NONE:
1395+
case GIT_DIR_CWD_FAILURE:
1396+
case GIT_DIR_INVALID_FORMAT:
14061397
/*
14071398
* As a safeguard against setup_git_directory_gently_1 returning
1408-
* this value, fallthrough to BUG. Otherwise it is possible to
1399+
* these values, fallthrough to BUG. Otherwise it is possible to
14091400
* set startup_info->have_repository to 1 when we did nothing to
14101401
* find a repository.
14111402
*/

0 commit comments

Comments
 (0)