[Xfce4-commits] <thunar-vcs-plugin:master> Change files to relative paths to git repository.

Peter de Ridder noreply at xfce.org
Tue Aug 3 20:06:06 CEST 2010


Updating branch refs/heads/master
         to 460cbe9abcb4593b78b70331097e00b12f4d4dfc (commit)
       from 7bd57d8bb6c899922dd8519749c28eee587a3375 (commit)

commit 460cbe9abcb4593b78b70331097e00b12f4d4dfc
Author: Peter de Ridder <peter at xfce.org>
Date:   Tue Aug 3 20:00:38 2010 +0200

    Change files to relative paths to git repository.
    
    Find the common prefix for the files and make that the current
    directory.
    Make the files relative paths to the current derectory.
    Solving bug #6411.

 tvp-git-helper/tgh-add.c    |   16 +++++--
 tvp-git-helper/tgh-blame.c  |   21 +++++++---
 tvp-git-helper/tgh-branch.c |    8 ++--
 tvp-git-helper/tgh-clean.c  |   16 +++++--
 tvp-git-helper/tgh-common.c |   94 +++++++++++++++++++++++++++++++++++++++++++
 tvp-git-helper/tgh-common.h |    3 +
 tvp-git-helper/tgh-log.c    |   26 ++++++++----
 tvp-git-helper/tgh-move.c   |   18 ++++++--
 tvp-git-helper/tgh-reset.c  |   16 +++++--
 tvp-git-helper/tgh-stash.c  |    8 ++--
 tvp-git-helper/tgh-status.c |    8 ++--
 11 files changed, 190 insertions(+), 44 deletions(-)

diff --git a/tvp-git-helper/tgh-add.c b/tvp-git-helper/tgh-add.c
index deb782e..41a0799 100644
--- a/tvp-git-helper/tgh-add.c
+++ b/tvp-git-helper/tgh-add.c
@@ -82,18 +82,26 @@ static gboolean add_spawn (GtkWidget *dialog, gchar **files, GPid *pid)
 gboolean tgh_add (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
+  gchar *prefix;
 
-  if (files)
-    if (chdir(files[0]))
+  prefix = tgh_common_prefix (files);
+
+  if (prefix)
+  {
+    if (chdir(prefix))
     {
-      gchar *dirname = g_path_get_dirname (files[0]);
+      gchar *dirname = g_path_get_dirname (prefix);
       if (chdir(dirname))
       {
         g_free (dirname);
         return FALSE;
       }
-      g_free (dirname);
+      g_free (prefix);
+      prefix = dirname;
     }
+    files = tgh_strip_prefix (files, prefix);
+    g_free (prefix);
+  }
 
   dialog = tgh_file_selection_dialog_new (_("Add"), NULL, 0, TGH_FILE_SELECTION_FLAG_MODIFIED|TGH_FILE_SELECTION_FLAG_UNTRACKED);
   if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK)
