[ostree] core: Allow resolving e.g. "origin/master" as a rev



commit db9801bff2e721f707fb64a5a4b84f4927d0742a
Author: Colin Walters <walters verbum org>
Date:   Thu Nov 17 10:36:04 2011 -0500

    core: Allow resolving e.g. "origin/master" as a rev
    
    Also:
    
    * Disallow .. in revs to avoid looking outside where we should be
    * Allow resolving empty revs

 src/libostree/ostree-repo.c       |   69 +++++++++++++++++++++++++------------
 src/libostree/ostree-repo.h       |    1 +
 src/ostree/ot-builtin-compose.c   |    2 +-
 src/ostree/ot-builtin-log.c       |    2 +-
 src/ostree/ot-builtin-pull.c      |   34 ++++++++++++++----
 src/ostree/ot-builtin-rev-parse.c |    2 +-
 src/ostree/ot-builtin-show.c      |    4 +-
 7 files changed, 79 insertions(+), 35 deletions(-)
---
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index fca253b..a2d2170 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -249,12 +249,12 @@ parse_rev_file (OstreeRepo     *self,
   return ret;
 }
 
-static gboolean
-resolve_rev (OstreeRepo     *self,
-             const char     *rev,
-             gboolean        allow_noent,
-             char          **sha256,
-             GError        **error)
+gboolean
+ostree_repo_resolve_rev (OstreeRepo     *self,
+                         const char     *rev,
+                         gboolean        allow_noent,
+                         char          **sha256,
+                         GError        **error)
 {
   OstreeRepoPrivate *priv = GET_PRIVATE (self);
   gboolean ret = FALSE;
@@ -262,16 +262,25 @@ resolve_rev (OstreeRepo     *self,
   char *tmp2 = NULL;
   char *ret_rev = NULL;
   GFile *child = NULL;
+  GFile *origindir = NULL;
   char *child_path = NULL;
   GError *temp_error = NULL;
   GVariant *commit = NULL;
 
+  g_return_val_if_fail (rev != NULL, FALSE);
+
   if (strlen (rev) == 0)
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                    "Invalid empty rev");
       goto out;
     }
+  else if (strstr (rev, "..") != NULL)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "Invalid rev %s", rev);
+      goto out;
+    }
   else if (strlen (rev) == 64)
     {
       ret_rev = g_strdup (rev);
@@ -281,7 +290,7 @@ resolve_rev (OstreeRepo     *self,
       tmp = g_strdup (rev);
       tmp[strlen(tmp) - 1] = '\0';
 
-      if (!resolve_rev (self, tmp, allow_noent, &tmp2, error))
+      if (!ostree_repo_resolve_rev (self, tmp, allow_noent, &tmp2, error))
         goto out;
 
       if (!ostree_repo_load_variant_checked (self, OSTREE_SERIALIZED_COMMIT_VARIANT, tmp2, &commit, error))
@@ -298,8 +307,33 @@ resolve_rev (OstreeRepo     *self,
     }
   else
     {
-      child = g_file_get_child (priv->local_heads_dir, rev);
-      child_path = g_file_get_path (child);
+      const char *slash = strchr (rev, '/');
+      if (slash != NULL && (slash == rev || !*(slash+1)))
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "Invalid rev %s", rev);
+          goto out;
+        }
+      else if (slash == NULL)
+        {
+          child = g_file_get_child (priv->local_heads_dir, rev);
+          child_path = g_file_get_path (child);
+        }
+      else
+        {
+          const char *rest = slash + 1;
+
+          if (strchr (rest, '/'))
+            {
+              g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "Invalid rev %s", rev);
+              goto out;
+            }
+          
+          child = g_file_get_child (priv->remote_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))
@@ -333,21 +367,12 @@ resolve_rev (OstreeRepo     *self,
   g_free (tmp);
   g_free (tmp2);
   g_clear_object (&child);
+  g_clear_object (&origindir);
   g_free (child_path);
   g_free (ret_rev);
   return ret;
 }
 
