Commit 37ee70fa by Russell Belfer

Implement GIT_STATUS_OPT_EXCLUDE_SUBMODULES

This option has been sitting unimplemented for a while, so I
finally went through and implemented it along with some tests.

As part of this, I improved the implementation of
GIT_DIFF_IGNORE_SUBMODULES so it be more diligent about avoiding
extra work and about leaving off delta records for submodules to
the greatest extent possible (though it may include them still
if you are request TYPECHANGE records).
parent 0c289dd7
...@@ -51,7 +51,7 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); ...@@ -51,7 +51,7 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
* *
* @param out oid structure the result is written into. * @param out oid structure the result is written into.
* @param str input hex string; must be at least 4 characters * @param str input hex string; must be at least 4 characters
* long and null-terminated. * long and null-terminated.
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str); GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
......
...@@ -133,7 +133,8 @@ typedef enum { ...@@ -133,7 +133,8 @@ typedef enum {
* *
* Calling `git_status_foreach()` is like calling the extended version * Calling `git_status_foreach()` is like calling the extended version
* with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED, * with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED,
* and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS. * and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS. Those options are bundled
* together as `GIT_STATUS_OPT_DEFAULTS` if you want them as a baseline.
*/ */
typedef enum { typedef enum {
GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1u << 0), GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1u << 0),
...@@ -145,6 +146,11 @@ typedef enum { ...@@ -145,6 +146,11 @@ typedef enum {
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS = (1u << 6), GIT_STATUS_OPT_RECURSE_IGNORED_DIRS = (1u << 6),
} git_status_opt_t; } git_status_opt_t;
#define GIT_STATUS_OPT_DEFAULTS \
(GIT_STATUS_OPT_INCLUDE_IGNORED | \
GIT_STATUS_OPT_INCLUDE_UNTRACKED | \
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS)
/** /**
* Options to control how `git_status_foreach_ext()` will issue callbacks. * Options to control how `git_status_foreach_ext()` will issue callbacks.
* *
......
...@@ -73,6 +73,10 @@ static int diff_delta__from_one( ...@@ -73,6 +73,10 @@ static int diff_delta__from_one(
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED)) DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
return 0; return 0;
if (entry->mode == GIT_FILEMODE_COMMIT &&
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
return 0;
if (!git_pathspec_match_path( if (!git_pathspec_match_path(
&diff->pathspec, entry->path, &diff->pathspec, entry->path,
DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH), DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH),
...@@ -131,6 +135,11 @@ static int diff_delta__from_two( ...@@ -131,6 +135,11 @@ static int diff_delta__from_two(
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED)) DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
return 0; return 0;
if (old_entry->mode == GIT_FILEMODE_COMMIT &&
new_entry->mode == GIT_FILEMODE_COMMIT &&
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
return 0;
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
uint32_t temp_mode = old_mode; uint32_t temp_mode = old_mode;
const git_index_entry *temp_entry = old_entry; const git_index_entry *temp_entry = old_entry;
...@@ -548,6 +557,11 @@ static int maybe_modified( ...@@ -548,6 +557,11 @@ static int maybe_modified(
} }
} }
/* if mode is GITLINK and submodules are ignored, then skip */
else if (S_ISGITLINK(nmode) &&
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
status = GIT_DELTA_UNMODIFIED;
/* if we got here and decided that the files are modified, but we /* if we got here and decided that the files are modified, but we
* haven't calculated the OID of the new item, then calculate it now * haven't calculated the OID of the new item, then calculate it now
*/ */
......
...@@ -80,22 +80,37 @@ static unsigned int workdir_delta2status(git_delta_t workdir_status) ...@@ -80,22 +80,37 @@ static unsigned int workdir_delta2status(git_delta_t workdir_status)
typedef struct { typedef struct {
git_status_cb cb; git_status_cb cb;
void *payload; void *payload;
const git_status_options *opts;
} status_user_callback; } status_user_callback;
static int status_invoke_cb( static int status_invoke_cb(
git_diff_delta *i2h, git_diff_delta *w2i, void *payload) git_diff_delta *h2i, git_diff_delta *i2w, void *payload)
{ {
status_user_callback *usercb = payload; status_user_callback *usercb = payload;
const char *path = NULL; const char *path = NULL;
unsigned int status = 0; unsigned int status = 0;
if (w2i) { if (i2w) {
path = w2i->old_file.path; path = i2w->old_file.path;
status |= workdir_delta2status(w2i->status); status |= workdir_delta2status(i2w->status);
} }
if (i2h) { if (h2i) {
path = i2h->old_file.path; path = h2i->old_file.path;
status |= index_delta2status(i2h->status); status |= index_delta2status(h2i->status);
}
/* if excluding submodules and this is a submodule everywhere */
if (usercb->opts &&
(usercb->opts->flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0)
{
bool in_tree = (h2i && h2i->status != GIT_DELTA_ADDED);
bool in_index = (h2i && h2i->status != GIT_DELTA_DELETED);
bool in_wd = (i2w && i2w->status != GIT_DELTA_DELETED);
if ((!in_tree || h2i->old_file.mode == GIT_FILEMODE_COMMIT) &&
(!in_index || h2i->new_file.mode == GIT_FILEMODE_COMMIT) &&
(!in_wd || i2w->new_file.mode == GIT_FILEMODE_COMMIT))
return 0;
} }
return usercb->cb(path, status, usercb->payload); return usercb->cb(path, status, usercb->payload);
...@@ -109,7 +124,7 @@ int git_status_foreach_ext( ...@@ -109,7 +124,7 @@ int git_status_foreach_ext(
{ {
int err = 0; int err = 0;
git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT; git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT;
git_diff_list *idx2head = NULL, *wd2idx = NULL; git_diff_list *head2idx = NULL, *idx2wd = NULL;
git_tree *head = NULL; git_tree *head = NULL;
git_status_show_t show = git_status_show_t show =
opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR; opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
...@@ -144,33 +159,40 @@ int git_status_foreach_ext( ...@@ -144,33 +159,40 @@ int git_status_foreach_ext(
diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH; diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
if ((opts->flags & GIT_STATUS_OPT_RECURSE_IGNORED_DIRS) != 0) if ((opts->flags & GIT_STATUS_OPT_RECURSE_IGNORED_DIRS) != 0)
diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_IGNORED_DIRS; diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_IGNORED_DIRS;
if ((opts->flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0)
diffopt.flags = diffopt.flags | GIT_DIFF_IGNORE_SUBMODULES;
if (show != GIT_STATUS_SHOW_WORKDIR_ONLY && if (show != GIT_STATUS_SHOW_WORKDIR_ONLY) {
(err = git_diff_tree_to_index(&idx2head, repo, head, NULL, &diffopt)) < 0) err = git_diff_tree_to_index(&head2idx, repo, head, NULL, &diffopt);
goto cleanup; if (err < 0)
goto cleanup;
}
if (show != GIT_STATUS_SHOW_INDEX_ONLY && if (show != GIT_STATUS_SHOW_INDEX_ONLY) {
(err = git_diff_index_to_workdir(&wd2idx, repo, NULL, &diffopt)) < 0) err = git_diff_index_to_workdir(&idx2wd, repo, NULL, &diffopt);
goto cleanup; if (err < 0)
goto cleanup;
}
usercb.cb = cb; usercb.cb = cb;
usercb.payload = payload; usercb.payload = payload;
usercb.opts = opts;
if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) { if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) {
if ((err = git_diff__paired_foreach( if ((err = git_diff__paired_foreach(
idx2head, NULL, status_invoke_cb, &usercb)) < 0) head2idx, NULL, status_invoke_cb, &usercb)) < 0)
goto cleanup; goto cleanup;
git_diff_list_free(idx2head); git_diff_list_free(head2idx);
idx2head = NULL; head2idx = NULL;
} }
err = git_diff__paired_foreach(idx2head, wd2idx, status_invoke_cb, &usercb); err = git_diff__paired_foreach(head2idx, idx2wd, status_invoke_cb, &usercb);
cleanup: cleanup:
git_tree_free(head); git_tree_free(head);
git_diff_list_free(idx2head); git_diff_list_free(head2idx);
git_diff_list_free(wd2idx); git_diff_list_free(idx2wd);
if (err == GIT_EUSER) if (err == GIT_EUSER)
giterr_clear(); giterr_clear();
......
...@@ -694,7 +694,7 @@ int git_submodule_open( ...@@ -694,7 +694,7 @@ int git_submodule_open(
git_buf_free(&path); git_buf_free(&path);
/* if we have opened the submodule successfully, let's grab the HEAD OID */ /* if we have opened the submodule successfully, let's grab the HEAD OID */
if (!error && !(submodule->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID)) { if (!error) {
if (!git_reference_name_to_id( if (!git_reference_name_to_id(
&submodule->wd_oid, *subrepo, GIT_HEAD_FILE)) &submodule->wd_oid, *subrepo, GIT_HEAD_FILE))
submodule->flags |= GIT_SUBMODULE_STATUS__WD_OID_VALID; submodule->flags |= GIT_SUBMODULE_STATUS__WD_OID_VALID;
......
...@@ -245,10 +245,7 @@ void test_status_ignore__subdirectories_recursion(void) ...@@ -245,10 +245,7 @@ void test_status_ignore__subdirectories_recursion(void)
GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
}; };
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED | opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
g_repo = cl_git_sandbox_init("empty_standard_repo"); g_repo = cl_git_sandbox_init("empty_standard_repo");
......
...@@ -71,31 +71,34 @@ static unsigned int expected_status[] = { ...@@ -71,31 +71,34 @@ static unsigned int expected_status[] = {
GIT_STATUS_WT_NEW GIT_STATUS_WT_NEW
}; };
static int static int cb_status__match(const char *p, unsigned int s, void *payload)
cb_status__match(const char *p, unsigned int s, void *payload)
{ {
volatile int *index = (int *)payload; status_entry_counts *counts = payload;
int idx = counts->entry_count++;
cl_assert_equal_s(expected_files[*index], p); cl_assert_equal_s(counts->expected_paths[idx], p);
cl_assert(expected_status[*index] == s); cl_assert(counts->expected_statuses[idx] == s);
(*index)++;
return 0; return 0;
} }
void test_status_submodules__1(void) void test_status_submodules__1(void)
{ {
int index = 0; status_entry_counts counts;
cl_assert(git_path_isdir("submodules/.git")); cl_assert(git_path_isdir("submodules/.git"));
cl_assert(git_path_isdir("submodules/testrepo/.git")); cl_assert(git_path_isdir("submodules/testrepo/.git"));
cl_assert(git_path_isfile("submodules/.gitmodules")); cl_assert(git_path_isfile("submodules/.gitmodules"));
memset(&counts, 0, sizeof(counts));
counts.expected_paths = expected_files;
counts.expected_statuses = expected_status;
cl_git_pass( cl_git_pass(
git_status_foreach(g_repo, cb_status__match, &index) git_status_foreach(g_repo, cb_status__match, &counts)
); );
cl_assert_equal_i(6, index); cl_assert_equal_i(6, counts.entry_count);
} }
void test_status_submodules__single_file(void) void test_status_submodules__single_file(void)
...@@ -104,3 +107,62 @@ void test_status_submodules__single_file(void) ...@@ -104,3 +107,62 @@ void test_status_submodules__single_file(void)
cl_git_pass( git_status_file(&status, g_repo, "testrepo") ); cl_git_pass( git_status_file(&status, g_repo, "testrepo") );
cl_assert(!status); cl_assert(!status);
} }
void test_status_submodules__moved_head(void)
{
git_submodule *sm;
git_repository *smrepo;
git_oid oid;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts;
static const char *expected_files_with_sub[] = {
".gitmodules",
"added",
"deleted",
"ignored",
"modified",
"testrepo",
"untracked"
};
static unsigned int expected_status_with_sub[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_IGNORED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW
};
cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
cl_git_pass(git_submodule_open(&smrepo, sm));
/* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
cl_git_pass(
git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_repository_set_head_detached(smrepo, &oid));
/* 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;
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;
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment