Skip to content

Commit ffd036b

Browse files
peffgitster
authored andcommitted
clean: make is_git_repository a public function
We have always had is_git_directory(), for looking at a specific directory to see if it contains a git repo. In 0179ca7 (clean: improve performance when removing lots of directories, 2015-06-15), we added is_git_repository() which checks for a non-bare repository by looking at its ".git" entry. However, the fix in 0179ca7 needs to be applied other places, too. Let's make this new helper globally available. We need to give it a better name, though, to avoid confusion with is_git_directory(). This patch does that, documents both functions with a comment to reduce confusion, and removes the clean-specific references in the comments. Based-on-a-patch-by: Andreas Krey <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b0b6dd commit ffd036b

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

builtin/clean.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,6 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
147147
return 0;
148148
}
149149

150-
/*
151-
* Return 1 if the given path is the root of a git repository or
152-
* submodule else 0. Will not return 1 for bare repositories with the
153-
* exception of creating a bare repository in "foo/.git" and calling
154-
* is_git_repository("foo").
155-
*/
156-
static int is_git_repository(struct strbuf *path)
157-
{
158-
int ret = 0;
159-
int gitfile_error;
160-
size_t orig_path_len = path->len;
161-
assert(orig_path_len != 0);
162-
strbuf_complete(path, '/');
163-
strbuf_addstr(path, ".git");
164-
if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf))
165-
ret = 1;
166-
if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED ||
167-
gitfile_error == READ_GITFILE_ERR_READ_FAILED)
168-
ret = 1; /* This could be a real .git file, take the
169-
* safe option and avoid cleaning */
170-
strbuf_setlen(path, orig_path_len);
171-
return ret;
172-
}
173-
174150
static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
175151
int dry_run, int quiet, int *dir_gone)
176152
{
@@ -182,7 +158,7 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
182158

183159
*dir_gone = 1;
184160

185-
if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_git_repository(path)) {
161+
if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_nonbare_repository_dir(path)) {
186162
if (!quiet) {
187163
quote_path_relative(path->buf, prefix, &quoted);
188164
printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),

cache.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ extern char *git_work_tree_cfg;
456456
extern int is_inside_work_tree(void);
457457
extern const char *get_git_dir(void);
458458
extern const char *get_git_common_dir(void);
459-
extern int is_git_directory(const char *path);
460459
extern char *get_object_directory(void);
461460
extern char *get_index_file(void);
462461
extern char *get_graft_file(void);
@@ -467,6 +466,25 @@ extern const char *get_git_namespace(void);
467466
extern const char *strip_namespace(const char *namespaced_ref);
468467
extern const char *get_git_work_tree(void);
469468

469+
/*
470+
* Return true if the given path is a git directory; note that this _just_
471+
* looks at the directory itself. If you want to know whether "foo/.git"
472+
* is a repository, you must feed that path, not just "foo".
473+
*/
474+
extern int is_git_directory(const char *path);
475+
476+
/*
477+
* Return 1 if the given path is the root of a git repository or
478+
* submodule, else 0. Will not return 1 for bare repositories with the
479+
* exception of creating a bare repository in "foo/.git" and calling
480+
* is_git_repository("foo").
481+
*
482+
* If we run into read errors, we err on the side of saying "yes, it is",
483+
* as we usually consider sub-repos precious, and would prefer to err on the
484+
* side of not disrupting or deleting them.
485+
*/
486+
extern int is_nonbare_repository_dir(struct strbuf *path);
487+
470488
#define READ_GITFILE_ERR_STAT_FAILED 1
471489
#define READ_GITFILE_ERR_NOT_A_FILE 2
472490
#define READ_GITFILE_ERR_OPEN_FAILED 3

setup.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,23 @@ int is_git_directory(const char *suspect)
312312
return ret;
313313
}
314314

315+
int is_nonbare_repository_dir(struct strbuf *path)
316+
{
317+
int ret = 0;
318+
int gitfile_error;
319+
size_t orig_path_len = path->len;
320+
assert(orig_path_len != 0);
321+
strbuf_complete(path, '/');
322+
strbuf_addstr(path, ".git");
323+
if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf))
324+
ret = 1;
325+
if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED ||
326+
gitfile_error == READ_GITFILE_ERR_READ_FAILED)
327+
ret = 1;
328+
strbuf_setlen(path, orig_path_len);
329+
return ret;
330+
}
331+
315332
int is_inside_git_dir(void)
316333
{
317334
if (inside_git_dir < 0)

0 commit comments

Comments
 (0)