[gnome-builder] vcs: make ide_vcs_uri_to_string() non-destructive



commit d8bc8400aabf0c292ef4f3c9a749a129f0cfefbf
Author: Christian Hergert <chergert redhat com>
Date:   Mon Nov 28 19:37:53 2016 -0800

    vcs: make ide_vcs_uri_to_string() non-destructive
    
    If we've simply created an IdeVcsUri and not changed it, try harder to
    return the same URI string back via ide_vcs_uri_to_string().

 libide/vcs/ide-vcs-uri.c |   32 +++++++++++++++++++++++++++++++-
 tests/test-ide-vcs-uri.c |   19 ++++++++++---------
 2 files changed, 41 insertions(+), 10 deletions(-)
---
diff --git a/libide/vcs/ide-vcs-uri.c b/libide/vcs/ide-vcs-uri.c
index 08f4614..c9e018c 100644
--- a/libide/vcs/ide-vcs-uri.c
+++ b/libide/vcs/ide-vcs-uri.c
@@ -28,6 +28,13 @@ struct _IdeVcsUri
 {
   volatile gint ref_count;
 
+  /*
+   * If the URI string was created and has not been changed, we try extra
+   * hard to provide the same URI back from ide_vcs_uri_to_string(). This
+   * field is cleared any time any of the other fields are changed.
+   */
+  gchar *non_destructive_uri;
+
   gchar *scheme;
   gchar *user;
   gchar *host;
@@ -35,6 +42,12 @@ struct _IdeVcsUri
   guint  port;
 };
 
+static inline void
+ide_vcs_uri_set_dirty (IdeVcsUri *self)
+{
+  g_clear_pointer (&self->non_destructive_uri, g_free);
+}
+
 static gboolean
 ide_vcs_uri_validate (const IdeVcsUri *self)
 {
@@ -205,7 +218,10 @@ ide_vcs_uri_new (const gchar *uri)
   self->ref_count = 1;
 
   if (ide_vcs_uri_parse (self, uri) && ide_vcs_uri_validate (self))
-    return self;
+    {
+      self->non_destructive_uri = g_strdup (uri);
+      return self;
+    }
 
   g_free (self);
 
@@ -215,6 +231,7 @@ ide_vcs_uri_new (const gchar *uri)
 static void
 ide_vcs_uri_finalize (IdeVcsUri *self)
 {
+  g_free (self->non_destructive_uri);
   g_free (self->scheme);
   g_free (self->user);
   g_free (self->host);
@@ -303,6 +320,8 @@ ide_vcs_uri_set_scheme (IdeVcsUri   *self,
       else
         self->scheme = g_strdup (scheme);
     }
+
+  ide_vcs_uri_set_dirty (self);
 }
 
 void
@@ -325,6 +344,8 @@ ide_vcs_uri_set_user (IdeVcsUri   *self,
       else
         self->user = g_strdup (user);
     }
+
+  ide_vcs_uri_set_dirty (self);
 }
 
 void
@@ -341,6 +362,8 @@ ide_vcs_uri_set_host (IdeVcsUri   *self,
       g_free (self->host);
       self->host = g_strdup (host);
     }
+
+  ide_vcs_uri_set_dirty (self);
 }
 
 void
@@ -351,6 +374,8 @@ ide_vcs_uri_set_port (IdeVcsUri *self,
   g_return_if_fail (port <= G_MAXINT16);
 
   self->port = port;
+
+  ide_vcs_uri_set_dirty (self);
 }
 
 void
@@ -369,6 +394,8 @@ ide_vcs_uri_set_path (IdeVcsUri   *self,
       g_free (self->path);
       self->path = g_strdup (path);
     }
+
+  ide_vcs_uri_set_dirty (self);
 }
 
 gchar *
@@ -378,6 +405,9 @@ ide_vcs_uri_to_string (const IdeVcsUri *self)
 
   g_return_val_if_fail (self, NULL);
 
+  if (self->non_destructive_uri != NULL)
+    return g_strdup (self->non_destructive_uri);
+
   str = g_string_new (NULL);
 
   g_string_append_printf (str, "%s://", self->scheme);
