status: get file statuses and run callback Add git_status_foreach() to run a callback on each file passing the path and a status value.
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
diff --git a/include/git2/status.h b/include/git2/status.h
index 97fff43..72912c5 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -37,6 +37,20 @@
*/
GIT_BEGIN_DECL
+#define GIT_STATUS_CURRENT 0
+/** Flags for index status */
+#define GIT_STATUS_INDEX_NEW (1 << 0)
+#define GIT_STATUS_INDEX_MODIFIED (1 << 1)
+#define GIT_STATUS_INDEX_DELETED (1 << 2)
+
+/** Flags for worktree status */
+#define GIT_STATUS_WT_NEW (1 << 3)
+#define GIT_STATUS_WT_MODIFIED (1 << 4)
+#define GIT_STATUS_WT_DELETED (1 << 5)
+
+// TODO Ignored files not handled yet
+#define GIT_STATUS_IGNORED (1 << 6)
+
/**
* Read a file from disk and fill a git_oid with the object id
* that the file would have if it were written to the Object
@@ -49,6 +63,19 @@ GIT_BEGIN_DECL
*/
GIT_EXTERN(int) git_status_hashfile(git_oid *out, const char *path);
+/**
+ * Gather file statuses and run a callback for each one.
+ *
+ * The callback is passed the path of the file, the status and the data pointer
+ * passed to this function. If the callback returns something other than
+ * GIT_SUCCESS, this function will return that value.
+ *
+ * @param repo a repository object
+ * @param callback the function to call on each file
+ * @return GIT_SUCCESS or the return value of the callback which did not return 0;
+ */
+GIT_EXTERN(int) git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/status.c b/src/status.c
index 694ed92..6e18b4e 100644
--- a/src/status.c
+++ b/src/status.c
@@ -27,6 +27,9 @@
#include "git2.h"
#include "fileops.h"
#include "hash.h"
+#include "vector.h"
+#include "tree.h"
+#include "git2/status.h"
int git_status_hashfile(git_oid *out, const char *path)
{
@@ -75,3 +78,193 @@ int git_status_hashfile(git_oid *out, const char *path)
return GIT_SUCCESS;
}
+
+struct status_entry {
+ char path[GIT_PATH_MAX];
+
+ git_index_time mtime;
+
+ git_oid head_oid;
+ git_oid index_oid;
+ git_oid wt_oid;
+
+ unsigned int status_flags:6;
+};
+
+static int status_cmp(const void *a, const void *b)
+{
+ const struct status_entry *entry_a = (const struct status_entry *)(a);
+ const struct status_entry *entry_b = (const struct status_entry *)(b);
+
+ return strcmp(entry_a->path, entry_b->path);
+}
+
+static int status_srch(const void *key, const void *array_member)
+{
+ const char *path = (const char *)key;
+ const struct status_entry *entry = (const struct status_entry *)(array_member);
+
+ return strcmp(path, entry->path);
+}
+
+static int find_status_entry(git_vector *entries, const char *path)
+{
+ git_vector_sort(entries);
+ return git_vector_bsearch2(entries, status_srch, path);
+}
+
+static struct status_entry *new_status_entry(git_vector *entries, const char *path)
+{
+ struct status_entry *e = git__malloc(sizeof(struct status_entry));
+ memset(e, 0x0, sizeof(struct status_entry));
+ git_vector_insert(entries, e);
+ strcpy(e->path, path);
+ return e;
+}
+
+static void recurse_tree_entries(git_tree *tree, git_vector *entries, char *path)
+{
+ int i, cnt, idx;
+ struct status_entry *e;
+ char file_path[GIT_PATH_MAX];
+ git_tree *subtree;
+
+ cnt = git_tree_entrycount(tree);
+ for (i = 0; i < cnt; ++i) {
+ const git_tree_entry *tree_entry = git_tree_entry_byindex(tree, i);
+
+ git_path_join(file_path, path, tree_entry->filename);
+
+ if (git_tree_lookup(&subtree, tree->object.repo, &tree_entry->oid) == GIT_SUCCESS) {
+ recurse_tree_entries(subtree, entries, file_path);
+ return;
+ }
+
+ if ((idx = find_status_entry(entries, file_path)) != GIT_ENOTFOUND)
+ e = (struct status_entry *)git_vector_get(entries, idx);
+ else
+ e = new_status_entry(entries, file_path);
+
+ git_oid_cpy(&e->head_oid, &tree_entry->oid);
+ }
+
+ git_tree_close(tree);
+}
+
+static int workdir_path_len;
+static int dirent_cb(void *state, char *full_path)
+{
+ int idx;
+ struct status_entry *e;
+ git_vector *entries = (git_vector *)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, dirent_cb, state);
+
+ if ((idx = find_status_entry(entries, file_path)) != GIT_ENOTFOUND) {
+ e = (struct status_entry *)git_vector_get(entries, idx);
+
+ 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 0;
+ }
+ } else {
+ e = new_status_entry(entries, file_path);
+ }
+
+ git_status_hashfile(&oid, full_path);
+ git_oid_cpy(&e->wt_oid, &oid);
+
+ return 0;
+}
+
+int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload)
+{
+ git_vector entries;
+ struct status_entry *e;
+ git_index *index;
+ unsigned int i, cnt;
+ git_index_entry *index_entry;
+ char temp_path[GIT_PATH_MAX];
+ git_oid zero;
+ int error;
+ git_tree *tree;
+
+ git_reference *head_ref, *resolved_head_ref;
+ git_commit *head_commit;
+
+ git_repository_index(&index, repo);
+
+ cnt = git_index_entrycount(index);
+ git_vector_init(&entries, cnt, status_cmp);
+ for (i = 0; i < cnt; ++i) {
+ index_entry = git_index_get(index, i);
+
+ e = new_status_entry(&entries, index_entry->path);
+ git_oid_cpy(&e->index_oid, &index_entry->oid);
+ e->mtime = index_entry->mtime;
+ }
+
+ 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));
+
+ // recurse through tree entries
+ git_commit_tree(&tree, head_commit);
+ recurse_tree_entries(tree, &entries, "");
+
+ workdir_path_len = strlen(repo->path_workdir);
+ strcpy(temp_path, repo->path_workdir);
+ git_futils_direach(temp_path, GIT_PATH_MAX, dirent_cb, &entries);
+
+ 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;
+ }
+
+
+ for (i = 0; i < entries.length; ++i) {
+ e = (struct status_entry *)git_vector_get(&entries, i);
+
+ if ((error = callback(e->path, e->status_flags, payload)) < GIT_SUCCESS)
+ return git__throw(error, "Failed to list statuses. User callback failed");
+ }
+
+ for (i = 0; i < entries.length; ++i) {
+ e = (struct status_entry *)git_vector_get(&entries, i);
+ free(e);
+ }
+ git_vector_free(&entries);
+
+ return GIT_SUCCESS;
+}
+
diff --git a/tests/t18-status.c b/tests/t18-status.c
index 6281a67..9c9533d 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -27,10 +27,10 @@
#include "fileops.h"
#include "git2/status.h"
-#define STATUS_FOLDER TEST_RESOURCES "/status/"
+#define STATUS_FOLDER TEST_RESOURCES "/status"
#define TEMP_STATUS_FOLDER TEMP_FOLDER "status"
-static const char *test_blob_oid = "9daeafb9864cf43055ae93beb0afd6c7d144bfa4";
+static const char *test_blob_oid = "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a";
BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
char current_workdir[GIT_PATH_MAX];
@@ -43,7 +43,7 @@ BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
git_path_join(path_statusfiles, path_statusfiles, TEMP_STATUS_FOLDER);
must_pass(copydir_recurs(STATUS_FOLDER, path_statusfiles));
- git_path_join(temp_path, path_statusfiles, "test.txt");
+ git_path_join(temp_path, path_statusfiles, "new_file");
must_pass(git_futils_exists(temp_path));
@@ -55,6 +55,96 @@ BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
git_futils_rmdir_r(TEMP_STATUS_FOLDER, 1);
END_TEST
+static const char *entry_paths[] = {
+ "current_file",
+ "file_deleted",
+ "modified_file",
+ "new_file",
+ "staged_changes",
+ "staged_changes_file_deleted",
+ "staged_changes_modified_file",
+ "staged_delete_file_deleted",
+ "staged_delete_modified_file",
+ "staged_new_file",
+ "staged_new_file_deleted_file",
+ "staged_new_file_modified_file",
+};
+static const int entry_statuses[] = {
+ GIT_STATUS_CURRENT,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_DELETED,
+ GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW,
+ GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_DELETED,
+ GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED,
+};
+#define ENTRY_COUNT 12
+
+static unsigned int get_expected_entry_status(const char *path)
+{
+ int i;
+
+ for (i = 0; i < ENTRY_COUNT; ++i)
+ if (!strcmp(path, entry_paths[i]))
+ return entry_statuses[i];
+
+ return (unsigned int)-1;
+}
+
+struct status_entry_counts {
+ int wrong_status_flags_count;
+ int entry_count;
+};
+
+static int status_cb(const char *path, unsigned int status_flags, void *payload)
+{
+ unsigned int expected_status_flags = get_expected_entry_status(path);
+ struct status_entry_counts *counts = (struct status_entry_counts *)payload;
+
+ counts->entry_count++;
+ if (status_flags != expected_status_flags)
+ counts->wrong_status_flags_count++;
+
+ return GIT_SUCCESS;
+}
+
+BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
+ 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;
+ struct status_entry_counts counts;
+
+ 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));
+
+ memset(&counts, 0x0, sizeof(struct status_entry_counts));
+ git_status_foreach(repo, status_cb, &counts);
+ must_be_true(counts.entry_count == ENTRY_COUNT);
+ must_be_true(counts.wrong_status_flags_count == 0);
+
+ git_repository_free(repo);
+
+ git_futils_rmdir_r(TEMP_STATUS_FOLDER, 1);
+END_TEST
+
BEGIN_SUITE(status)
ADD_TEST(file0);
+ ADD_TEST(statuscb0);
END_SUITE
+