status: get status for single file Add git_status_file to be able to retrieve status of single file by supplying a path.
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
diff --git a/include/git2/status.h b/include/git2/status.h
index 72912c5..b8d4734 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -76,6 +76,16 @@ GIT_EXTERN(int) git_status_hashfile(git_oid *out, const char *path);
*/
GIT_EXTERN(int) git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload);
+/**
+ * Get file status for a single file
+ *
+ * @param status_flags the status value
+ * @param repo a repository object
+ * @param path the file to retrieve status for, rooted at the repo's workdir
+ * @return GIT_SUCCESS
+ */
+GIT_EXTERN(int) git_status_file(unsigned int *status_flags, git_repository *repo, const char *path);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/status.c b/src/status.c
index 6e18b4e..60310c5 100644
--- a/src/status.c
+++ b/src/status.c
@@ -117,7 +117,8 @@ static struct status_entry *new_status_entry(git_vector *entries, const char *pa
{
struct status_entry *e = git__malloc(sizeof(struct status_entry));
memset(e, 0x0, sizeof(struct status_entry));
- git_vector_insert(entries, e);
+ if (entries != NULL)
+ git_vector_insert(entries, e);
strcpy(e->path, path);
return e;
}
@@ -187,6 +188,64 @@ static int dirent_cb(void *state, char *full_path)
return 0;
}
+static int single_dirent_cb(void *state, char *full_path)
+{
+ struct status_entry *e = *(struct status_entry **)(state);
+ char *file_path = full_path + workdir_path_len;
+ struct stat filest;
+ git_oid oid;
+
+ if ((git_futils_isdir(full_path) == GIT_SUCCESS) && (!strcmp(".git", file_path)))
+ return 0;
+
+ if (git_futils_isdir(full_path) == GIT_SUCCESS)
+ return git_futils_direach(full_path, GIT_PATH_MAX, single_dirent_cb, state);
+
+ if (!strcmp(file_path, e->path)) {
+ if (p_stat(full_path, &filest) < 0)
+ return git__throw(GIT_EOSERR, "Failed to read file %s", full_path);
+
+ if (e->mtime.seconds == (git_time_t)filest.st_mtime) {
+ git_oid_cpy(&e->wt_oid, &e->index_oid);
+ return 1;
+ }
+
+ git_status_hashfile(&oid, full_path);
+ git_oid_cpy(&e->wt_oid, &oid);
+ return 1;
+ }
+
+ return 0;
+}
+
+static int set_status_flags(struct status_entry *e)
+{
+ git_oid zero;
+ int head_zero, index_zero, wt_zero;
+
+ memset(&zero, 0x0, sizeof(git_oid));
+
+ head_zero = git_oid_cmp(&zero, &e->head_oid);
+ index_zero = git_oid_cmp(&zero, &e->index_oid);
+ wt_zero = git_oid_cmp(&zero, &e->wt_oid);
+
+ if (head_zero == 0 && index_zero != 0)
+ e->status_flags |= GIT_STATUS_INDEX_NEW;
+ else if (index_zero == 0 && head_zero != 0)
+ e->status_flags |= GIT_STATUS_INDEX_DELETED;
+ else if (git_oid_cmp(&e->head_oid, &e->index_oid) != 0)
+ e->status_flags |= GIT_STATUS_INDEX_MODIFIED;
+
+ if (index_zero == 0 && wt_zero != 0)
+ e->status_flags |= GIT_STATUS_WT_NEW;
+ else if (wt_zero == 0 && index_zero != 0)
+ e->status_flags |= GIT_STATUS_WT_DELETED;
+ else if (git_oid_cmp(&e->index_oid, &e->wt_oid) != 0)
+ e->status_flags |= GIT_STATUS_WT_MODIFIED;
+
+ return GIT_SUCCESS;
+}
+
int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload)
{
git_vector entries;
@@ -229,26 +288,9 @@ int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsig
memset(&zero, 0x0, sizeof(git_oid));
for (i = 0; i < entries.length; ++i) {
- int head_zero, index_zero, wt_zero;
e = (struct status_entry *)git_vector_get(&entries, i);
- head_zero = git_oid_cmp(&zero, &e->head_oid);
- index_zero = git_oid_cmp(&zero, &e->index_oid);
- wt_zero = git_oid_cmp(&zero, &e->wt_oid);
-
- if (head_zero == 0 && index_zero != 0)
- e->status_flags |= GIT_STATUS_INDEX_NEW;
- else if (index_zero == 0 && head_zero != 0)
- e->status_flags |= GIT_STATUS_INDEX_DELETED;
- else if (git_oid_cmp(&e->head_oid, &e->index_oid) != 0)
- e->status_flags |= GIT_STATUS_INDEX_MODIFIED;
-
- if (index_zero == 0 && wt_zero != 0)
- e->status_flags |= GIT_STATUS_WT_NEW;
- else if (wt_zero == 0 && index_zero != 0)
- e->status_flags |= GIT_STATUS_WT_DELETED;
- else if (git_oid_cmp(&e->index_oid, &e->wt_oid) != 0)
- e->status_flags |= GIT_STATUS_WT_MODIFIED;
+ set_status_flags(e);
}
@@ -268,3 +310,54 @@ int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsig
return GIT_SUCCESS;
}
+int git_status_file(unsigned int *status_flags, git_repository *repo, const char *path)
+{
+ struct status_entry *e;
+ git_index *index;
+ git_index_entry *index_entry;
+ char temp_path[GIT_PATH_MAX];
+ int idx;
+ git_tree *tree;
+ git_reference *head_ref, *resolved_head_ref;
+ git_commit *head_commit;
+ const git_tree_entry *tree_entry;
+
+ assert(status_flags);
+
+ e = new_status_entry(NULL, path);
+
+ // Find file in Index
+ git_repository_index(&index, repo);
+ idx = git_index_find(index, path);
+ if (idx >= 0) {
+ index_entry = git_index_get(index, idx);
+ git_oid_cpy(&e->index_oid, &index_entry->oid);
+ e->mtime = index_entry->mtime;
+ }
+
+ // Find file in HEAD
+ git_reference_lookup(&head_ref, repo, GIT_HEAD_FILE);
+ git_reference_resolve(&resolved_head_ref, head_ref);
+
+ git_commit_lookup(&head_commit, repo, git_reference_oid(resolved_head_ref));
+
+ git_commit_tree(&tree, head_commit);
+ // TODO: handle subdirectories by walking into subtrees
+ tree_entry = git_tree_entry_byname(tree, path);
+ if (tree_entry != NULL) {
+ git_oid_cpy(&e->head_oid, &tree_entry->oid);
+ }
+ git_tree_close(tree);
+
+ // Find file in Workdir
+ workdir_path_len = strlen(repo->path_workdir);
+ strcpy(temp_path, repo->path_workdir);
+ git_futils_direach(temp_path, GIT_PATH_MAX, single_dirent_cb, &e);
+
+ set_status_flags(e);
+ *status_flags = e->status_flags;
+
+ free(e);
+
+ return GIT_SUCCESS;
+}
diff --git a/tests/t18-status.c b/tests/t18-status.c
index 9c9533d..f5899d4 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -69,7 +69,7 @@ static const char *entry_paths[] = {
"staged_new_file_deleted_file",
"staged_new_file_modified_file",
};
-static const int entry_statuses[] = {
+static const unsigned int entry_statuses[] = {
GIT_STATUS_CURRENT,
GIT_STATUS_WT_DELETED,
GIT_STATUS_WT_MODIFIED,
@@ -143,8 +143,40 @@ BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
git_futils_rmdir_r(TEMP_STATUS_FOLDER, 1);
END_TEST
+BEGIN_TEST(singlestatus0, "test retrieving status for single file")
+ char current_workdir[GIT_PATH_MAX];
+ char path_statusfiles[GIT_PATH_MAX];
+ char temp_path[GIT_PATH_MAX];
+ char gitted[GIT_PATH_MAX];
+ git_repository *repo;
+ unsigned int status_flags;
+ int i;
+
+ must_pass(p_getcwd(current_workdir, sizeof(current_workdir)));
+ strcpy(path_statusfiles, current_workdir);
+ git_path_join(path_statusfiles, path_statusfiles, TEMP_STATUS_FOLDER);
+
+ must_pass(copydir_recurs(STATUS_FOLDER, path_statusfiles));
+
+ git_path_join(gitted, path_statusfiles, ".gitted");
+ git_path_join(temp_path, path_statusfiles, ".git");
+ copydir_recurs(gitted, temp_path);
+ git_futils_rmdir_r(gitted, 1);
+ must_pass(git_repository_open(&repo, temp_path));
+
+ for (i = 0; i < ENTRY_COUNT; ++i) {
+ must_pass(git_status_file(&status_flags, repo, entry_paths[i]));
+ must_be_true(status_flags == entry_statuses[i]);
+ }
+
+ git_repository_free(repo);
+
+ git_futils_rmdir_r(TEMP_STATUS_FOLDER, 1);
+END_TEST
+
BEGIN_SUITE(status)
ADD_TEST(file0);
ADD_TEST(statuscb0);
+ ADD_TEST(singlestatus0);
END_SUITE