Commit 20361b2f6988c994a09c53ad563f847c200948ea

Jason Penny 2011-06-23T18:51:22

status: get status for single file Add git_status_file to be able to retrieve status of single file by supplying a path.

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