[hacktree] Add log builtin
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hacktree] Add log builtin
- Date: Sat, 15 Oct 2011 19:44:18 +0000 (UTC)
commit 97003b48da171318e3e25a403e188fbc818419ef
Author: Colin Walters <walters verbum org>
Date: Sat Oct 15 15:43:43 2011 -0400
Add log builtin
The "less" spawning is buggy for some reason =(
Makefile-hacktree.am | 1 +
src/ht-builtin-log.c | 152 +++++++++++++++++++++++++++++++++++++++
src/ht-builtins.h | 1 +
src/libhacktree/hacktree-repo.c | 1 +
src/libhtutil/ht-unix-utils.c | 43 +++++++++++
src/libhtutil/ht-unix-utils.h | 2 +
src/main.c | 1 +
tests/t0008-log.sh | 31 ++++++++
8 files changed, 232 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-hacktree.am b/Makefile-hacktree.am
index 3dfa8ca..636b7d0 100644
--- a/Makefile-hacktree.am
+++ b/Makefile-hacktree.am
@@ -51,6 +51,7 @@ hacktree_SOURCES = src/main.c \
src/ht-builtin-fsck.c \
src/ht-builtin-init.c \
src/ht-builtin-link-file.c \
+ src/ht-builtin-log.c \
src/ht-builtin-show.c \
$(NULL)
hacktree_CFLAGS = -I$(srcdir)/src -I$(srcdir)/src/libhacktree -I$(srcdir)/src/libhtutil -DLOCALEDIR=\"$(datadir)/locale\" $(GIO_UNIX_CFLAGS)
diff --git a/src/ht-builtin-log.c b/src/ht-builtin-log.c
new file mode 100644
index 0000000..aa0c271
--- /dev/null
+++ b/src/ht-builtin-log.c
@@ -0,0 +1,152 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2011 Colin Walters <walters verbum org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Colin Walters <walters verbum org>
+ */
+
+#include "config.h"
+
+#include "ht-builtins.h"
+#include "hacktree.h"
+
+#include <glib/gi18n.h>
+
+static char *repo_path;
+
+static GOptionEntry options[] = {
+ { "repo", 0, 0, G_OPTION_ARG_FILENAME, &repo_path, "Repository path", "repo" },
+ { NULL }
+};
+
+gboolean
+hacktree_builtin_log (int argc, char **argv, const char *prefix, GError **error)
+{
+ GOptionContext *context;
+ gboolean ret = FALSE;
+ HacktreeRepo *repo = NULL;
+ GOutputStream *pager = NULL;
+ GVariant *commit = NULL;
+ char *head;
+
+ context = g_option_context_new ("- Show revision log");
+ g_option_context_add_main_entries (context, options, NULL);
+
+ if (!g_option_context_parse (context, &argc, &argv, error))
+ goto out;
+
+ if (repo_path == NULL)
+ repo_path = ".";
+ if (prefix == NULL)
+ prefix = ".";
+
+ repo = hacktree_repo_new (repo_path);
+ if (!hacktree_repo_check (repo, error))
+ goto out;
+
+ head = g_strdup (hacktree_repo_get_head (repo));
+ if (!head)
+ {
+ g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No HEAD exists");
+ goto out;
+ }
+
+ if (!ht_util_spawn_pager (&pager, error))
+ goto out;
+
+ while (TRUE)
+ {
+ HacktreeSerializedVariantType type;
+ char *formatted = NULL;
+ guint32 version;
+ const char *parent;
+ const char *subject;
+ const char *body;
+ guint64 timestamp;
+ const char *contents;
+ const char *root_metadata;
+ GDateTime *time_obj = NULL;
+ char *formatted_date = NULL;
+ const char *body_newline;
+ gsize bytes_written;
+ GVariant *commit_metadata = NULL;
+ char *formatted_metadata = NULL;
+
+ if (commit)
+ g_variant_unref (commit);
+ if (!hacktree_repo_load_variant (repo, head, &type, &commit, error))
+ goto out;
+
+ /* Ignore commit metadata for now */
+ g_variant_get (commit, "(u a{sv}&s&s&st&s&s)",
+ &version, &commit_metadata, &parent, &subject, &body,
+ ×tamp, &contents, &root_metadata);
+ time_obj = g_date_time_new_from_unix_utc (timestamp);
+ formatted_date = g_date_time_format (time_obj, "%a %b %d %H:%M:%S %Y %z");
+ g_date_time_unref (time_obj);
+ time_obj = NULL;
+
+ formatted_metadata = g_variant_print (commit_metadata, TRUE);
+ g_variant_unref (commit_metadata);
+ formatted = g_strdup_printf ("commit %s\nSubject: %s\nDate: %s\nMetadata: %s\n\n",
+ head, subject, formatted_date, formatted_metadata);
+ g_free (formatted_metadata);
+ g_free (formatted_date);
+ formatted_date = NULL;
+
+ if (!g_output_stream_write_all (pager, formatted, strlen (formatted), &bytes_written, NULL, error))
+ {
+ g_free (formatted);
+ goto out;
+ }
+ g_free (formatted);
+
+ body_newline = strchr (body, '\n');
+ do {
+ gsize len;
+ if (!g_output_stream_write_all (pager, " ", 4, &bytes_written, NULL, error))
+ goto out;
+ len = body_newline ? body_newline - body : strlen (body);
+ if (!g_output_stream_write_all (pager, body, len, &bytes_written, NULL, error))
+ goto out;
+ if (!g_output_stream_write_all (pager, "\n\n", 2, &bytes_written, NULL, error))
+ goto out;
+ body_newline = strchr (body, '\n');
+ if (!body_newline)
+ break;
+ else
+ body_newline += 1;
+ } while (*body_newline);
+
+ if (strcmp (parent, "") == 0)
+ break;
+ g_free (head);
+ head = g_strdup (parent);
+ }
+
+ if (!g_output_stream_close (pager, NULL, error))
+ goto out;
+
+ ret = TRUE;
+ out:
+ if (context)
+ g_option_context_free (context);
+ if (commit)
+ g_variant_unref (commit);
+ g_clear_object (&repo);
+ return ret;
+}
diff --git a/src/ht-builtins.h b/src/ht-builtins.h
index e1db9c1..3cfe5b1 100644
--- a/src/ht-builtins.h
+++ b/src/ht-builtins.h
@@ -39,6 +39,7 @@ typedef struct {
gboolean hacktree_builtin_checkout (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_commit (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_init (int argc, char **argv, const char *prefix, GError **error);
+gboolean hacktree_builtin_log (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_link_file (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_fsck (int argc, char **argv, const char *prefix, GError **error);
gboolean hacktree_builtin_show (int argc, char **argv, const char *prefix, GError **error);
diff --git a/src/libhacktree/hacktree-repo.c b/src/libhacktree/hacktree-repo.c
index 3521bf1..6781d25 100644
--- a/src/libhacktree/hacktree-repo.c
+++ b/src/libhacktree/hacktree-repo.c
@@ -1602,6 +1602,7 @@ hacktree_repo_load_variant (HacktreeRepo *repo,
{
if (ret_variant)
g_variant_unref (ret_variant);
+ g_prefix_error (error, "Failed to load metadata variant '%s': ", sha256);
}
return ret;
}
diff --git a/src/libhtutil/ht-unix-utils.c b/src/libhtutil/ht-unix-utils.c
index 375c52a..d955725 100644
--- a/src/libhtutil/ht-unix-utils.c
+++ b/src/libhtutil/ht-unix-utils.c
@@ -25,11 +25,54 @@
#include <glib-unix.h>
#include <gio/gio.h>
+#include <gio/gunixoutputstream.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
+gboolean
+ht_util_spawn_pager (GOutputStream **out_stream,
+ GError **error)
+{
+ const char *pager;
+ char *argv[2];
+ int stdin_fd;
+ pid_t pid;
+ gboolean ret = FALSE;
+ GOutputStream *ret_stream = NULL;
+
+ if (!isatty (1))
+ {
+ ret_stream = (GOutputStream*)g_unix_output_stream_new (1, TRUE);
+ }
+ else
+ {
+ pager = g_getenv ("GIT_PAGER");
+ if (pager == NULL)
+ pager = "less";
+
+ argv[0] = (char*)pager;
+ argv[1] = NULL;
+
+ if (!g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
+ NULL, NULL, &pid, &stdin_fd, NULL, NULL, error))
+ {
+ g_prefix_error (error, "%s", "Failed to spawn pager: ");
+ goto out;
+ }
+
+ ret_stream = (GOutputStream*)g_unix_output_stream_new (stdin_fd, TRUE);
+ }
+
+ *out_stream = ret_stream;
+ ret_stream = NULL;
+ ret = TRUE;
+ out:
+ g_clear_object (&ret_stream);
+ return ret;
+}
+
static int
compare_filenames_by_component_length (const char *a,
const char *b)
diff --git a/src/libhtutil/ht-unix-utils.h b/src/libhtutil/ht-unix-utils.h
index bd95b55..cc027a3 100644
--- a/src/libhtutil/ht-unix-utils.h
+++ b/src/libhtutil/ht-unix-utils.h
@@ -36,6 +36,8 @@
G_BEGIN_DECLS
+gboolean ht_util_spawn_pager (GOutputStream **out_stream, GError **error);
+
gboolean ht_util_filename_has_dotdot (const char *path);
GPtrArray *ht_util_sort_filenames_by_component_length (GPtrArray *files);
diff --git a/src/main.c b/src/main.c
index 6ccb158..6e8ce9d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -33,6 +33,7 @@ static HacktreeBuiltin builtins[] = {
{ "init", hacktree_builtin_init, 0 },
{ "commit", hacktree_builtin_commit, 0 },
{ "link-file", hacktree_builtin_link_file, 0 },
+ { "log", hacktree_builtin_log, 0 },
{ "fsck", hacktree_builtin_fsck, 0 },
{ "show", hacktree_builtin_show, 0 },
{ NULL }
diff --git a/tests/t0008-log.sh b/tests/t0008-log.sh
new file mode 100755
index 0000000..60316ce
--- /dev/null
+++ b/tests/t0008-log.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+#
+# Copyright (C) 2011 Colin Walters <walters verbum org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# Author: Colin Walters <walters verbum org>
+
+set -e
+
+. libtest.sh
+
+echo "1..2"
+
+setup_test_repository2
+hacktree log $ht_repo > $test_tmpdir/log.txt
+assert_file_has_content $test_tmpdir/log.txt "Test Commit 1"
+assert_file_has_content $test_tmpdir/log.txt "Test Commit 2"
+echo "ok log"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]