-gboolean
-ostree_repo_resolve_rev (OstreeRepo     *self,
-                         const char     *rev,
-                         char          **sha256,
-                         GError        **error)
-{
-  g_return_val_if_fail (rev != NULL, FALSE);
-  return resolve_rev (self, rev, FALSE, sha256, error);
-}
-
 static gboolean
 write_checksum_file (GFile *parentdir,
                      const char *name,
@@ -1450,7 +1475,7 @@ ostree_repo_commit_from_filelist_fd (OstreeRepo *self,
   if (!import_root (self, base, &root, error))
     goto out;
 
-  if (!resolve_rev (self, parent, TRUE, &current_head, error))
+  if (!ostree_repo_resolve_rev (self, parent, TRUE, &current_head, error))
     goto out;
 
   in = (GUnixInputStream*)g_unix_input_stream_new (fd, FALSE);
@@ -1802,7 +1827,7 @@ ostree_repo_checkout (OstreeRepo *self,
       goto out;
     }
 
-  if (!resolve_rev (self, rev, FALSE, &resolved, error))
+  if (!ostree_repo_resolve_rev (self, rev, FALSE, &resolved, error))
     goto out;
 
   root = (OstreeRepoFile*)_ostree_repo_file_new_root (self, resolved);
@@ -2175,7 +2200,7 @@ ostree_repo_read_commit (OstreeRepo *self,
   GFile *ret_root = NULL;
   char *resolved_rev = NULL;
 
-  if (!resolve_rev (self, rev, FALSE, &resolved_rev, error))
+  if (!ostree_repo_resolve_rev (self, rev, FALSE, &resolved_rev, error))
     goto out;
 
   ret_root = _ostree_repo_file_new_root (self, resolved_rev);
diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h
index 927fcae..1c7eeb3 100644
--- a/src/libostree/ostree-repo.h
+++ b/src/libostree/ostree-repo.h
@@ -86,6 +86,7 @@ gboolean      ostree_repo_store_object_trusted (OstreeRepo   *self,
 
 gboolean      ostree_repo_resolve_rev (OstreeRepo  *self,
                                        const char  *rev,
+                                       gboolean     allow_noent,
                                        char       **out_resolved,
                                        GError     **error);
 
diff --git a/src/ostree/ot-builtin-compose.c b/src/ostree/ot-builtin-compose.c
index 31cfacc..b2cdb40 100644
--- a/src/ostree/ot-builtin-compose.c
+++ b/src/ostree/ot-builtin-compose.c
@@ -187,7 +187,7 @@ compose_branch_on_dir (OstreeRepo *repo,
   gboolean ret = FALSE;
   char *branchrev = NULL;
 
-  if (!ostree_repo_resolve_rev (repo, branch, &branchrev, error))
+  if (!ostree_repo_resolve_rev (repo, branch, FALSE, &branchrev, error))
     goto out;
   
   destpath = g_file_get_path (destination);
diff --git a/src/ostree/ot-builtin-log.c b/src/ostree/ot-builtin-log.c
index ff82fde..fe3723e 100644
--- a/src/ostree/ot-builtin-log.c
+++ b/src/ostree/ot-builtin-log.c
@@ -64,7 +64,7 @@ ostree_builtin_log (int argc, char **argv, const char *repo_path, GError **error
   if (!ot_util_spawn_pager (&pager, error))
     goto out;
 
-  if (!ostree_repo_resolve_rev (repo, rev, &resolved_rev, error))
+  if (!ostree_repo_resolve_rev (repo, rev, FALSE, &resolved_rev, error))
     goto out;
 
   while (TRUE)
diff --git a/src/ostree/ot-builtin-pull.c b/src/ostree/ot-builtin-pull.c
index d702219..a064abd 100644
--- a/src/ostree/ot-builtin-pull.c
+++ b/src/ostree/ot-builtin-pull.c
@@ -263,6 +263,8 @@ ostree_builtin_pull (int argc, char **argv, const char *repo_path, GError **erro
   char *baseurl = NULL;
   char *refpath = NULL;
   char *temppath = NULL;
+  char *remote_ref = NULL;
+  char *original_rev = NULL;
   GKeyFile *config = NULL;
   SoupURI *base_uri = NULL;
   SoupURI *target_uri = NULL;
@@ -288,6 +290,11 @@ ostree_builtin_pull (int argc, char **argv, const char *repo_path, GError **erro
   remote = argv[1];
   branch = argv[2];
 
+  remote_ref = g_strdup_printf ("%s/%s", remote, branch);
+
+  if (!ostree_repo_resolve_rev (repo, remote_ref, TRUE, &original_rev, error))
+    goto out;
+
   config = ostree_repo_get_config (repo);
 
   key = g_strdup_printf ("remote \"%s\"", remote);
@@ -319,14 +326,23 @@ ostree_builtin_pull (int argc, char **argv, const char *repo_path, GError **erro
     goto out;
   g_strchomp (rev);
 
-  if (!ostree_validate_checksum_string (rev, error))
-    goto out;
-
-  if (!store_commit_recurse (repo, soup, base_uri, rev, error))
-    goto out;
-
-  if (!ostree_repo_write_ref (repo, FALSE, branch, rev, error))
-    goto out;
+  if (original_rev && strcmp (rev, original_rev) == 0)
+    {
+      g_print ("No changes in %s\n", remote_ref);
+    }
+  else
+    {
+      if (!ostree_validate_checksum_string (rev, error))
+        goto out;
+      
+      if (!store_commit_recurse (repo, soup, base_uri, rev, error))
+        goto out;
+      
+      if (!ostree_repo_write_ref (repo, remote, branch, rev, error))
+        goto out;
+      
+      g_print ("remote %s is now %s\n", remote_ref, rev);
+    }
  
   ret = TRUE;
  out:
@@ -337,6 +353,8 @@ ostree_builtin_pull (int argc, char **argv, const char *repo_path, GError **erro
   g_free (temppath);
   g_free (key);
   g_free (rev);
+  g_free (remote_ref);
+  g_free (original_rev);
   g_free (baseurl);
   g_free (refpath);
   g_free (remote_branch_ref_path);
diff --git a/src/ostree/ot-builtin-rev-parse.c b/src/ostree/ot-builtin-rev-parse.c
index 3a83cd7..6868c10 100644
--- a/src/ostree/ot-builtin-rev-parse.c
+++ b/src/ostree/ot-builtin-rev-parse.c
@@ -59,7 +59,7 @@ ostree_builtin_rev_parse (int argc, char **argv, const char *repo_path, GError *
     }
   rev = argv[1];
 
-  if (!ostree_repo_resolve_rev (repo, rev, &resolved_rev, error))
+  if (!ostree_repo_resolve_rev (repo, rev, FALSE, &resolved_rev, error))
     goto out;
 
   g_print ("%s\n", resolved_rev);
diff --git a/src/ostree/ot-builtin-show.c b/src/ostree/ot-builtin-show.c
index 0440cf3..452a360 100644
--- a/src/ostree/ot-builtin-show.c
+++ b/src/ostree/ot-builtin-show.c
@@ -216,7 +216,7 @@ ostree_builtin_show (int argc, char **argv, const char *repo_path, GError **erro
     }
   else if (print_compose)
     {
-      if (!ostree_repo_resolve_rev (repo, rev, &resolved_rev, error))
+      if (!ostree_repo_resolve_rev (repo, rev, FALSE, &resolved_rev, error))
         goto out;
 
       if (!do_print_compose (repo, rev, resolved_rev, error))
@@ -229,7 +229,7 @@ ostree_builtin_show (int argc, char **argv, const char *repo_path, GError **erro
     }
   else
     {
-      if (!ostree_repo_resolve_rev (repo, rev, &resolved_rev, error))
+      if (!ostree_repo_resolve_rev (repo, rev, FALSE, &resolved_rev, error))
         goto out;
 
       if (!show_repo_meta (repo, rev, resolved_rev, error))



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