Commit db55bb73ff4bccbaccbb4c3a7f6b1fcf09498df7

Ben Straub 2014-02-06T11:18:10

Correct default reflog message for git_remote_fetch

diff --git a/include/git2/remote.h b/include/git2/remote.h
index 9f66b99..238b6fd 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -357,7 +357,8 @@ GIT_EXTERN(void) git_remote_free(git_remote *remote);
  * @param remote the remote to update
  * @param signature The identity to use when updating reflogs
  * @param reflog_message The message to insert into the reflogs. If NULL, the
- *                       default is "fetch"
+ *                       default is "fetch <name>", where <name> is the name of
+ *                       the remote (or its url, for in-memory remotes).
  * @return 0 or an error code
  */
 GIT_EXTERN(int) git_remote_update_tips(
diff --git a/src/remote.c b/src/remote.c
index 28188ac..f320f4a 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -851,6 +851,7 @@ int git_remote_fetch(
 		const char *reflog_message)
 {
 	int error;
+	git_buf reflog_msg_buf = GIT_BUF_INIT;
 
 	/* Connect and download everything */
 	if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0)
@@ -862,8 +863,18 @@ int git_remote_fetch(
 	/* We don't need to be connected anymore */
 	git_remote_disconnect(remote);
 
+	/* Default reflog message */
+	if (reflog_message)
+		git_buf_sets(&reflog_msg_buf, reflog_message);
+	else {
+		git_buf_printf(&reflog_msg_buf, "fetch %s",
+				remote->name ? remote->name : remote->url);
+	}
+
 	/* Create "remote/foo" branches for all remote branches */
-	return git_remote_update_tips(remote, signature, reflog_message);
+	error = git_remote_update_tips(remote, signature, git_buf_cstr(&reflog_msg_buf));
+	git_buf_free(&reflog_msg_buf);
+	return error;
 }
 
 static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *update_heads, const char *fetchspec_src)
diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c
index 5265647..589e6ac 100644
--- a/tests/network/remote/local.c
+++ b/tests/network/remote/local.c
@@ -328,3 +328,31 @@ void test_network_remote_local__reflog(void)
 	git_reflog_free(log);
 	git_signature_free(sig);
 }
+
+void test_network_remote_local__fetch_default_reflog_message(void)
+{
+	const char *refspec = "master:remotes/sloppy/master";
+
+	git_reflog *log;
+	const git_reflog_entry *entry;
+	git_signature *sig;
+	char expected_reflog_msg[1024];
+
+	cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com"));
+
+	connect_to_local_repository(cl_fixture("testrepo.git"));
+	cl_git_pass(git_remote_add_fetch(remote, refspec));
+
+	cl_git_pass(git_remote_fetch(remote, sig, NULL));
+
+	cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
+	cl_assert_equal_i(1, git_reflog_entrycount(log));
+	entry = git_reflog_entry_byindex(log, 0);
+	cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+
+	sprintf(expected_reflog_msg, "fetch %s", git_remote_url(remote));
+	cl_assert_equal_s(expected_reflog_msg, git_reflog_entry_message(entry));
+
+	git_reflog_free(log);
+	git_signature_free(sig);
+}