Skip to content

Commit

Permalink
repository: add message for merge, fix cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmn committed Jul 30, 2012
1 parent 5a0722b commit 23b6879
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,15 +1141,15 @@ static ssize_t state_revert(char *buffer, size_t len, git_repository *repo)
return -1;
}

static ssize_t state_none(char *buffer, size_t len, git_repository *repo)
static ssize_t state_fromfile(char *buffer, size_t len, git_repository *repo, const char *filename)
{
git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
struct stat st;
ssize_t size;
int error;

/* FIXME: use a #define for this */
if (git_buf_joinpath(&path, repo->path_repository, "COMMIT_EDITMSG") < 0)
if (git_buf_joinpath(&path, repo->path_repository, filename) < 0)
return -1;

error = p_stat(git_buf_cstr(&path), &st);
Expand Down Expand Up @@ -1181,14 +1181,24 @@ static ssize_t state_none(char *buffer, size_t len, git_repository *repo)
ssize_t git_repository_message(char *buffer, size_t len, git_repository *repo)
{
int state;
ssize_t ret;

state = git_repository_state(repo);
switch (state) {
case GIT_REPOSITORY_STATE_REVERT:
return state_revert(buffer, len, repo);

case GIT_REPOSITORY_STATE_CHERRY_PICK:
ret = state_fromfile(buffer, len, repo, "MERGE_MSG");
if (ret != GIT_ENOTFOUND)
return ret;
return state_fromfile(buffer, len, repo, "COMMIT_EDITMSG");

case GIT_REPOSITORY_STATE_MERGE:
return state_fromfile(buffer, len, repo, "MERGE_MSG");

case GIT_REPOSITORY_STATE_NONE:
return state_none(buffer, len, repo);
return state_fromfile(buffer, len, repo, "COMMIT_EDITMSG");
default:
giterr_set(GITERR_REPOSITORY, "I don't know about this state, sorry");
return -1;
Expand Down
19 changes: 18 additions & 1 deletion tests-clar/repo/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ void test_repo_state__none(void)

void test_repo_state__merge(void)
{
const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n";
ssize_t len;

/* Then it should recognise that .git/MERGE_HEAD and friends mean their respective states */
cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), GIT_MERGE_HEAD_FILE));
cl_git_mkfile(git_buf_cstr(&_path), "dummy");
cl_assert_equal_i(GIT_REPOSITORY_STATE_MERGE, git_repository_state(_repo));

cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG"));
cl_git_mkfile(git_buf_cstr(&_path), expected);
cl_assert_equal_i(GIT_REPOSITORY_STATE_MERGE, git_repository_state(_repo));

len = git_repository_message(NULL, 0, _repo);
cl_assert(len > 0);
_actual = git__malloc(len + 1);
cl_assert(_actual != NULL);

cl_assert(git_repository_message(_actual, len, _repo) > 0);
_actual[len] = '\0';
cl_assert_equal_s(expected, _actual);

}

void test_repo_state__revert(void)
Expand Down Expand Up @@ -62,7 +79,7 @@ void test_repo_state__cherry_pick(void)

cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), GIT_CHERRY_PICK_HEAD_FILE));
cl_git_mkfile(git_buf_cstr(&_path), "dummy");
cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "COMMIT_EDITMSG"));
cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG"));
cl_git_mkfile(git_buf_cstr(&_path), expected);
cl_assert_equal_i(GIT_REPOSITORY_STATE_CHERRY_PICK, git_repository_state(_repo));

Expand Down

0 comments on commit 23b6879

Please sign in to comment.