[ostree] core: add ^ for rev-parse



commit 2b00cf381076bb72557f9721dbc3ca8dc2940440
Author: Colin Walters <walters verbum org>
Date:   Wed Nov 2 13:22:13 2011 -0400

    core: add ^ for rev-parse

 Makefile-src.am              |    2 +
 src/libostree/ostree-repo.c  |  172 +++++++++++++++++++++++-------------------
 src/libostree/ostree-repo.h  |    6 ++
 src/libotutil/ot-opt-utils.c |   37 +++++++++
 src/libotutil/ot-opt-utils.h |   33 ++++++++
 src/libotutil/otutil.h       |    1 +
 src/ot-builtin-pull.c        |   12 +---
 src/ot-builtin-rev-parse.c   |   10 ++-
 tests/t0000-basic.sh         |    7 ++-
 9 files changed, 186 insertions(+), 94 deletions(-)
---
diff --git a/Makefile-src.am b/Makefile-src.am
index 30b5af8..33657e6 100644
--- a/Makefile-src.am
+++ b/Makefile-src.am
@@ -21,6 +21,8 @@
 noinst_LTLIBRARIES += libotutil.la
 
 libotutil_la_SOURCES = \
+	src/libotutil/ot-opt-utils.c \
+	src/libotutil/ot-opt-utils.h \
 	src/libotutil/ot-unix-utils.c \
 	src/libotutil/ot-unix-utils.h \
 	src/libotutil/ot-gio-utils.c \
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index 9a3528c..1b58d32 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -264,46 +264,79 @@ resolve_rev (OstreeRepo     *self,
 {
   OstreeRepoPrivate *priv = GET_PRIVATE (self);
   gboolean ret = FALSE;
+  char *tmp = NULL;
+  char *tmp2 = NULL;
   char *ret_rev = NULL;
   GFile *child = NULL;
   char *child_path = NULL;
   GError *temp_error = NULL;
+  GVariant *commit = NULL;
+
+  if (strlen (rev) == 0)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Invalid empty rev");
+      goto out;
+    }
+  else if (strlen (rev) == 64)
+    {
+      ret_rev = g_strdup (rev);
+    }
+  else if (g_str_has_suffix (rev, "^"))
+    {
+      tmp = g_strdup (rev);
+      tmp[strlen(tmp) - 1] = '\0';
+
+      if (!resolve_rev (self, tmp, allow_noent, &tmp2, error))
+        goto out;
+
+      if (!ostree_repo_load_variant_checked (self, OSTREE_SERIALIZED_COMMIT_VARIANT, tmp2, &commit, error))
+        goto out;
+      
+      g_variant_get_child (commit, 2, "s", &ret_rev);
+      if (strlen (ret_rev) == 0)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "Commit %s has no parent", tmp2);
+          goto out;
 
- if (strlen (rev) == 64)
-   {
-     ret_rev = g_strdup (rev);
-   }
- else
-   {
-     child = g_file_get_child (priv->local_heads_dir, rev);
-     child_path = g_file_get_path (child);
-     if (!ot_util_gfile_load_contents_utf8 (child, NULL, &ret_rev, NULL, &temp_error))
-       {
-         if (allow_noent && g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
-           {
-             g_free (ret_rev);
-             ret_rev = NULL;
-           }
-         else
-           {
-             g_propagate_error (error, temp_error);
-             g_prefix_error (error, "Couldn't open ref '%s': ", child_path);
-             goto out;
-           }
-       }
-     else
-       {
-         g_strchomp (ret_rev);
+        }
+    }
+  else
+    {
+      child = g_file_get_child (priv->local_heads_dir, rev);
+      child_path = g_file_get_path (child);
+      if (!ot_util_gfile_load_contents_utf8 (child, NULL, &ret_rev, NULL, &temp_error))
+        {
+          if (allow_noent && g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+            {
+              g_free (ret_rev);
+              ret_rev = NULL;
+            }
+          else
+            {
+              g_propagate_error (error, temp_error);
+              g_prefix_error (error, "Couldn't open ref '%s': ", child_path);
+              goto out;
+            }
+        }
+      else
+        {
+          g_strchomp (ret_rev);
          
-         if (!ostree_validate_checksum_string (ret_rev, error))
-           goto out;
-       }
-   }
+          if (!ostree_validate_checksum_string (ret_rev, error))
+            goto out;
+        }
+    }
 
   *sha256 = ret_rev;
   ret_rev = NULL;
   ret = TRUE;
  out:
+  if (commit)
+    g_variant_unref (commit);
+  g_free (tmp);
+  g_free (tmp2);
   g_clear_object (&child);
   g_free (child_path);
   g_free (ret_rev);
@@ -557,35 +590,18 @@ import_gvariant_object (OstreeRepo  *self,
   return ret;
 }
 
-static gboolean
-load_gvariant_object_unknown (OstreeRepo  *self,
-                              const char    *sha256,
-                              OstreeSerializedVariantType *out_type,
-                              GVariant     **out_variant,
-                              GError       **error)
-{
-  gboolean ret = FALSE;
-  char *path = NULL;
-
-  path = get_object_path (self, sha256, OSTREE_OBJECT_TYPE_META);
-  ret = ostree_parse_metadata_file (path, out_type, out_variant, error);
-  g_free (path);
-
-  return ret;
-}
-
-static gboolean
-load_gvariant_object (OstreeRepo  *self,
-                      OstreeSerializedVariantType expected_type,
-                      const char    *sha256, 
-                      GVariant     **out_variant,
-                      GError       **error)
+gboolean
+ostree_repo_load_variant_checked (OstreeRepo  *self,
+                                  OstreeSerializedVariantType expected_type,
+                                  const char    *sha256, 
+                                  GVariant     **out_variant,
+                                  GError       **error)
 {
   gboolean ret = FALSE;
   OstreeSerializedVariantType type;
   GVariant *ret_variant = NULL;
 
-  if (!load_gvariant_object_unknown (self, sha256, &type, &ret_variant, error))
+  if (!ostree_repo_load_variant (self, sha256, &type, &ret_variant, error))
     goto out;
 
   if (type != expected_type)
@@ -598,12 +614,10 @@ load_gvariant_object (OstreeRepo  *self,
 
   ret = TRUE;
   *out_variant = ret_variant;
+  ret_variant = NULL;
  out:
-  if (!ret)
-    {
-      if (ret_variant)
-        g_variant_unref (ret_variant);
-    }
+  if (ret_variant)
+    g_variant_unref (ret_variant);
   return ret;
 }
 
@@ -1021,8 +1035,8 @@ parse_tree (OstreeRepo    *self,
   GVariant *files_variant = NULL;
   GVariant *dirs_variant = NULL;
 
-  if (!load_gvariant_object (self, OSTREE_SERIALIZED_TREE_VARIANT,
-                             sha256, &tree_variant, error))
+  if (!ostree_repo_load_variant_checked (self, OSTREE_SERIALIZED_TREE_VARIANT,
+                                         sha256, &tree_variant, error))
     goto out;
 
   /* PARSE OSTREE_SERIALIZED_TREE_VARIANT */
@@ -1058,8 +1072,8 @@ parse_tree (OstreeRepo    *self,
       if (!parse_tree (self, tree_checksum, &child_tree, error))
         goto out;
 
-      if (!load_gvariant_object (self, OSTREE_SERIALIZED_DIRMETA_VARIANT,
-                                 meta_checksum, &metadata, error))
+      if (!ostree_repo_load_variant_checked (self, OSTREE_SERIALIZED_DIRMETA_VARIANT,
+                                             meta_checksum, &metadata, error))
         {
           parsed_tree_data_free (child_tree);
           goto out;
@@ -1106,16 +1120,16 @@ load_commit_and_trees (OstreeRepo   *self,
   const char *tree_contents_checksum;
   const char *tree_meta_checksum;
 
-  if (!load_gvariant_object (self, OSTREE_SERIALIZED_COMMIT_VARIANT,
-                             commit_sha256, &ret_commit, error))
+  if (!ostree_repo_load_variant_checked (self, OSTREE_SERIALIZED_COMMIT_VARIANT,
+                                         commit_sha256, &ret_commit, error))
     goto out;
 
   /* PARSE OSTREE_SERIALIZED_COMMIT_VARIANT */
   g_variant_get_child (ret_commit, 6, "&s", &tree_contents_checksum);
   g_variant_get_child (ret_commit, 7, "&s", &tree_meta_checksum);
 
-  if (!load_gvariant_object (self, OSTREE_SERIALIZED_DIRMETA_VARIANT,
-                             tree_meta_checksum, &root_metadata, error))
+  if (!ostree_repo_load_variant_checked (self, OSTREE_SERIALIZED_DIRMETA_VARIANT,
+                                         tree_meta_checksum, &root_metadata, error))
     goto out;
 
   if (!parse_tree (self, tree_contents_checksum, &tree_data, error))
@@ -1946,31 +1960,31 @@ ostree_repo_iter_objects (OstreeRepo  *self,
 }
 
 gboolean
-ostree_repo_load_variant (OstreeRepo *repo,
-                            const char   *sha256,
-                            OstreeSerializedVariantType *out_type,
-                            GVariant    **out_variant,
-                            GError      **error)
+ostree_repo_load_variant (OstreeRepo *self,
+                          const char   *sha256,
+                          OstreeSerializedVariantType *out_type,
+                          GVariant    **out_variant,
+                          GError      **error)
 {
   gboolean ret = FALSE;
   OstreeSerializedVariantType ret_type;
   GVariant *ret_variant = NULL;
+  char *path = NULL;
 
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-  
-  if (!load_gvariant_object_unknown (repo, sha256, &ret_type, &ret_variant, error))
+
+  path = get_object_path (self, sha256, OSTREE_OBJECT_TYPE_META);
+  if (!ostree_parse_metadata_file (path, &ret_type, &ret_variant, error))
     goto out;
 
   ret = TRUE;
   *out_type = ret_type;
   *out_variant = ret_variant;
+  ret_variant = NULL;
  out:
-  if (!ret)
-    {
-      if (ret_variant)
-        g_variant_unref (ret_variant);
-      g_prefix_error (error, "Failed to load metadata variant '%s': ", sha256);
-    }
+  if (ret_variant)
+    g_variant_unref (ret_variant);
+  g_free (path);
   return ret;
 }
 
diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h
index 15d5036..e9a690a 100644
--- a/src/libostree/ostree-repo.h
+++ b/src/libostree/ostree-repo.h
@@ -103,6 +103,12 @@ gboolean      ostree_repo_load_variant (OstreeRepo *self,
                                           GVariant    **out_variant,
                                           GError      **error);
 
+gboolean      ostree_repo_load_variant_checked (OstreeRepo  *self,
+                                                OstreeSerializedVariantType expected_type,
+                                                const char    *sha256, 
+                                                GVariant     **out_variant,
+                                                GError       **error);
+
 gboolean      ostree_repo_commit (OstreeRepo   *self,
                                   const char   *branch,
                                   const char   *parent,
diff --git a/src/libotutil/ot-opt-utils.c b/src/libotutil/ot-opt-utils.c
new file mode 100644
index 0000000..54d107c
--- /dev/null
+++ b/src/libotutil/ot-opt-utils.c
@@ -0,0 +1,37 @@
+/* -*- 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 <gio/gio.h>
+
+#include <string.h>
+
+#include "otutil.h"
+
+void
+ot_util_usage_error (GOptionContext *context, const char *message, GError **error)
+{
+  gchar *help = g_option_context_get_help (context, TRUE, NULL);
+  g_printerr ("%s\n", help);
+  g_free (help);
+  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, message);
+}
diff --git a/src/libotutil/ot-opt-utils.h b/src/libotutil/ot-opt-utils.h
new file mode 100644
index 0000000..b65996f
--- /dev/null
+++ b/src/libotutil/ot-opt-utils.h
@@ -0,0 +1,33 @@
+/* -*- 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>
+ */
+
+#ifndef __OSTREE_GIO_UTILS_H__
+#define __OSTREE_GIO_UTILS_H__
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+void ot_util_usage_error (GOptionContext *context, const char *message, GError **error);
+
+G_END_DECLS
+
+#endif
diff --git a/src/libotutil/otutil.h b/src/libotutil/otutil.h
index 38cfd8e..2c49819 100644
--- a/src/libotutil/otutil.h
+++ b/src/libotutil/otutil.h
@@ -23,5 +23,6 @@
 
 #include <ot-unix-utils.h>
 #include <ot-gio-utils.h>
+#include <ot-opt-utils.h>
 
 #endif
diff --git a/src/ot-builtin-pull.c b/src/ot-builtin-pull.c
index a9ba6e0..65a5206 100644
--- a/src/ot-builtin-pull.c
+++ b/src/ot-builtin-pull.c
@@ -35,16 +35,6 @@ static GOptionEntry options[] = {
   { NULL }
 };
 
-static void
-usage_error (GOptionContext *context, const char *message, GError **error)
-{
-  gchar *help = g_option_context_get_help (context, TRUE, NULL);
-  g_printerr ("%s\n", help);
-  g_free (help);
-  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                       message);
-}
-
 static gboolean
 fetch_uri (OstreeRepo  *repo,
            SoupSession *soup,
@@ -295,7 +285,7 @@ ostree_builtin_pull (int argc, char **argv, const char *prefix, GError **error)
 
   if (argc < 3)
     {
-      usage_error (context, "REMOTE and BRANCH must be specified", error);
+      ot_util_usage_error (context, "REMOTE and BRANCH must be specified", error);
       goto out;
     }
 
diff --git a/src/ot-builtin-rev-parse.c b/src/ot-builtin-rev-parse.c
index 7b24b7e..6e86d6c 100644
--- a/src/ot-builtin-rev-parse.c
+++ b/src/ot-builtin-rev-parse.c
@@ -44,7 +44,7 @@ ostree_builtin_rev_parse (int argc, char **argv, const char *prefix, GError **er
   GVariant *variant = NULL;
   char *formatted_variant = NULL;
 
-  context = g_option_context_new ("- Output the target of a rev");
+  context = g_option_context_new ("REV - Output the target of a rev");
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
@@ -57,8 +57,12 @@ ostree_builtin_rev_parse (int argc, char **argv, const char *prefix, GError **er
   if (!ostree_repo_check (repo, error))
     goto out;
 
-  if (argc > 1)
-    rev = argv[1];
+  if (argc < 2)
+    {
+      ot_util_usage_error (context, "REV must be specified", error);
+      goto out;
+    }
+  rev = argv[1];
 
   if (!ostree_repo_resolve_rev (repo, rev, &resolved_rev, error))
     goto out;
diff --git a/tests/t0000-basic.sh b/tests/t0000-basic.sh
index 3e07b8f..3cd0604 100755
--- a/tests/t0000-basic.sh
+++ b/tests/t0000-basic.sh
@@ -20,7 +20,7 @@
 
 set -e
 
-echo "1..7"
+echo "1..8"
 
 . libtest.sh
 
@@ -35,6 +35,11 @@ echo "ok check"
 ostree checkout $ot_repo test2 checkout-test2
 echo "ok checkout"
 
+ostree rev-parse $ot_repo test2
+ostree rev-parse $ot_repo 'test2^'
+ostree rev-parse $ot_repo 'test2^^' 2>/dev/null && (echo 1>&2 "rev-parse test2^^ unexpectedly succeeded!"; exit 1)
+echo "ok rev-parse"
+
 cd checkout-test2
 assert_has_file firstfile
 assert_has_file baz/cow



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]