Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git_status_file_at #4318

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/git2/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,16 @@ typedef enum {
* The `pathspec` is an array of path patterns to match (using
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pleas mind the preferred commit message style. First again, the area-prefix ("status" in this case), and then please also mind that we wrap the body at 80 characters. Also, please try to make the message more imperative instead arguing in place of the project, not yourself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now created PR #4422 to document our style. There's still two minor violations:

  1. the first letter after the area-prefix is usually lowercased
  2. the subject is some characters too long

Proposed: "status: add option to compare to trees other than head". You could add some reasoning in the commit message body, but that's only optional.

* fnmatch-style matching), or just an array of paths to match exactly if
* `GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH` is specified in the flags.
*
* The `baseline` is the tree to be used for comparison to the working directory
* and index; defaults to HEAD.
*/
typedef struct {
unsigned int version;
git_status_show_t show;
unsigned int flags;
git_strarray pathspec;
git_tree *baseline;
} git_status_options;

#define GIT_STATUS_OPTIONS_VERSION 1
Expand Down
19 changes: 12 additions & 7 deletions src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,16 @@ int git_status_list_new(
if ((error = git_repository__ensure_not_bare(repo, "status")) < 0 ||
(error = git_repository_index(&index, repo)) < 0)
return error;

/* if there is no HEAD, that's okay - we'll make an empty iterator */
if ((error = git_repository_head_tree(&head, repo)) < 0) {
if (error != GIT_ENOTFOUND && error != GIT_EUNBORNBRANCH)
goto done;
giterr_clear();

if (opts != NULL && opts->baseline != NULL) {
head = opts->baseline;
} else {
/* if there is no HEAD, that's okay - we'll make an empty iterator */
if ((error = git_repository_head_tree(&head, repo)) < 0) {
if (error != GIT_ENOTFOUND && error != GIT_EUNBORNBRANCH)
goto done;
giterr_clear();
}
}

/* refresh index from disk unless prevented */
Expand Down Expand Up @@ -377,7 +381,8 @@ int git_status_list_new(

*out = status;

git_tree_free(head);
if (opts == NULL || opts->baseline != head)
git_tree_free(head);
git_index_free(index);

return error;
Expand Down
31 changes: 31 additions & 0 deletions tests/status/worktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,3 +1280,34 @@ void test_status_worktree__with_directory_in_pathlist(void)
git_status_list_free(statuslist);
}

void test_status_worktree__at_head_parent(void)
{
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *statuslist;
git_tree *parent_tree;
const git_status_entry *status;

cl_git_mkfile("empty_standard_repo/file1", "ping");
stage_and_commit(repo, "file1");

cl_git_pass(git_repository_head_tree(&parent_tree, repo));

cl_git_mkfile("empty_standard_repo/file2", "pong");
stage_and_commit(repo, "file2");

cl_git_rewritefile("empty_standard_repo/file2", "pyng");

opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.baseline = parent_tree;
cl_git_pass(git_status_list_new(&statuslist, repo, &opts));

cl_assert_equal_sz(1, git_status_list_entrycount(statuslist));
status = git_status_byindex(statuslist, 0);
cl_assert(status != NULL);
cl_assert_equal_s("file2", status->index_to_workdir->old_file.path);
cl_assert_equal_i(GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW, status->status);

git_tree_free(parent_tree);
git_status_list_free(statuslist);
}