From: "Kristian Høgsberg" <krh@redhat.com>
To: git@vger.kernel.org
Cc: "Kristian Høgsberg" <krh@redhat.com>
Subject: [PATCH 5/5] Split out the actual commit creation from the option parsing etc.
Date: Mon, 30 Jul 2007 17:28:46 -0400 [thread overview]
Message-ID: <11858309332705-git-send-email-krh@redhat.com> (raw)
In-Reply-To: <11858309322915-git-send-email-krh@redhat.com>
From: Kristian Høgsberg <krh@redhat.com>
The functionality is available inside git through the function
create_commit().
Signed-off-by: Kristian Høgsberg <krh@redhat.com>
---
builtin-commit-tree.c | 96 ++++++++++++++++++++++++++++++------------------
commit.h | 7 ++++
2 files changed, 67 insertions(+), 36 deletions(-)
diff --git a/builtin-commit-tree.c b/builtin-commit-tree.c
index 72884eb..e20f0b4 100644
--- a/builtin-commit-tree.c
+++ b/builtin-commit-tree.c
@@ -52,15 +52,59 @@ static const char commit_utf8_warn[] =
"You may want to amend it after fixing the message, or set the config\n"
"variable i18n.commitencoding to the encoding your project uses.\n";
+const unsigned char *
+create_commit(const unsigned char *tree_sha1,
+ unsigned char parent_sha1[][20], int parents,
+ const char *author_info, const char *committer_info,
+ const char *message, int length)
+{
+ static unsigned char commit_sha1[20];
+ int encoding_is_utf8, i;
+ struct strbuf sb;
+
+ /* Not having i18n.commitencoding is the same as having utf-8 */
+ encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
+
+ strbuf_init(&sb);
+ strbuf_printf(&sb, "tree %s\n", sha1_to_hex(tree_sha1));
+
+ /*
+ * NOTE! This ordering means that the same exact tree merged with a
+ * different order of parents will be a _different_ changeset even
+ * if everything else stays the same.
+ */
+ for (i = 0; i < parents; i++)
+ strbuf_printf(&sb, "parent %s\n", sha1_to_hex(parent_sha1[i]));
+
+ /* Person/date information */
+ strbuf_printf(&sb, "author %s\n", author_info);
+ strbuf_printf(&sb, "committer %s\n", committer_info);
+ if (!encoding_is_utf8)
+ strbuf_printf(&sb, "encoding %s\n", git_commit_encoding);
+ strbuf_printf(&sb, "\n");
+
+ /* And add the comment */
+ strbuf_add(&sb, message, length);
+
+ /* And check the encoding */
+ strbuf_add_char(&sb, '\0');
+ if (encoding_is_utf8 && !is_utf8(sb.buf))
+ fprintf(stderr, commit_utf8_warn);
+
+ if (!write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1))
+ return commit_sha1;
+
+ return NULL;
+}
+
int cmd_commit_tree(int argc, const char **argv, const char *prefix)
{
int i;
int parents = 0;
unsigned char tree_sha1[20];
- unsigned char commit_sha1[20];
- char comment[1000];
- struct strbuf sb;
- int encoding_is_utf8;
+ char *buffer;
+ unsigned long len;
+ const unsigned char *commit_sha1;
git_config(git_default_config);
@@ -85,40 +129,20 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
parents++;
}
- /* Not having i18n.commitencoding is the same as having utf-8 */
- encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
+ buffer = NULL;
+ if (read_fd(0, &buffer, &len))
+ die("Could not read commit message from standard input");
- strbuf_init(&sb);
- strbuf_printf(&sb, "tree %s\n", sha1_to_hex(tree_sha1));
+ commit_sha1 = create_commit(tree_sha1,
+ parent_sha1, parents,
+ xstrdup(git_author_info(1)),
+ xstrdup(git_committer_info(1)),
+ buffer, len);
- /*
- * NOTE! This ordering means that the same exact tree merged with a
- * different order of parents will be a _different_ changeset even
- * if everything else stays the same.
- */
- for (i = 0; i < parents; i++)
- strbuf_printf(&sb, "parent %s\n", sha1_to_hex(parent_sha1[i]));
-
- /* Person/date information */
- strbuf_printf(&sb, "author %s\n", git_author_info(1));
- strbuf_printf(&sb, "committer %s\n", git_committer_info(1));
- if (!encoding_is_utf8)
- strbuf_printf(&sb, "encoding %s\n", git_commit_encoding);
- strbuf_printf(&sb, "\n");
-
- /* And add the comment */
- while (fgets(comment, sizeof(comment), stdin) != NULL)
- strbuf_printf(&sb, "%s", comment);
+ if (!commit_sha1)
+ return 1;
- /* And check the encoding */
- strbuf_add_char(&sb, '\0');
- if (encoding_is_utf8 && !is_utf8(sb.buf))
- fprintf(stderr, commit_utf8_warn);
+ printf("%s\n", sha1_to_hex(commit_sha1));
- if (!write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
- printf("%s\n", sha1_to_hex(commit_sha1));
- return 0;
- }
- else
- return 1;
+ return 0;
}
diff --git a/commit.h b/commit.h
index 467872e..9f640ba 100644
--- a/commit.h
+++ b/commit.h
@@ -122,4 +122,11 @@ extern struct commit_list *get_shallow_commits(struct object_array *heads,
int depth, int shallow_flag, int not_shallow_flag);
int in_merge_bases(struct commit *, struct commit **, int);
+
+extern const unsigned char *
+create_commit(const unsigned char *tree_sha1,
+ unsigned char parent_sha1[][20], int parents,
+ const char *author_info, const char *committer_info,
+ const char *message, int length);
+
#endif /* COMMIT_H */
--
1.5.2.GIT
next prev parent reply other threads:[~2007-07-30 21:34 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-30 21:28 [PATCH 1/5] Add test case for basic commit functionality Kristian Høgsberg
2007-07-30 21:28 ` [PATCH 2/5] Enable wt-status output to a given FILE pointer Kristian Høgsberg
2007-07-30 21:28 ` [PATCH 3/5] Add strbuf_printf() to do formatted printing to a strbuf Kristian Høgsberg
2007-07-30 21:28 ` [PATCH 4/5] Make builtin-commit-tree use a strbuf instead of hand-rolled realloc buffer Kristian Høgsberg
2007-07-30 21:28 ` Kristian Høgsberg [this message]
2007-07-31 4:43 ` [PATCH 5/5] Split out the actual commit creation from the option parsing etc Junio C Hamano
2007-07-31 14:11 ` Kristian Høgsberg
2007-07-31 4:39 ` [PATCH 4/5] Make builtin-commit-tree use a strbuf instead of hand-rolled realloc buffer Junio C Hamano
2007-07-31 4:36 ` [PATCH 3/5] Add strbuf_printf() to do formatted printing to a strbuf Junio C Hamano
2007-07-31 14:23 ` Kristian Høgsberg
2007-07-31 14:55 ` Johannes Schindelin
2007-07-31 15:33 ` Kristian Høgsberg
2007-07-31 14:57 ` Johannes Schindelin
2007-07-31 15:28 ` Kristian Høgsberg
2007-07-31 19:54 ` [PATCH] " Kristian Høgsberg
2007-07-31 22:01 ` Junio C Hamano
2007-07-31 4:18 ` [PATCH 1/5] Add test case for basic commit functionality Junio C Hamano
2007-07-31 14:27 ` Kristian Høgsberg
2007-07-31 19:37 ` [PATCH] " Kristian Høgsberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=11858309332705-git-send-email-krh@redhat.com \
--to=krh@redhat.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).