Deploy GIT_STATUS_OPTIONS_INIT
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
diff --git a/src/stash.c b/src/stash.c
index 261d3b1..98c7a7c 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -471,9 +471,8 @@ static int ensure_there_are_changes_to_stash(
bool include_ignored_files)
{
int error;
- git_status_options opts;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
- memset(&opts, 0, sizeof(opts));
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
if (include_untracked_files)
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
diff --git a/src/status.c b/src/status.c
index ca500c5..f23f40e 100644
--- a/src/status.c
+++ b/src/status.c
@@ -101,6 +101,18 @@ static int status_invoke_cb(
return usercb->cb(path, status, usercb->payload);
}
+static bool options_have_valid_version(const git_status_options *opts)
+{
+ if (!opts)
+ return true;
+
+ if (opts->version > 0 && opts->version <= GIT_REMOTE_CALLBACKS_VERSION)
+ return true;
+
+ giterr_set(GITERR_INVALID, "Invalid version %d for git_repository_init_options", opts->version);
+ return false;
+}
+
int git_status_foreach_ext(
git_repository *repo,
const git_status_options *opts,
@@ -117,6 +129,9 @@ int git_status_foreach_ext(
assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR);
+ if (!options_have_valid_version(opts))
+ return -1;
+
if (show != GIT_STATUS_SHOW_INDEX_ONLY &&
(err = git_repository__ensure_not_bare(repo, "status")) < 0)
return err;
@@ -180,9 +195,8 @@ int git_status_foreach(
git_status_cb callback,
void *payload)
{
- git_status_options opts;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
- memset(&opts, 0, sizeof(opts));
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNTRACKED |
@@ -223,16 +237,14 @@ int git_status_file(
const char *path)
{
int error;
- git_status_options opts;
- struct status_file_info sfi;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+ struct status_file_info sfi = {0};
assert(status_flags && repo && path);
- memset(&sfi, 0, sizeof(sfi));
if ((sfi.expected = git__strdup(path)) == NULL)
return -1;
- memset(&opts, 0, sizeof(opts));
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNTRACKED |
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 27f9d85..e2e4aaf 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -180,9 +180,6 @@ void test_status_ignore__subdirectories(void)
{
status_entry_single st;
int ignored;
- git_status_options opts;
-
- GIT_UNUSED(opts);
g_repo = cl_git_sandbox_init("empty_standard_repo");
@@ -216,11 +213,6 @@ void test_status_ignore__subdirectories(void)
cl_git_mkfile(
"empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!");
- opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
- opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
- GIT_STATUS_OPT_INCLUDE_UNTRACKED |
- GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
-
memset(&st, 0, sizeof(st));
cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
cl_assert_equal_i(2, st.count);
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index 838a043..6f5fb3c 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -111,7 +111,7 @@ void test_status_worktree__swap_subdir_and_file(void)
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
git_index *index;
- git_status_options opts;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
bool ignore_case;
cl_git_pass(git_repository_index(&index, repo));
@@ -133,7 +133,6 @@ void test_status_worktree__swap_subdir_and_file(void)
counts.expected_paths = ignore_case ? entry_paths3_icase : entry_paths3;
counts.expected_statuses = ignore_case ? entry_statuses3_icase : entry_statuses3;
- memset(&opts, 0, sizeof(opts));
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_INCLUDE_IGNORED;
@@ -150,7 +149,7 @@ void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
- git_status_options opts;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
/* first alter the contents of the worktree */
cl_git_pass(p_rename("status/current_file", "status/swap"));
@@ -167,7 +166,6 @@ void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void)
counts.expected_paths = entry_paths4;
counts.expected_statuses = entry_statuses4;
- memset(&opts, 0, sizeof(opts));
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
/* TODO: set pathspec to "current_file" eventually */
@@ -691,7 +689,7 @@ void test_status_worktree__filemode_changes(void)
{
git_repository *repo = cl_git_sandbox_init("filemodes");
status_entry_counts counts;
- git_status_options opts;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_config *cfg;
/* overwrite stored filemode with platform appropriate value */
@@ -708,7 +706,6 @@ void test_status_worktree__filemode_changes(void)
filemode_statuses[i] = GIT_STATUS_CURRENT;
}
- memset(&opts, 0, sizeof(opts));
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED;
@@ -746,7 +743,7 @@ static int cb_status__expected_path(const char *p, unsigned int s, void *payload
void test_status_worktree__disable_pathspec_match(void)
{
git_repository *repo;
- git_status_options opts;
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
char *file_with_bracket = "LICENSE[1].md",
*imaginary_file_with_bracket = "LICENSE[1-2].md";
@@ -754,7 +751,6 @@ void test_status_worktree__disable_pathspec_match(void)
cl_git_mkfile("pathspec/LICENSE[1].md", "screaming bracket\n");
cl_git_mkfile("pathspec/LICENSE1.md", "no bracket\n");
- memset(&opts, 0, sizeof(opts));
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
opts.pathspec.count = 1;