diff --git a/tvp-git-helper/tgh-blame.c b/tvp-git-helper/tgh-blame.c
index 81c3ae7..abfecf6 100644
--- a/tvp-git-helper/tgh-blame.c
+++ b/tvp-git-helper/tgh-blame.c
@@ -72,19 +72,28 @@ static gboolean blame_spawn (GtkWidget *dialog, gchar *file, GPid *pid)
 gboolean tgh_blame (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
+  gchar *prefix;
 
   if (!files)
     return FALSE;
 
-  if (chdir(files[0]))
+  prefix = tgh_common_prefix (files);
+
+  if (prefix)
   {
-    gchar *dirname = g_path_get_dirname (files[0]);
-    if (chdir(dirname))
+    if (chdir(prefix))
     {
-      g_free (dirname);
-      return FALSE;
+      gchar *dirname = g_path_get_dirname (prefix);
+      if (chdir(dirname))
+      {
+        g_free (dirname);
+        return FALSE;
+      }
+      g_free (prefix);
+      prefix = dirname;
     }
-    g_free (dirname);
+    files = tgh_strip_prefix (files, prefix);
+    g_free (prefix);
   }
 
   dialog = tgh_blame_dialog_new (NULL, NULL, 0);
diff --git a/tvp-git-helper/tgh-branch.c b/tvp-git-helper/tgh-branch.c
index b8b5f9e..098dcb8 100644
--- a/tvp-git-helper/tgh-branch.c
+++ b/tvp-git-helper/tgh-branch.c
@@ -74,16 +74,16 @@ gboolean tgh_branch (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
 
+  if (files)
+    if (chdir(files[0]))
+      return FALSE;
+
   dialog = tgh_branch_dialog_new (NULL, NULL, 0);
   g_signal_connect(dialog, "cancel-clicked", tgh_cancel, NULL);
   tgh_dialog_start (GTK_DIALOG (dialog), TRUE);
 
   g_signal_connect(dialog, "refresh-clicked", G_CALLBACK(create_branch_child), NULL);
 
-  if (files)
-    if (chdir(files[0]))
-      return FALSE;
-
   return branch_spawn(TGH_BRANCH_DIALOG(dialog), pid);
 }
 
diff --git a/tvp-git-helper/tgh-clean.c b/tvp-git-helper/tgh-clean.c
index 7f545a8..8a83d44 100644
--- a/tvp-git-helper/tgh-clean.c
+++ b/tvp-git-helper/tgh-clean.c
@@ -113,18 +113,26 @@ gboolean tgh_clean (gchar **files, GPid *pid)
   GtkWidget *dialog;
   gboolean direcotries, force;
   TghCleanIgnore ignore;
+  gchar *prefix;
 
-  if (files)
-    if (chdir(files[0]))
+  prefix = tgh_common_prefix (files);
+
+  if (prefix)
+  {
+    if (chdir(prefix))
     {
-      gchar *dirname = g_path_get_dirname (files[0]);
+      gchar *dirname = g_path_get_dirname (prefix);
       if (chdir(dirname))
       {
         g_free (dirname);
         return FALSE;
       }
-      g_free (dirname);
+      g_free (prefix);
+      prefix = dirname;
     }
+    files = tgh_strip_prefix (files, prefix);
+    g_free (prefix);
+  }
 
   dialog = tgh_clean_dialog_new (NULL, NULL, 0);
   if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK)
diff --git a/tvp-git-helper/tgh-common.c b/tvp-git-helper/tgh-common.c
index 7ae58c9..14bffc8 100644
--- a/tvp-git-helper/tgh-common.c
+++ b/tvp-git-helper/tgh-common.c
@@ -88,6 +88,100 @@ tgh_cancel(void)
 {
 }
 
