Merge pull request #2233 from libgit2/rb/fix-untracked-repo-status Skip untracked contained repo contents even with gitlink files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
diff --git a/src/diff.c b/src/diff.c
index 25c5937..484273f 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -880,8 +880,10 @@ static int handle_unmatched_new_item(
git_buf *full = NULL;
if (git_iterator_current_workdir_path(&full, info->new_iter) < 0)
return -1;
- if (full && git_path_contains_dir(full, DOT_GIT))
+ if (full && git_path_contains(full, DOT_GIT)) {
+ /* TODO: warning if not a valid git repository */
recurse_into_dir = false;
+ }
}
/* still have to look into untracked directories to match core git -
diff --git a/tests/status/status_helpers.h b/tests/status/status_helpers.h
index f1f009e..242076c 100644
--- a/tests/status/status_helpers.h
+++ b/tests/status/status_helpers.h
@@ -8,9 +8,19 @@ typedef struct {
const unsigned int* expected_statuses;
const char** expected_paths;
int expected_entry_count;
+ const char *file;
+ int line;
bool debug;
} status_entry_counts;
+#define status_counts_init(counts, paths, statuses) do { \
+ memset(&(counts), 0, sizeof(counts)); \
+ (counts).expected_statuses = (statuses); \
+ (counts).expected_paths = (paths); \
+ (counts).file = __FILE__; \
+ (counts).line = __LINE__; \
+ } while (0)
+
/* cb_status__normal takes payload of "status_entry_counts *" */
extern int cb_status__normal(
diff --git a/tests/status/submodules.c b/tests/status/submodules.c
index 8575f9f..6d0d63a 100644
--- a/tests/status/submodules.c
+++ b/tests/status/submodules.c
@@ -72,8 +72,15 @@ static int cb_status__match(const char *p, unsigned int s, void *payload)
status_entry_counts *counts = payload;
int idx = counts->entry_count++;
- cl_assert_equal_s(counts->expected_paths[idx], p);
- cl_assert(counts->expected_statuses[idx] == s);
+ clar__assert_equal(
+ counts->file, counts->line,
+ "Status path mismatch", 1,
+ "%s", counts->expected_paths[idx], p);
+
+ clar__assert_equal(
+ counts->file, counts->line,
+ "Status code mismatch", 1,
+ "%o", counts->expected_statuses[idx], s);
return 0;
}
@@ -88,13 +95,9 @@ void test_status_submodules__1(void)
cl_assert(git_path_isdir("submodules/testrepo/.git"));
cl_assert(git_path_isfile("submodules/.gitmodules"));
- memset(&counts, 0, sizeof(counts));
- counts.expected_paths = expected_files;
- counts.expected_statuses = expected_status;
+ status_counts_init(counts, expected_files, expected_status);
- cl_git_pass(
- git_status_foreach(g_repo, cb_status__match, &counts)
- );
+ cl_git_pass( git_status_foreach(g_repo, cb_status__match, &counts) );
cl_assert_equal_i(6, counts.entry_count);
}
@@ -146,24 +149,19 @@ void test_status_submodules__moved_head(void)
/* first do a normal status, which should now include the submodule */
- memset(&counts, 0, sizeof(counts));
- counts.expected_paths = expected_files_with_sub;
- counts.expected_statuses = expected_status_with_sub;
-
opts.flags = GIT_STATUS_OPT_DEFAULTS;
+ status_counts_init(
+ counts, expected_files_with_sub, expected_status_with_sub);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* try again with EXCLUDE_SUBMODULES which should skip it */
- memset(&counts, 0, sizeof(counts));
- counts.expected_paths = expected_files;
- counts.expected_statuses = expected_status;
-
opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
+ status_counts_init(counts, expected_files, expected_status);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
@@ -201,24 +199,19 @@ void test_status_submodules__dirty_workdir_only(void)
/* first do a normal status, which should now include the submodule */
- memset(&counts, 0, sizeof(counts));
- counts.expected_paths = expected_files_with_sub;
- counts.expected_statuses = expected_status_with_sub;
-
opts.flags = GIT_STATUS_OPT_DEFAULTS;
+ status_counts_init(
+ counts, expected_files_with_sub, expected_status_with_sub);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* try again with EXCLUDE_SUBMODULES which should skip it */
- memset(&counts, 0, sizeof(counts));
- counts.expected_paths = expected_files;
- counts.expected_statuses = expected_status;
-
opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
+ status_counts_init(counts, expected_files, expected_status);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
@@ -240,3 +233,159 @@ void test_status_submodules__uninitialized(void)
git_repository_free(cloned_repo);
cl_git_sandbox_cleanup();
}
+
+void test_status_submodules__contained_untracked_repo(void)
+{
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+ status_entry_counts counts;
+ git_repository *contained;
+ static const char *expected_files_not_ignored[] = {
+ ".gitmodules",
+ "added",
+ "deleted",
+ "modified",
+ "untracked"
+ };
+ static unsigned int expected_status_not_ignored[] = {
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_WT_NEW,
+ };
+ static const char *expected_files_with_untracked[] = {
+ ".gitmodules",
+ "added",
+ "deleted",
+ "dir/file.md",
+ "modified",
+ "untracked"
+ };
+ static const char *expected_files_with_untracked_dir[] = {
+ ".gitmodules",
+ "added",
+ "deleted",
+ "dir/",
+ "modified",
+ "untracked"
+ };
+ static unsigned int expected_status_with_untracked[] = {
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_WT_NEW
+ };
+
+ g_repo = setup_fixture_submodules();
+
+ /* skip empty directory */
+
+ cl_must_pass(p_mkdir("submodules/dir", 0777));
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+
+ status_counts_init(
+ counts, expected_files_not_ignored, expected_status_not_ignored);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(5, counts.entry_count);
+
+ /* still skipping because empty == ignored */
+
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ status_counts_init(
+ counts, expected_files_not_ignored, expected_status_not_ignored);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(5, counts.entry_count);
+
+ /* find non-ignored contents of directory */
+
+ cl_git_mkfile("submodules/dir/file.md", "hello");
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ status_counts_init(
+ counts, expected_files_with_untracked, expected_status_with_untracked);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(6, counts.entry_count);
+
+ /* but skip if all content is ignored */
+
+ cl_git_append2file("submodules/.git/info/exclude", "\n*.md\n\n");
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ status_counts_init(
+ counts, expected_files_not_ignored, expected_status_not_ignored);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(5, counts.entry_count);
+
+ /* same is true if it contains a git link */
+
+ cl_git_mkfile("submodules/dir/.git", "gitlink: ../.git");
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ status_counts_init(
+ counts, expected_files_not_ignored, expected_status_not_ignored);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(5, counts.entry_count);
+
+ /* but if it contains tracked files, it should just show up as a
+ * directory and exclude the files in it
+ */
+
+ cl_git_mkfile("submodules/dir/another_file", "hello");
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ status_counts_init(
+ counts, expected_files_with_untracked_dir,
+ expected_status_with_untracked);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(6, counts.entry_count);
+
+ /* that applies to a git repo with a .git directory too */
+
+ cl_must_pass(p_unlink("submodules/dir/.git"));
+ cl_git_pass(git_repository_init(&contained, "submodules/dir", false));
+ git_repository_free(contained);
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ status_counts_init(
+ counts, expected_files_with_untracked_dir,
+ expected_status_with_untracked);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(6, counts.entry_count);
+
+ /* same result even if we don't recurse into subdirectories */
+
+ opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+
+ status_counts_init(
+ counts, expected_files_with_untracked_dir,
+ expected_status_with_untracked);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(6, counts.entry_count);
+
+ /* and if we remove the untracked file, it goes back to ignored */
+
+ cl_must_pass(p_unlink("submodules/dir/another_file"));
+
+ status_counts_init(
+ counts, expected_files_not_ignored, expected_status_not_ignored);
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__match, &counts));
+ cl_assert_equal_i(5, counts.entry_count);
+}