Provide good default reflog messages in branch api
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
diff --git a/src/branch.c b/src/branch.c
index c7651c6..133d6f6 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 0c0fdb0..32e17d6 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 0bdb58a..622921d 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);
+
+}