Skip to content

Commit cf8e9a3

Browse files
authored
Merge pull request libgit2#4143 from richardipsum/issue-4094
Fix: make reflog include "(merge)" for merge commits
2 parents a4b5ac6 + 397cf1a commit cf8e9a3

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/refs.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,18 @@ int git_reference__update_terminal(
11301130
return error;
11311131
}
11321132

1133+
static const char *commit_type(const git_commit *commit)
1134+
{
1135+
unsigned int count = git_commit_parentcount(commit);
1136+
1137+
if (count >= 2)
1138+
return " (merge)";
1139+
else if (count == 0)
1140+
return " (initial)";
1141+
else
1142+
return "";
1143+
}
1144+
11331145
int git_reference__update_for_commit(
11341146
git_repository *repo,
11351147
git_reference *ref,
@@ -1146,7 +1158,7 @@ int git_reference__update_for_commit(
11461158
if ((error = git_commit_lookup(&commit, repo, id)) < 0 ||
11471159
(error = git_buf_printf(&reflog_msg, "%s%s: %s",
11481160
operation ? operation : "commit",
1149-
git_commit_parentcount(commit) == 0 ? " (initial)" : "",
1161+
commit_type(commit),
11501162
git_commit_summary(commit))) < 0)
11511163
goto done;
11521164

tests/refs/reflog/reflog.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "git2/reflog.h"
55
#include "reflog.h"
66

7-
7+
static const char *merge_reflog_message = "commit (merge): Merge commit";
88
static const char *new_ref = "refs/heads/test-reflog";
99
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
1010
#define commit_msg "commit: bla bla"
@@ -448,3 +448,45 @@ void test_refs_reflog_reflog__logallrefupdates_nonbare_set_false(void)
448448

449449
assert_no_reflog_update();
450450
}
451+
452+
void test_refs_reflog_reflog__show_merge_for_merge_commits(void)
453+
{
454+
git_oid b1_oid;
455+
git_oid b2_oid;
456+
git_oid merge_commit_oid;
457+
git_commit *b1_commit;
458+
git_commit *b2_commit;
459+
git_signature *s;
460+
git_commit *parent_commits[2];
461+
git_tree *tree;
462+
git_reflog *log;
463+
const git_reflog_entry *entry;
464+
465+
cl_git_pass(git_signature_now(&s, "alice", "alice@example.com"));
466+
467+
cl_git_pass(git_reference_name_to_id(&b1_oid, g_repo, "HEAD"));
468+
cl_git_pass(git_reference_name_to_id(&b2_oid, g_repo, "refs/heads/test"));
469+
470+
cl_git_pass(git_commit_lookup(&b1_commit, g_repo, &b1_oid));
471+
cl_git_pass(git_commit_lookup(&b2_commit, g_repo, &b2_oid));
472+
473+
parent_commits[0] = b1_commit;
474+
parent_commits[1] = b2_commit;
475+
476+
cl_git_pass(git_commit_tree(&tree, b1_commit));
477+
478+
cl_git_pass(git_commit_create(&merge_commit_oid,
479+
g_repo, "HEAD", s, s, NULL,
480+
"Merge commit", tree,
481+
2, (const struct git_commit **) parent_commits));
482+
483+
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
484+
entry = git_reflog_entry_byindex(log, 0);
485+
cl_assert_equal_s(merge_reflog_message, git_reflog_entry_message(entry));
486+
487+
git_reflog_free(log);
488+
git_tree_free(tree);
489+
git_commit_free(b1_commit);
490+
git_commit_free(b2_commit);
491+
git_signature_free(s);
492+
}

0 commit comments

Comments
 (0)