+static guint
+path_compare (const gchar *path1, const gchar *path2)
+{
+  const gchar *i1, *i2;
+  gchar c1, c2;
+  gint count = 0;
+  gint depth = 0;
+  i1 = path1;
+  i2 = path2;
+  while ((c1 = *i1++))
+  {
+    c2 = *i2++;
+
+    if (c1 != c2)
+    {
+      if (c1 == '/'&& !c2)
+        depth = count;
+
+      return depth;
+    }
+
+    if (c1 == '/')
+      depth = count;
+
+    count++;
+  }
+
+  if (*i2 == '/')
+      depth = count;
+
+  return depth;
+}
+
+gchar*
+tgh_common_prefix (gchar **files)
+{
+  gchar **iter;
+  gchar *prefix;
+  guint prefix_len, match;
+
+  if (files == NULL || files[0] == NULL)
+    return NULL;
+
+  prefix = g_strdup (files[0]);
+  prefix_len = strlen (prefix);
+
+  for (iter = &files[1]; *iter; iter++)
+  {
+    match = path_compare (prefix, *iter);
+    prefix[match] = '\0';
+    if (match == 0)
+      break;
+    prefix_len = match;
+  }
+
+  return prefix;
+}
+
+gchar**
+tgh_strip_prefix (gchar **files, const gchar *prefix)
+{
+  gchar **stripped;
+  guint len, i;
+  guint prefix_len, start;
+
+  if (files == NULL)
+    return NULL;
+
+  len = g_strv_length (files);
+  stripped = g_new (gchar*, len + 1);
+  stripped[len] = NULL;
+
+  prefix_len = strlen (prefix);
+
+  for (i = 0; i < len; i++)
+  {
+    if (G_LIKELY (g_str_has_prefix (files[i], prefix)))
+    {
+      start = prefix_len;
+      while (files[i][start] == '/')
+	start++;
+      /* prefix is support to be a directory, so if the file fully matches is the current directory */
+      if (G_UNLIKELY (files[i][start] == '\0'))
+	stripped[i] = g_strdup (".");
+      else
+	stripped[i] = g_strdup (files[i] + start);
+    }
+    else
+      stripped[i] = g_strdup (files[i]);
+  }
+
+  return stripped;
+}
+
 typedef struct {
   TghOutputParser parent;
   gchar *error;
diff --git a/tvp-git-helper/tgh-common.h b/tvp-git-helper/tgh-common.h
index a8c26fb..f9542f4 100644
--- a/tvp-git-helper/tgh-common.h
+++ b/tvp-git-helper/tgh-common.h
@@ -23,6 +23,9 @@ void tgh_replace_child  (gboolean, GPid);
 void tgh_cancel         (void);
 void tgh_child_exit     (GPid, gint, gpointer);
 
+gchar* tgh_common_prefix (gchar **files);
+gchar** tgh_strip_prefix (gchar **files, const gchar *prefix);
+
 #define TGH_OUTPUT_PARSER(x) ((TghOutputParser*)(x))
 #define TGH_OUTPUT_PARSER_FUNC(x) ((TghOutputParserFunc)(x))
 
diff --git a/tvp-git-helper/tgh-log.c b/tvp-git-helper/tgh-log.c
index 0959030..929114d 100644
--- a/tvp-git-helper/tgh-log.c
+++ b/tvp-git-helper/tgh-log.c
@@ -96,24 +96,32 @@ static void create_log_child(TghLogDialog *dialog, gpointer user_data)
 gboolean tgh_log (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
+  gchar *prefix;
 
-  dialog = tgh_log_dialog_new (NULL, NULL, 0);
-  g_signal_connect(dialog, "cancel-clicked", tgh_cancel, NULL);
-  tgh_dialog_start (GTK_DIALOG (dialog), TRUE);
+  prefix = tgh_common_prefix (files);
 
-  g_signal_connect(dialog, "refresh-clicked", G_CALLBACK(create_log_child), files);
-
-  if (files)
-    if (chdir(files[0]))
+  if (prefix)
+  {
+    if (chdir(prefix))
     {
-      gchar *dirname = g_path_get_dirname (files[0]);
+      gchar *dirname = g_path_get_dirname (prefix);
       if (chdir(dirname))
       {
         g_free (dirname);
         return FALSE;
       }
-      g_free (dirname);
+      g_free (prefix);
+      prefix = dirname;
     }
+    files = tgh_strip_prefix (files, prefix);
+    g_free (prefix);
+  }
+
+  dialog = tgh_log_dialog_new (NULL, NULL, 0);
+  g_signal_connect(dialog, "cancel-clicked", tgh_cancel, NULL);
+  tgh_dialog_start (GTK_DIALOG (dialog), TRUE);
+
+  g_signal_connect(dialog, "refresh-clicked", G_CALLBACK(create_log_child), files);
 
   return log_spawn(TGH_LOG_DIALOG(dialog), files, pid);
 }
diff --git a/tvp-git-helper/tgh-move.c b/tvp-git-helper/tgh-move.c
index 5c90635..e5c49ce 100644
--- a/tvp-git-helper/tgh-move.c
+++ b/tvp-git-helper/tgh-move.c
@@ -117,21 +117,29 @@ gboolean tgh_move (gchar **files, GPid *pid)
   GtkWidget *dialog;
   gchar *to;
   gboolean multiple = FALSE;
+  gchar *prefix;
 
   if (files && files[0] && files[1])
     multiple = TRUE;
 
-  if (files)
-    if (chdir (files[0]))
+  prefix = tgh_common_prefix (files);
+
+  if (prefix)
+  {
+    if (chdir(prefix))
     {
-      gchar *dirname = g_path_get_dirname (files[0]);
-      if (chdir (dirname))
+      gchar *dirname = g_path_get_dirname (prefix);
+      if (chdir(dirname))
       {
         g_free (dirname);
         return FALSE;
       }
-      g_free (dirname);
+      g_free (prefix);
+      prefix = dirname;
     }
+    files = tgh_strip_prefix (files, prefix);
+    g_free (prefix);
+  }
 
   dialog = gtk_file_chooser_dialog_new (_("Move To"), NULL,
       multiple?GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:GTK_FILE_CHOOSER_ACTION_SAVE,
diff --git a/tvp-git-helper/tgh-reset.c b/tvp-git-helper/tgh-reset.c
index 9ea408b..763e34d 100644
--- a/tvp-git-helper/tgh-reset.c
+++ b/tvp-git-helper/tgh-reset.c
@@ -115,18 +115,26 @@ static gboolean reset_spawn (GtkWidget *dialog, gchar **files, GPid *pid)
 gboolean tgh_reset (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
+  gchar *prefix;
 
-  if (files)
-    if (chdir(files[0]))
+  prefix = tgh_common_prefix (files);
+
+  if (prefix)
+  {
+    if (chdir(prefix))
     {
-      gchar *dirname = g_path_get_dirname (files[0]);
+      gchar *dirname = g_path_get_dirname (prefix);
       if (chdir(dirname))
       {
         g_free (dirname);
         return FALSE;
       }
-      g_free (dirname);
+      g_free (prefix);
+      prefix = dirname;
     }
+    files = tgh_strip_prefix (files, prefix);
+    g_free (prefix);
+  }
 
   dialog = tgh_file_selection_dialog_new (_("Reset"), NULL, 0, TGH_FILE_SELECTION_FLAG_ADDED);
   if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK)
diff --git a/tvp-git-helper/tgh-stash.c b/tvp-git-helper/tgh-stash.c
index fd6945e..824313b 100644
--- a/tvp-git-helper/tgh-stash.c
+++ b/tvp-git-helper/tgh-stash.c
@@ -362,14 +362,14 @@ gboolean tgh_stash (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
 
-  dialog = tgh_stash_dialog_new (NULL, NULL, 0);
-  g_signal_connect(dialog, "cancel-clicked", tgh_cancel, NULL);
-  tgh_dialog_start (GTK_DIALOG (dialog), TRUE);
-
   if (files)
     if (chdir(files[0]))
       return FALSE;
 
+  dialog = tgh_stash_dialog_new (NULL, NULL, 0);
+  g_signal_connect(dialog, "cancel-clicked", tgh_cancel, NULL);
+  tgh_dialog_start (GTK_DIALOG (dialog), TRUE);
+
   g_signal_connect(dialog, "selection-changed", G_CALLBACK (show_stash), NULL);
   g_signal_connect(dialog, "save-clicked", G_CALLBACK (save_stash), NULL);
   g_signal_connect(dialog, "apply-clicked", G_CALLBACK (apply_stash), NULL);
diff --git a/tvp-git-helper/tgh-status.c b/tvp-git-helper/tgh-status.c
index 8b065f5..304df18 100644
--- a/tvp-git-helper/tgh-status.c
+++ b/tvp-git-helper/tgh-status.c
@@ -74,16 +74,16 @@ gboolean tgh_status (gchar **files, GPid *pid)
 {
   GtkWidget *dialog;
 
+  if (files)
+    if (chdir(files[0]))
+      return FALSE;
+
   dialog = tgh_status_dialog_new (NULL, NULL, 0);
   g_signal_connect(dialog, "cancel-clicked", tgh_cancel, NULL);
   tgh_dialog_start (GTK_DIALOG (dialog), TRUE);
 
   g_signal_connect(dialog, "refresh-clicked", G_CALLBACK(create_status_child), NULL);
 
-  if (files)
-    if (chdir(files[0]))
-      return FALSE;
-
   return status_spawn(TGH_STATUS_DIALOG(dialog), pid);
 }
 



More information about the Xfce4-commits mailing list