Skip to content

Commit 0505d60

Browse files
phillipwoodgitster
authored andcommitted
Add a function to update HEAD after creating a commit
Add update_head_with_reflog() based on the code that updates HEAD after committing in builtin/commit.c that can be called by 'git commit' and other commands. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d0aaa46 commit 0505d60

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

builtin/commit.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,13 +1591,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15911591
struct strbuf sb = STRBUF_INIT;
15921592
struct strbuf author_ident = STRBUF_INIT;
15931593
const char *index_file, *reflog_msg;
1594-
char *nl;
15951594
struct object_id oid;
15961595
struct commit_list *parents = NULL;
15971596
struct stat statbuf;
15981597
struct commit *current_head = NULL;
15991598
struct commit_extra_header *extra = NULL;
1600-
struct ref_transaction *transaction;
16011599
struct strbuf err = STRBUF_INIT;
16021600

16031601
if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -1720,25 +1718,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17201718
strbuf_release(&author_ident);
17211719
free_commit_extra_headers(extra);
17221720

1723-
nl = strchr(sb.buf, '\n');
1724-
if (nl)
1725-
strbuf_setlen(&sb, nl + 1 - sb.buf);
1726-
else
1727-
strbuf_addch(&sb, '\n');
1728-
strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1729-
strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1730-
1731-
transaction = ref_transaction_begin(&err);
1732-
if (!transaction ||
1733-
ref_transaction_update(transaction, "HEAD", &oid,
1734-
current_head
1735-
? &current_head->object.oid : &null_oid,
1736-
0, sb.buf, &err) ||
1737-
ref_transaction_commit(transaction, &err)) {
1721+
if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1722+
&err)) {
17381723
rollback_index_files();
17391724
die("%s", err.buf);
17401725
}
1741-
ref_transaction_free(transaction);
17421726

17431727
unlink(git_path_cherry_pick_head());
17441728
unlink(git_path_revert_head());

sequencer.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "cache.h"
22
#include "config.h"
33
#include "lockfile.h"
4-
#include "sequencer.h"
54
#include "dir.h"
65
#include "object.h"
76
#include "commit.h"
7+
#include "sequencer.h"
88
#include "tag.h"
99
#include "run-command.h"
1010
#include "exec_cmd.h"
@@ -752,6 +752,43 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
752752
return rest_is_empty(sb, start - sb->buf);
753753
}
754754

755+
int update_head_with_reflog(const struct commit *old_head,
756+
const struct object_id *new_head,
757+
const char *action, const struct strbuf *msg,
758+
struct strbuf *err)
759+
{
760+
struct ref_transaction *transaction;
761+
struct strbuf sb = STRBUF_INIT;
762+
const char *nl;
763+
int ret = 0;
764+
765+
if (action) {
766+
strbuf_addstr(&sb, action);
767+
strbuf_addstr(&sb, ": ");
768+
}
769+
770+
nl = strchr(msg->buf, '\n');
771+
if (nl) {
772+
strbuf_add(&sb, msg->buf, nl + 1 - msg->buf);
773+
} else {
774+
strbuf_addbuf(&sb, msg);
775+
strbuf_addch(&sb, '\n');
776+
}
777+
778+
transaction = ref_transaction_begin(err);
779+
if (!transaction ||
780+
ref_transaction_update(transaction, "HEAD", new_head,
781+
old_head ? &old_head->object.oid : &null_oid,
782+
0, sb.buf, err) ||
783+
ref_transaction_commit(transaction, err)) {
784+
ret = -1;
785+
}
786+
ref_transaction_free(transaction);
787+
strbuf_release(&sb);
788+
789+
return ret;
790+
}
791+
755792
static int is_original_commit_empty(struct commit *commit)
756793
{
757794
const struct object_id *ptree_oid;

sequencer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ int message_is_empty(const struct strbuf *sb,
6969
enum commit_msg_cleanup_mode cleanup_mode);
7070
int template_untouched(const struct strbuf *sb, const char *template_file,
7171
enum commit_msg_cleanup_mode cleanup_mode);
72+
int update_head_with_reflog(const struct commit *old_head,
73+
const struct object_id *new_head,
74+
const char *action, const struct strbuf *msg,
75+
struct strbuf *err);
7276
#endif

0 commit comments

Comments
 (0)