Commit 3af6b34a76cd953f485b8733981493b65becf59e

Jason Penny 2011-06-22T18:31:20

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.

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
+