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>
Phillip Wood committed
Nov 17, 2017 at 11:34 UTC
0505d604c9c5a361ee027d155c7d1facaf326863
3 files changed
+44
-19
builtin/commit.c
+2
-18
@@ -1591,13 +1591,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1591
struct strbuf sb = STRBUF_INIT;
1592
struct strbuf author_ident = STRBUF_INIT;
1593
const char *index_file, *reflog_msg;
1594
- char *nl;
1594
struct object_id oid;
1595
struct commit_list *parents = NULL;
1596
struct stat statbuf;
1597
struct commit *current_head = NULL;
1598
struct commit_extra_header *extra = NULL;
1600
- struct ref_transaction *transaction;
1599
struct strbuf err = STRBUF_INIT;
1600
1601
if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -1720,25 +1718,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1718
strbuf_release(&author_ident);
1719
free_commit_extra_headers(extra);
1720
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
- ? ¤t_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)) {
1723
rollback_index_files();
1724
die("%s", err.buf);
1725
}
1741
- ref_transaction_free(transaction);
1726
1727
unlink(git_path_cherry_pick_head());
1728
unlink(git_path_revert_head());
sequencer.c
+38
-1
@@ -1,10 +1,10 @@
1
#include "cache.h"
2
#include "config.h"
3
#include "lockfile.h"
4
-#include "sequencer.h"
4
#include "dir.h"
5
#include "object.h"
6
#include "commit.h"
7
+#include "sequencer.h"
8
#include "tag.h"
9
#include "run-command.h"
10
#include "exec_cmd.h"
@@ -752,6 +752,43 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
752
return rest_is_empty(sb, start - sb->buf);
753
}
754
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
+
792
static int is_original_commit_empty(struct commit *commit)
793
{
794
const struct object_id *ptree_oid;
sequencer.h
+4
@@ -69,4 +69,8 @@ int message_is_empty(const struct strbuf *sb,
69
enum commit_msg_cleanup_mode cleanup_mode);
70
int template_untouched(const struct strbuf *sb, const char *template_file,
71
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);
76
#endif