|
10 | 10 | #include "cache.h"
|
11 | 11 | #include "dir.h"
|
12 | 12 | #include "parse-options.h"
|
13 |
| -#include "refs.h" |
14 | 13 | #include "string-list.h"
|
15 | 14 | #include "quote.h"
|
16 | 15 | #include "column.h"
|
@@ -148,20 +147,43 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
|
148 | 147 | return 0;
|
149 | 148 | }
|
150 | 149 |
|
| 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 | + if (path->buf[orig_path_len - 1] != '/') |
| 163 | + strbuf_addch(path, '/'); |
| 164 | + strbuf_addstr(path, ".git"); |
| 165 | + if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) |
| 166 | + ret = 1; |
| 167 | + if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || |
| 168 | + gitfile_error == READ_GITFILE_ERR_READ_FAILED) |
| 169 | + ret = 1; /* This could be a real .git file, take the |
| 170 | + * safe option and avoid cleaning */ |
| 171 | + strbuf_setlen(path, orig_path_len); |
| 172 | + return ret; |
| 173 | +} |
| 174 | + |
151 | 175 | static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
|
152 | 176 | int dry_run, int quiet, int *dir_gone)
|
153 | 177 | {
|
154 | 178 | DIR *dir;
|
155 | 179 | struct strbuf quoted = STRBUF_INIT;
|
156 | 180 | struct dirent *e;
|
157 | 181 | int res = 0, ret = 0, gone = 1, original_len = path->len, len;
|
158 |
| - unsigned char submodule_head[20]; |
159 | 182 | struct string_list dels = STRING_LIST_INIT_DUP;
|
160 | 183 |
|
161 | 184 | *dir_gone = 1;
|
162 | 185 |
|
163 |
| - if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && |
164 |
| - !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) { |
| 186 | + if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_git_repository(path)) { |
165 | 187 | if (!quiet) {
|
166 | 188 | quote_path_relative(path->buf, prefix, "ed);
|
167 | 189 | printf(dry_run ? _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
|
|
0 commit comments