bin/commit: Port helper functions to new style

Prep for more work here.  Can't yet port the main function
without a cleanup for transactions.

Closes: #988
Approved by: jlebon
This commit is contained in:
Colin Walters
2017-06-30 09:19:43 -04:00
committed by Atomic Bot
parent 192e7b888f
commit e3a540a606

View File

@ -63,12 +63,10 @@ parse_fsync_cb (const char *option_name,
GError **error) GError **error)
{ {
gboolean val; gboolean val;
if (!ot_parse_boolean (value, &val, error)) if (!ot_parse_boolean (value, &val, error))
return FALSE; return FALSE;
opt_disable_fsync = !val;
opt_disable_fsync = !val;
return TRUE; return TRUE;
} }
@ -109,16 +107,12 @@ parse_file_by_line (const char *path,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE; g_autofree char *contents =
g_autofree char *contents = NULL; glnx_file_get_contents_utf8_at (AT_FDCWD, path, NULL, cancellable, error);
g_autoptr(GFile) file = NULL; if (!contents)
char **lines = NULL; return FALSE;
file = g_file_new_for_path (path); g_auto(GStrv) lines = g_strsplit (contents, "\n", -1);
if (!g_file_load_contents (file, cancellable, &contents, NULL, NULL, error))
goto out;
lines = g_strsplit (contents, "\n", -1);
for (char **iter = lines; iter && *iter; iter++) for (char **iter = lines; iter && *iter; iter++)
{ {
/* skip empty lines at least */ /* skip empty lines at least */
@ -126,13 +120,10 @@ parse_file_by_line (const char *path,
continue; continue;
if (!cb (*iter, cbdata, error)) if (!cb (*iter, cbdata, error))
goto out; return FALSE;
} }
ret = TRUE; return TRUE;
out:
g_strfreev (lines);
return ret;
} }
static gboolean static gboolean
@ -141,16 +132,11 @@ handle_statoverride_line (const char *line,
GError **error) GError **error)
{ {
GHashTable *files = data; GHashTable *files = data;
const char *spc; const char *spc = strchr (line, ' ');
guint mode_add;
spc = strchr (line, ' ');
if (spc == NULL) if (spc == NULL)
{ return glnx_throw (error, "Malformed statoverride file (no space found)");
return glnx_throw (error, "Malformed statoverride file (no space found)");
}
mode_add = (guint32)(gint32)g_ascii_strtod (line, NULL); guint mode_add = (guint32)(gint32)g_ascii_strtod (line, NULL);
g_hash_table_insert (files, g_strdup (spc + 1), g_hash_table_insert (files, g_strdup (spc + 1),
GUINT_TO_POINTER((gint32)mode_add)); GUINT_TO_POINTER((gint32)mode_add));
return TRUE; return TRUE;
@ -213,14 +199,7 @@ commit_editor (OstreeRepo *repo,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
g_autofree char *input = NULL; g_autofree char *input = g_strdup_printf ("\n"
g_autofree char *output = NULL;
gboolean ret = FALSE;
g_autoptr(GString) bodybuf = NULL;
char **lines = NULL;
int i;
input = g_strdup_printf ("\n"
"# Please enter the commit message for your changes. The first line will\n" "# Please enter the commit message for your changes. The first line will\n"
"# become the subject, and the remainder the body. Lines starting\n" "# become the subject, and the remainder the body. Lines starting\n"
"# with '#' will be ignored, and an empty message aborts the commit." "# with '#' will be ignored, and an empty message aborts the commit."
@ -233,12 +212,13 @@ commit_editor (OstreeRepo *repo,
*subject = NULL; *subject = NULL;
*body = NULL; *body = NULL;
output = ot_editor_prompt (repo, input, cancellable, error); g_autofree char *output = ot_editor_prompt (repo, input, cancellable, error);
if (output == NULL) if (output == NULL)
goto out; return FALSE;
lines = g_strsplit (output, "\n", -1); g_auto(GStrv) lines = g_strsplit (output, "\n", -1);
for (i = 0; lines[i] != NULL; i++) g_autoptr(GString) bodybuf = NULL;
for (guint i = 0; lines[i] != NULL; i++)
{ {
g_strchomp (lines[i]); g_strchomp (lines[i]);
@ -269,24 +249,15 @@ commit_editor (OstreeRepo *repo,
} }
if (!*subject) if (!*subject)
{ return glnx_throw (error, "Aborting commit due to empty commit subject.");
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Aborting commit due to empty commit subject.");
goto out;
}
if (bodybuf) if (bodybuf)
{ {
*body = g_string_free (bodybuf, FALSE); *body = g_string_free (g_steal_pointer (&bodybuf), FALSE);
g_strchomp (*body); g_strchomp (*body);
bodybuf = NULL;
} }
ret = TRUE; return TRUE;
out:
g_strfreev (lines);
return ret;
} }
static gboolean static gboolean
@ -294,38 +265,22 @@ parse_keyvalue_strings (char **strings,
GVariant **out_metadata, GVariant **out_metadata,
GError **error) GError **error)
{ {
gboolean ret = FALSE; g_autoptr(GVariantBuilder) builder =
char **iter; g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
g_autoptr(GVariantBuilder) builder = NULL;
builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}")); for (char ** iter = strings; *iter; iter++)
for (iter = strings; *iter; iter++)
{ {
const char *s; const char *s = *iter;
const char *eq; const char *eq = strchr (s, '=');
g_autofree char *key = NULL;
s = *iter;
eq = strchr (s, '=');
if (!eq) if (!eq)
{ return glnx_throw (error, "Missing '=' in KEY=VALUE metadata '%s'", s);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, g_autofree char *key = g_strndup (s, eq - s);
"Missing '=' in KEY=VALUE metadata '%s'", s);
goto out;
}
key = g_strndup (s, eq - s);
g_variant_builder_add (builder, "{sv}", key, g_variant_builder_add (builder, "{sv}", key,
g_variant_new_string (eq + 1)); g_variant_new_string (eq + 1));
} }
ret = TRUE; *out_metadata = g_variant_ref_sink (g_variant_builder_end (builder));
*out_metadata = g_variant_builder_end (builder); return TRUE;
g_variant_ref_sink (*out_metadata);
out:
return ret;
} }
gboolean gboolean
@ -387,7 +342,7 @@ ostree_builtin_commit (int argc, char **argv, GCancellable *cancellable, GError
&detached_metadata, error)) &detached_metadata, error))
goto out; goto out;
} }
if (!(opt_branch || opt_orphan)) if (!(opt_branch || opt_orphan))
{ {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,