From 46544f5b4da4d33967794136094d3d6f14d599f1 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sat, 11 Feb 2017 11:24:15 -0500 Subject: [PATCH] commit: Support -F/--body-file, like git This is more convenient to script for projects which haven't yet made the leap to using the API. Closes: https://github.com/ostreedev/ostree/issues/674 Closes: #681 Approved by: jlebon --- src/ostree/ot-builtin-commit.c | 18 +++++++++++++++--- tests/basic-test.sh | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/ostree/ot-builtin-commit.c b/src/ostree/ot-builtin-commit.c index d3b634f9..218fb701 100644 --- a/src/ostree/ot-builtin-commit.c +++ b/src/ostree/ot-builtin-commit.c @@ -32,6 +32,7 @@ static char *opt_subject; static char *opt_body; +static char *opt_body_file; static gboolean opt_editor; static char *opt_parent; static gboolean opt_orphan; @@ -74,6 +75,7 @@ static GOptionEntry options[] = { { "parent", 0, 0, G_OPTION_ARG_STRING, &opt_parent, "Parent ref, or \"none\"", "REF" }, { "subject", 's', 0, G_OPTION_ARG_STRING, &opt_subject, "One line subject", "SUBJECT" }, { "body", 'm', 0, G_OPTION_ARG_STRING, &opt_body, "Full description", "BODY" }, + { "body-file", 'F', 0, G_OPTION_ARG_STRING, &opt_body_file, "Commit message from FILE path", "FILE" }, { "editor", 'e', 0, G_OPTION_ARG_NONE, &opt_editor, "Use an editor to write the commit message", NULL }, { "branch", 'b', 0, G_OPTION_ARG_STRING, &opt_branch, "Branch", "BRANCH" }, { "orphan", 0, 0, G_OPTION_ARG_NONE, &opt_orphan, "Create a commit without writing a ref", NULL }, @@ -349,6 +351,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError OstreeRepoCommitModifier *modifier = NULL; OstreeRepoTransactionStats stats; struct CommitFilterData filter_data = { 0, }; + g_autofree char *commit_body = NULL; context = g_option_context_new ("[PATH] - Commit a new revision"); @@ -441,9 +444,18 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError if (opt_editor) { - if (!commit_editor (repo, opt_branch, &opt_subject, &opt_body, cancellable, error)) + if (!commit_editor (repo, opt_branch, &opt_subject, &commit_body, cancellable, error)) goto out; } + else if (opt_body_file) + { + commit_body = glnx_file_get_contents_utf8_at (AT_FDCWD, opt_body_file, NULL, + cancellable, error); + if (!commit_body) + goto out; + } + else if (opt_body) + commit_body = g_strdup (opt_body); if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error)) goto out; @@ -576,7 +588,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError timestamp = g_date_time_to_unix (now); g_date_time_unref (now); - if (!ostree_repo_write_commit (repo, parent, opt_subject, opt_body, metadata, + if (!ostree_repo_write_commit (repo, parent, opt_subject, commit_body, metadata, OSTREE_REPO_FILE (root), &commit_checksum, cancellable, error)) goto out; @@ -592,7 +604,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError } timestamp = ts.tv_sec; - if (!ostree_repo_write_commit_with_time (repo, parent, opt_subject, opt_body, metadata, + if (!ostree_repo_write_commit_with_time (repo, parent, opt_subject, commit_body, metadata, OSTREE_REPO_FILE (root), timestamp, &commit_checksum, cancellable, error)) diff --git a/tests/basic-test.sh b/tests/basic-test.sh index 5cad8ab3..d9308138 100644 --- a/tests/basic-test.sh +++ b/tests/basic-test.sh @@ -19,7 +19,7 @@ set -euo pipefail -echo "1..60" +echo "1..61" $OSTREE checkout test2 checkout-test2 echo "ok checkout" @@ -115,6 +115,22 @@ omitted_rev=$($OSTREE commit -b test2-no-subject-2 --timestamp="2005-10-29 12:43 assert_streq $empty_rev $omitted_rev echo "ok commit no subject" +cd ${test_tmpdir} +cat >commitmsg.txt < log.txt +assert_file_has_content log.txt '^ *This is a long$' +assert_file_has_content log.txt '^ *Build-Host:.*example.com$' +assert_file_has_content log.txt '^ *Crunchy-With.*true$' +$OSTREE refs --delete branch-with-commitmsg +echo "ok commit body file" + cd ${test_tmpdir} $OSTREE commit -b test2-custom-parent -s '' $test_tmpdir/checkout-test2-4 $OSTREE commit -b test2-custom-parent -s '' $test_tmpdir/checkout-test2-4