diff --git a/tests/test-ide-vcs-uri.c b/tests/test-ide-vcs-uri.c
index c43710d..8639c55 100644
--- a/tests/test-ide-vcs-uri.c
+++ b/tests/test-ide-vcs-uri.c
@@ -25,22 +25,23 @@ test_sample_uris (void)
     { "ssh://host.xz/~user/path/to/repo.git/", "ssh", NULL,"host.xz", "~user/path/to/repo.git/", 0, 
"ssh://host.xz/~user/path/to/repo.git/" },
     { "ssh://user host xz/~/path/to/repo.git", "ssh", "user", "host.xz", "~/path/to/repo.git",0, "ssh://user 
host xz/~/path/to/repo.git" },
     { "ssh://host.xz/~/path/to/repo.git", "ssh", NULL, "host.xz", "~/path/to/repo.git", 0, 
"ssh://host.xz/~/path/to/repo.git" },
-    { "user host xz:/path/to/repo.git/", "ssh", "user", "host.xz", "/path/to/repo.git/", 0, "ssh://user host 
xz/path/to/repo.git/" },
-    { "host.xz:/path/to/repo.git/", "ssh", NULL, "host.xz", "/path/to/repo.git/", 0, 
"ssh://host.xz/path/to/repo.git/" },
-    { "user host xz:~user/path/to/repo.git/", "ssh", "user", "host.xz", "~user/path/to/repo.git/", 0, 
"ssh://user host xz/~user/path/to/repo.git/" },
-    { "host.xz:~user/path/to/repo.git/", "ssh", NULL, "host.xz", "~user/path/to/repo.git/", 0, 
"ssh://host.xz/~user/path/to/repo.git/" },
-    { "user host xz:path/to/repo.git", "ssh", "user", "host.xz", "~/path/to/repo.git", 0, "ssh://user host 
xz/~/path/to/repo.git" },
-    { "host.xz:path/to/repo.git", "ssh", NULL, "host.xz", "~/path/to/repo.git", 0, 
"ssh://host.xz/~/path/to/repo.git" },
+    { "user host xz:/path/to/repo.git/", "ssh", "user", "host.xz", "/path/to/repo.git/", 0, "user host 
xz:/path/to/repo.git/" },
+    { "host.xz:/path/to/repo.git/", "ssh", NULL, "host.xz", "/path/to/repo.git/", 0, 
"host.xz:/path/to/repo.git/" },
+    { "user host xz:~user/path/to/repo.git/", "ssh", "user", "host.xz", "~user/path/to/repo.git/", 0, "user 
host xz:~user/path/to/repo.git/" },
+    { "host.xz:~user/path/to/repo.git/", "ssh", NULL, "host.xz", "~user/path/to/repo.git/", 0, 
"host.xz:~user/path/to/repo.git/" },
+    { "user host xz:path/to/repo.git", "ssh", "user", "host.xz", "~/path/to/repo.git", 0, "user host 
xz:path/to/repo.git" },
+    { "host.xz:path/to/repo.git", "ssh", NULL, "host.xz", "~/path/to/repo.git", 0, 
"host.xz:path/to/repo.git" },
     { "rsync://host.xz/path/to/repo.git/", "rsync", NULL, "host.xz", "/path/to/repo.git/", 0, 
"rsync://host.xz/path/to/repo.git/" },
     { "git://host.xz/path/to/repo.git/", "git", NULL, "host.xz", "/path/to/repo.git/", 0, 
"git://host.xz/path/to/repo.git/" },
     { "git://host.xz/~user/path/to/repo.git/", "git", NULL, "host.xz", "~user/path/to/repo.git/", 0, 
"git://host.xz/~user/path/to/repo.git/" },
     { "http://host.xz/path/to/repo.git/";, "http", NULL, "host.xz", "/path/to/repo.git/", 0, 
"http://host.xz/path/to/repo.git/"; },
     { "https://host.xz/path/to/repo.git/";, "https", NULL, "host.xz", "/path/to/repo.git/", 0, 
"https://host.xz/path/to/repo.git/"; },
-    { "/path/to/repo.git/", "file", NULL, NULL, "/path/to/repo.git/", 0, "file:///path/to/repo.git/" },
-    { "path/to/repo.git/", "file", NULL, NULL, "path/to/repo.git/", 0, "file://path/to/repo.git/" },
-    { "~/path/to/repo.git", "file", NULL, NULL, "~/path/to/repo.git", 0, "file://~/path/to/repo.git" },
+    { "/path/to/repo.git/", "file", NULL, NULL, "/path/to/repo.git/", 0, "/path/to/repo.git/" },
+    { "path/to/repo.git/", "file", NULL, NULL, "path/to/repo.git/", 0, "path/to/repo.git/" },
+    { "~/path/to/repo.git", "file", NULL, NULL, "~/path/to/repo.git", 0, "~/path/to/repo.git" },
     { "file:///path/to/repo.git/", "file", NULL, NULL, "/path/to/repo.git/", 0, "file:///path/to/repo.git/" 
},
     { "file://~/path/to/repo.git/", "file", NULL, NULL, "~/path/to/repo.git/", 0, 
"file://~/path/to/repo.git/" },
+    { "git github com:example/example.git", "ssh", "git", "github.com", "~/example/example.git", 0, "git 
github com:example/example.git" },
     { NULL }
   };
   guint i;


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