From 59bb1126e0a6d5c004f3856e23c75ae440211f1c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 28 Jan 2014 11:45:30 -0800 Subject: [PATCH] Provide good default reflog messages in branch api --- src/branch.c | 36 +++++++++++++++++++++++++++--------- tests/refs/branches/create.c | 17 +++++++++++++++++ tests/refs/branches/move.c | 21 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/branch.c b/src/branch.c index c7651c6c3..133d6f6d4 100644 --- a/src/branch.c +++ b/src/branch.c @@ -59,8 +59,9 @@ int git_branch_create( const char *log_message) { git_reference *branch = NULL; - git_buf canonical_branch_name = GIT_BUF_INIT; - int error = 0; + git_buf canonical_branch_name = GIT_BUF_INIT, + log_message_buf = GIT_BUF_INIT; + int error = -1; assert(branch_name && commit && ref_out); assert(git_object_owner((const git_object *)commit) == repository); @@ -68,12 +69,19 @@ int git_branch_create( if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0) goto cleanup; - if (!(error = git_reference_create(&branch, repository, - git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force, signature, log_message))) + if (git_buf_sets(&log_message_buf, log_message ? log_message : "Branch: created") < 0) + goto cleanup; + + error = git_reference_create(&branch, repository, + git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force, signature, + git_buf_cstr(&log_message_buf)); + + if (!error) *ref_out = branch; cleanup: git_buf_free(&canonical_branch_name); + git_buf_free(&log_message_buf); return error; } @@ -195,8 +203,9 @@ int git_branch_move( const char *log_message) { git_buf new_reference_name = GIT_BUF_INIT, - old_config_section = GIT_BUF_INIT, - new_config_section = GIT_BUF_INIT; + old_config_section = GIT_BUF_INIT, + new_config_section = GIT_BUF_INIT, + log_message_buf = GIT_BUF_INIT; int error; assert(branch && new_branch_name); @@ -204,15 +213,23 @@ int git_branch_move( if (!git_reference_is_branch(branch)) return not_a_local_branch(git_reference_name(branch)); - error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name); - if (error < 0) + if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0) goto done; + if (log_message) { + if ((error = git_buf_sets(&log_message_buf, log_message)) < 0) + goto done; + } else { + if ((error = git_buf_printf(&log_message_buf, "Branch: renamed %s to %s", + git_reference_name(branch), git_buf_cstr(&new_reference_name))) < 0) + goto done; + } + /* first update ref then config so failure won't trash config */ error = git_reference_rename( out, branch, git_buf_cstr(&new_reference_name), force, - signature, log_message); + signature, git_buf_cstr(&log_message_buf)); if (error < 0) goto done; @@ -229,6 +246,7 @@ done: git_buf_free(&new_reference_name); git_buf_free(&old_config_section); git_buf_free(&new_config_section); + git_buf_free(&log_message_buf); return error; } diff --git a/tests/refs/branches/create.c b/tests/refs/branches/create.c index 0c0fdb013..32e17d600 100644 --- a/tests/refs/branches/create.c +++ b/tests/refs/branches/create.c @@ -86,4 +86,21 @@ void test_refs_branches_create__creation_creates_new_reflog(void) cl_assert_equal_i(1, git_reflog_entrycount(log)); entry = git_reflog_entry_byindex(log, 0); cl_assert_equal_s("create!", git_reflog_entry_message(entry)); + + git_reflog_free(log); +} + +void test_refs_branches_create__default_reflog_message(void) +{ + git_reflog *log; + const git_reflog_entry *entry; + + retrieve_known_commit(&target, repo); + cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, false, NULL, NULL)); + cl_git_pass(git_reflog_read(&log, repo, "refs/heads/" NEW_BRANCH_NAME)); + + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s("Branch: created", git_reflog_entry_message(entry)); + + git_reflog_free(log); } diff --git a/tests/refs/branches/move.c b/tests/refs/branches/move.c index 0bdb58a5f..622921d4f 100644 --- a/tests/refs/branches/move.c +++ b/tests/refs/branches/move.c @@ -205,3 +205,24 @@ void test_refs_branches_move__updates_the_reflog(void) git_reference_free(new_branch); git_reflog_free(log); } + +void test_refs_branches_move__default_reflog_message(void) +{ + git_reference *branch; + git_reference *new_branch; + git_reflog *log; + const git_reflog_entry *entry; + + cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master")); + cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0, NULL, NULL)); + + cl_git_pass(git_reflog_read(&log, repo, git_reference_name(new_branch))); + entry = git_reflog_entry_byindex(log, 0); + cl_assert_equal_s("Branch: renamed refs/heads/master to refs/heads/master2", + git_reflog_entry_message(entry)); + + git_reference_free(branch); + git_reference_free(new_branch); + git_reflog_free(log); + +}