Skip to content

Commit 26ae8da

Browse files
derrickstoleegitster
authored andcommitted
setup: add discover_git_directory_reason()
There are many reasons why discovering a Git directory may fail. In particular, 8959555 (setup_git_directory(): add an owner check for the top-level directory, 2022-03-02) added ownership checks as a security precaution. Callers attempting to set up a Git directory may want to inform the user about the reason for the failure. For that, expose the enum discovery_result from within setup.c and move it into cache.h where discover_git_directory() is defined. I initially wanted to change the return type of discover_git_directory() to be this enum, but several callers rely upon the "zero means success". The two problems with this are: 1. The zero value of the enum is actually GIT_DIR_NONE, so nonpositive results are errors. 2. There are multiple successful states; positive results are successful. It is worth noting that GIT_DIR_NONE is not returned, so we remove this option from the enum. We must be careful to keep the successful reasons as positive values, so they are given explicit positive values. Instead of updating all callers immediately, add a new method, discover_git_directory_reason(), and convert discover_git_directory() to be a thin shim on top of it. One thing that is important to note is that discover_git_directory() previously returned -1 on error, so let's continue that into the future. There is only one caller (in scalar.c) that depends on that signedness instead of a non-zero check, so clean that up, too. Because there are extra checks that discover_git_directory_reason() does after setup_git_directory_gently_1(), there are other modes that can be returned for failure states. Add these modes to the enum, but be sure to explicitly add them as BUG() states in the switch of setup_git_directory_gently(). Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4527db8 commit 26ae8da

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

setup.c

Lines changed: 12 additions & 22 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,23 @@ 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+
result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0);
1380+
if (result <= 0) {
13921381
strbuf_release(&dir);
1393-
return -1;
1382+
return result;
13941383
}
13951384

13961385
/*
@@ -1420,7 +1409,7 @@ int discover_git_directory(struct strbuf *commondir,
14201409
strbuf_setlen(commondir, commondir_offset);
14211410
strbuf_setlen(gitdir, gitdir_offset);
14221411
clear_repository_format(&candidate);
1423-
return -1;
1412+
return GIT_DIR_INVALID_FORMAT;
14241413
}
14251414

14261415
/* take ownership of candidate.partial_clone */
@@ -1429,7 +1418,7 @@ int discover_git_directory(struct strbuf *commondir,
14291418
candidate.partial_clone = NULL;
14301419

14311420
clear_repository_format(&candidate);
1432-
return 0;
1421+
return result;
14331422
}
14341423

14351424
const char *setup_git_directory_gently(int *nongit_ok)
@@ -1511,10 +1500,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
15111500
}
15121501
*nongit_ok = 1;
15131502
break;
1514-
case GIT_DIR_NONE:
1503+
case GIT_DIR_CWD_FAILURE:
1504+
case GIT_DIR_INVALID_FORMAT:
15151505
/*
15161506
* As a safeguard against setup_git_directory_gently_1 returning
1517-
* this value, fallthrough to BUG. Otherwise it is possible to
1507+
* these values, fallthrough to BUG. Otherwise it is possible to
15181508
* set startup_info->have_repository to 1 when we did nothing to
15191509
* find a repository.
15201510
*/

setup.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,45 @@ 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_EXPLICIT = 1,
54+
GIT_DIR_DISCOVERED = 2,
55+
GIT_DIR_BARE = 3,
56+
/* these are errors */
57+
GIT_DIR_HIT_CEILING = -1,
58+
GIT_DIR_HIT_MOUNT_POINT = -2,
59+
GIT_DIR_INVALID_GITFILE = -3,
60+
GIT_DIR_INVALID_OWNERSHIP = -4,
61+
GIT_DIR_DISALLOWED_BARE = -5,
62+
GIT_DIR_INVALID_FORMAT = -6,
63+
GIT_DIR_CWD_FAILURE = -7,
64+
};
65+
enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
66+
struct strbuf *gitdir);
67+
4568
/*
4669
* Find the commondir and gitdir of the repository that contains the current
4770
* working directory, without changing the working directory or other global
4871
* state. The result is appended to commondir and gitdir. If the discovered
4972
* gitdir does not correspond to a worktree, then 'commondir' and 'gitdir' will
5073
* both have the same result appended to the buffer. The return value is
51-
* either 0 upon success and non-zero if no repository was found.
74+
* either 0 upon success and -1 if no repository was found.
5275
*/
53-
int discover_git_directory(struct strbuf *commondir,
54-
struct strbuf *gitdir);
76+
static inline int discover_git_directory(struct strbuf *commondir,
77+
struct strbuf *gitdir)
78+
{
79+
if (discover_git_directory_reason(commondir, gitdir) <= 0)
80+
return -1;
81+
return 0;
82+
}
83+
5584
const char *setup_git_directory_gently(int *);
5685
const char *setup_git_directory(void);
5786
char *prefix_path(const char *prefix, int len, const char *path);

0 commit comments

Comments
 (0)