Exemplo n.º 1
0
/* This function will return a string of pathname
 * components from the first directory indicating
 * where two directories diverge. For instance:
 *
 * child: /a/b/c/d/e
 * parent: /a/b/d/
 *
 * Will return: c/d/e
 *
 * If the directories are not at all similar then
 * the full dirname of the child_path effectively
 * be returned.
 *
 * As a special case, child paths that are a URI
 * automatically return the full URI path with
 * the URI scheme and leading slash stripped out.
 */
static char *
find_diverging_child_components(GFile *child,
                                GFile *parent)
{
    g_object_ref(parent);
    GFile *ancestor = parent;
    while (ancestor != NULL) {
        char *relpath = g_file_get_relative_path(ancestor, child);
        if (relpath) {
            g_object_unref(ancestor);
            return relpath;
        }
        GFile *new_ancestor = g_file_get_parent(ancestor);
        g_object_unref(ancestor);
        ancestor = new_ancestor;
    }

    /* This is a special case of getting the URI below. The difference is that
     * this gives you a regular path name; getting it through the URI would
     * give a URI-encoded path (%20 for spaces, etc.) */
    GFile *root = g_file_new_for_path("/");
    char *child_path = g_file_get_relative_path(root, child);
    g_object_unref(root);
    if (child_path)
        return child_path;

    char *child_uri = g_file_get_uri(child);
    char *stripped_uri = strip_uri_scheme(child_uri);
    g_free(child_uri);
    return stripped_uri;
}
static gchar *
ensure_dest_path (GFile *file,
                  gint icon_size)
{
  gchar *str, *str2, *size_string, *dest_path;
  GFile *dest_file, *dest_dir, *tmp;

  str = g_file_get_relative_path (gnome_dir, file);
  tmp = g_file_resolve_relative_path (hc_dir, str);
  g_free (str);

  str = g_file_get_path (tmp);
  size_string = g_strdup_printf ("%dx%d", icon_size, icon_size);
  str2 = replace_str (str, "-symbolic.svg", ".png");
  dest_path = replace_str (str2, "scalable", size_string);

  dest_file = g_file_new_for_path (dest_path);
  dest_dir = g_file_get_parent (dest_file);

  g_file_make_directory_with_parents (dest_dir, NULL, NULL);

  g_object_unref (dest_file);
  g_object_unref (dest_dir);
  g_object_unref (tmp);
  g_free (str);
  g_free (size_string);

  return dest_path;
}
Exemplo n.º 3
0
static void
reference_file_scanned (GFile *scanned_file,
                        GFileInfo *scanned_file_info,
                        gpointer user_data)
{
  ExtractTest *extract_test = user_data;
  g_autofree char *relative_path;
  GFileInfo *corresponding_file_info;

  relative_path = scanned_file == extract_test->reference ?
                                  g_strdup ("") :
                                  g_file_get_relative_path (extract_test->reference,
                                                            scanned_file);

  corresponding_file_info = g_hash_table_lookup (extract_test->unmatched_files,
                                                 relative_path);

  g_assert_nonnull (corresponding_file_info);
  if (corresponding_file_info != NULL) {
    g_assert_cmpuint (g_file_info_get_file_type (scanned_file_info),
                      ==,
                      g_file_info_get_file_type (corresponding_file_info));

    g_hash_table_remove (extract_test->unmatched_files, relative_path);
  }
Exemplo n.º 4
0
char *
panel_launcher_get_filename (const char *location)
{
	GFile *file;
	GFile *launchers_dir;
	char  *launchers_path;
	char  *retval;

	if (!g_path_is_absolute (location) &&
	    g_ascii_strncasecmp (location, "file:", strlen ("file:")))
		/* this is not a local URI */
		return NULL;

	launchers_path = panel_launcher_get_personal_path ();
	launchers_dir = g_file_new_for_path (launchers_path);
	g_free (launchers_path);

	file = panel_launcher_get_gfile (location);

	retval = g_file_get_relative_path (launchers_dir, file);

	g_object_unref (file);
	g_object_unref (launchers_dir);

	return retval;
}
Exemplo n.º 5
0
static gboolean
can_delete_directory (RBAndroidSource *source, GFile *dir)
{
	GMount *mount;
	GFile *root;
	char *path;
	int i;
	int c;

	g_object_get (source, "mount", &mount, NULL);
	root = g_mount_get_root (mount);
	g_object_unref (mount);

	/*
	 * path here will be sdcard/Music/something for anything we want to delete
	 */
	path = g_file_get_relative_path (root, dir);
	c = 0;
	for (i = 0; path[i] != '\0'; i++) {
		if (path[i] == '/')
			c++;
	}

	g_free (path);
	return (c > 1);
}
Exemplo n.º 6
0
static gboolean
add_ref_to_set (const char       *remote,
                GFile            *base,
                GFile            *child,
                GHashTable       *refs,
                GCancellable     *cancellable,
                GError          **error)
{
    gboolean ret = FALSE;
    char *contents;
    char *relpath;
    gsize len;
    GString *refname;

    if (!g_file_load_contents (child, cancellable, &contents, &len, NULL, error))
        goto out;

    g_strchomp (contents);

    refname = g_string_new ("");
    if (remote)
    {
        g_string_append (refname, remote);
        g_string_append_c (refname, ':');
    }
    relpath = g_file_get_relative_path (base, child);
    g_string_append (refname, relpath);
    g_free (relpath);

    g_hash_table_insert (refs, g_string_free (refname, FALSE), contents);

    ret = TRUE;
out:
    return ret;
}
Exemplo n.º 7
0
static IdeProjectFile *
create_file (IdeContext *context,
             GFile      *file,
             GFileType   file_type)
{
  g_autofree gchar *path = NULL;
  g_autofree gchar *name = NULL;
  g_autoptr(GFileInfo) file_info = NULL;
  IdeVcs *vcs;
  GFile *workdir;

  g_assert (IDE_IS_CONTEXT (context));
  g_assert (G_IS_FILE (file));
  g_assert ((file_type == G_FILE_TYPE_DIRECTORY) || (file_type == G_FILE_TYPE_REGULAR));

  vcs = ide_context_get_vcs (context);
  workdir = ide_vcs_get_working_directory (vcs);
  path = g_file_get_relative_path (workdir, file);
  name = g_file_get_basename (file);

  file_info = g_file_info_new ();
  g_file_info_set_file_type (file_info, file_type);
  g_file_info_set_name (file_info, name);
  g_file_info_set_display_name (file_info, name);

  return g_object_new (IDE_TYPE_PROJECT_FILE,
                       "context", context,
                       "file", file,
                       "file-info", file_info,
                       "path", path,
                       "parent", NULL,
                       NULL);
}
Exemplo n.º 8
0
static gchar *
normalize_file_name (const gchar *file_name, const gchar *parent_file_name)
{
  GFile *parent_file = NULL;
  GFile *file;
  gchar *result;

  file = g_file_new_for_path (file_name);

  if (parent_file_name != NULL)
    {
      parent_file = g_file_new_for_path (parent_file_name);
      result = g_file_get_relative_path (parent_file, file);
    }
  else
    {
      result = g_file_get_path (file);
    }

  if (parent_file != NULL)
    g_object_unref (parent_file);

  g_object_unref (file);

  return result;
}
Exemplo n.º 9
0
/* Work even if file is not a descendant of parent */
static gchar*
get_relative_path (GFile *parent, GFile *file)
{
	gchar *relative;

	relative = g_file_get_relative_path (parent, file);
	if (relative == NULL)
	{
		if (g_file_equal (parent, file))
		{
			relative = g_strdup ("");
		}
		else
		{
			GFile *grand_parent = g_file_get_parent (parent);
			gint level;
			gchar *grand_relative;
			gchar *ptr;
			gsize len;


			for (level = 1;  !g_file_has_prefix (file, grand_parent); level++)
			{
				GFile *next = g_file_get_parent (grand_parent);

				g_object_unref (grand_parent);
				grand_parent = next;
			}

			grand_relative = g_file_get_relative_path (grand_parent, file);
			g_object_unref (grand_parent);

			len = strlen (grand_relative);
			relative = g_new (gchar, len + level * 3 + 1);
			ptr = relative;
			for (; level; level--)
			{
				memcpy(ptr, ".." G_DIR_SEPARATOR_S, 3);
				ptr += 3;
			}
			memcpy (ptr, grand_relative, len + 1);
			g_free (grand_relative);
		}
	}

	return relative;
}
Exemplo n.º 10
0
static void
ide_git_vcs_list_status_worker (GTask        *task,
                                gpointer      source_object,
                                gpointer      task_data,
                                GCancellable *cancellable)
{
  ListStatus *state = task_data;
  g_autoptr(GListStore) store = NULL;
  g_autoptr(GFile) workdir = NULL;
  g_autoptr(GgitRepository) repository = NULL;
  g_autoptr(GgitStatusOptions) options = NULL;
  g_autoptr(GString) pathspec = NULL;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *relative = NULL;
  gchar *strv[] = { NULL, NULL };

  g_assert (G_IS_TASK (task));
  g_assert (IDE_IS_GIT_VCS (source_object));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
  g_assert (state != NULL);
  g_assert (G_IS_FILE (state->repository_location));

  if (!(repository = ggit_repository_open (state->repository_location, &error)))
    {
      g_task_return_error (task, g_steal_pointer (&error));
      return;
    }

  if (!(workdir = ggit_repository_get_workdir (repository)))
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_FAILED,
                               "Failed to locate working directory");
      return;
    }

  g_set_object (&state->workdir, workdir);

  if (state->directory_or_file != NULL)
    relative = g_file_get_relative_path (workdir, state->directory_or_file);

  strv[0] = relative;
  options = ggit_status_options_new (GGIT_STATUS_OPTION_DEFAULT,
                                     GGIT_STATUS_SHOW_INDEX_AND_WORKDIR,
                                     (const gchar **)strv);

  store = g_list_store_new (IDE_TYPE_VCS_FILE_INFO);
  g_set_object (&state->store, store);

  if (!ggit_repository_file_status_foreach (repository,
                                            options,
                                            ide_git_vcs_list_status_cb,
                                            state,
                                            &error))
    g_task_return_error (task, g_steal_pointer (&error));
  else
    g_task_return_pointer (task, g_steal_pointer (&store), g_object_unref);
}
Exemplo n.º 11
0
void
test_utils_add_test_for_all_files (const gchar   *prefix,
                                   GFile         *base,
                                   GFile         *file,
                                   GTestDataFunc  test_func,
                                   AddTestFunc    add_test_func)
{
  GFileEnumerator *enumerator;
  GFileInfo *info;
  GList *l, *files;
  GError *error = NULL;


  if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
    {
      gchar *test_path;
      gchar *relative_path;

      if (base)
        relative_path = g_file_get_relative_path (base, file);
      else
        relative_path = g_file_get_path (file);

      test_path = g_strconcat (prefix, "/", relative_path, NULL);
      
      g_test_add_data_func_full (test_path, g_object_ref (file), test_func, g_object_unref);
      return;
    }


  enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
  g_assert_no_error (error);
  files = NULL;

  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
    {
      GFile *next_file = g_file_get_child (file, g_file_info_get_name (info));

      if (add_test_func == NULL || add_test_func (next_file))
        {
          files = g_list_prepend (files, g_object_ref (next_file));
        }

      g_object_unref (next_file);
      g_object_unref (info);
    }
  
  g_assert_no_error (error);
  g_object_unref (enumerator);

  files = g_list_sort (files, compare_files);

  for (l = files; l; l = l->next)
    {
      test_utils_add_test_for_all_files (prefix, base, l->data, test_func, add_test_func);
    }

  g_list_free_full (files, g_object_unref);
}
IdeBufferChangeMonitor *
gbp_git_buffer_change_monitor_new (IdeBuffer         *buffer,
                                   IpcGitRepository  *repository,
                                   GFile             *file,
                                   GCancellable      *cancellable,
                                   GError           **error)
{
  GbpGitBufferChangeMonitor *ret;
  g_autoptr(IpcGitChangeMonitor) proxy = NULL;
  g_autoptr(IdeContext) context = NULL;
  g_autoptr(GFile) workdir = NULL;
  g_autofree gchar *relative_path = NULL;
  g_autofree gchar *obj_path = NULL;
  GDBusConnection *connection;

  g_return_val_if_fail (IDE_IS_MAIN_THREAD (), NULL);
  g_return_val_if_fail (IDE_IS_BUFFER (buffer), NULL);
  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), NULL);

  context = ide_buffer_ref_context (buffer);
  workdir = ide_context_ref_workdir (context);

  if (!g_file_has_prefix (file, workdir))
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_NOT_SUPPORTED,
                   "Cannot monitor files outside the working directory");
      return NULL;
    }

  relative_path = g_file_get_relative_path (workdir, file);

  if (!ipc_git_repository_call_create_change_monitor_sync (repository,
                                                           relative_path,
                                                           &obj_path,
                                                           cancellable,
                                                           error))
    return NULL;

  connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (repository));

  if (!(proxy = ipc_git_change_monitor_proxy_new_sync (connection,
                                                       G_DBUS_PROXY_FLAGS_NONE,
                                                       NULL,
                                                       obj_path,
                                                       cancellable,
                                                       error)))
    return NULL;

  ret = g_object_new (GBP_TYPE_GIT_BUFFER_CHANGE_MONITOR,
                      "buffer", buffer,
                      NULL);
  ret->proxy = g_steal_pointer (&proxy);

  return IDE_BUFFER_CHANGE_MONITOR (g_steal_pointer (&ret));
}
Exemplo n.º 13
0
gboolean
directory_has_makefile_am (BasicAutotoolsPlugin *bb_plugin,  GFile *dir)
{
	GFile *file;
	gboolean exists;

	/* We need configure.ac or configure.in too */
	if (bb_plugin->project_root_dir == NULL) return FALSE;

	exists = TRUE;
	file = g_file_get_child (bb_plugin->project_root_dir,  "configure.ac");
	if (!g_file_query_exists (file, NULL))
	{
		g_object_unref (file);
		file =  g_file_get_child (bb_plugin->project_root_dir,  "configure.in");
		if (!g_file_query_exists (file, NULL))
		{
			exists = FALSE;
		}
	}
	g_object_unref (file);

	/* Check for Makefile.am or GNUmakefile.am */
	if (g_file_has_prefix (dir, bb_plugin->project_build_dir))
	{
		/* Check for Makefile.am in source directory not build directory */
		gchar *relative;
		GFile *src_dir;

		relative = g_file_get_relative_path (bb_plugin->project_build_dir, dir);
		src_dir = g_file_get_child (bb_plugin->project_root_dir, relative);
		file = g_file_get_child (src_dir,  "Makefile.am");
		g_object_unref (src_dir);
		g_free (relative);
	}
	else if (g_file_equal (dir, bb_plugin->project_build_dir))
	{
		file = g_file_get_child (bb_plugin->project_root_dir,  "Makefile.am");
	}
	else
	{
		file = g_file_get_child (dir,  "Makefile.am");
	}

	if (!g_file_query_exists (file, NULL))
	{
		g_object_unref (file);
		file =  g_file_get_child (dir, "GNUmakefile.am");
		if (!g_file_query_exists (file, NULL))
		{
			exists = FALSE;
		}
	}
	g_object_unref (file);

	return exists;
}
Exemplo n.º 14
0
static GList *
nautilus_directory_moved_internal (GFile *old_location,
				   GFile *new_location)
{
	CollectData collection;
	NautilusDirectory *directory;
	GList *node, *affected_files;
	GFile *new_directory_location;
	char *relative_path;

	collection.container = old_location;
	collection.directories = NULL;

	g_hash_table_foreach (directories,
			      collect_directories_by_container,
			      &collection);

	affected_files = NULL;

	for (node = collection.directories; node != NULL; node = node->next) {
		directory = NAUTILUS_DIRECTORY (node->data);
		new_directory_location = NULL;

		if (g_file_equal (directory->details->location, old_location)) {
			new_directory_location = g_object_ref (new_location);
		} else {
			relative_path = g_file_get_relative_path (old_location,
								  directory->details->location);
			if (relative_path != NULL) {
				new_directory_location = g_file_resolve_relative_path (new_location, relative_path);
				g_free (relative_path);
				
			}
		}
		
		if (new_directory_location) {
			change_directory_location (directory, new_directory_location);
			g_object_unref (new_directory_location);
		
			/* Collect affected files. */
			if (directory->details->as_file != NULL) {
				affected_files = g_list_prepend
					(affected_files,
					 nautilus_file_ref (directory->details->as_file));
			}
			affected_files = g_list_concat
				(affected_files,
				 nautilus_file_list_copy (directory->details->file_list));
		}
		
		nautilus_directory_unref (directory);
	}

	g_list_free (collection.directories);

	return affected_files;
}
Exemplo n.º 15
0
Arquivo: gvfs-ls.c Projeto: snnw/gvfs
static void
show_completed_file (GFile *hit,
                     gboolean is_dir,
                     const char *arg)
{
  char *path, *cwd, *display, *t;
  GFile *cwd_f;
  GFile *home;
  
  if (g_file_is_native (hit))
    {
      cwd = g_get_current_dir ();
      cwd_f = g_file_new_for_path (cwd);
      g_free (cwd);

      home = g_file_new_for_path (g_get_home_dir ());

      if ((g_file_has_prefix (hit, home) ||
           g_file_equal (hit, home)) &&
          arg[0] == '~')
        {
          t = g_file_get_relative_path (home, hit);
          path = g_strconcat ("~", (t != NULL) ? "/": "", t, NULL);
          g_free (t);
        }
      else if (g_file_has_prefix (hit, cwd_f) &&
               !g_path_is_absolute (arg))
        path = g_file_get_relative_path (cwd_f, hit);
      else
        path = g_file_get_path (hit);

      g_object_unref (cwd_f);
      g_object_unref (home);
      
      display = shell_quote (path);
      g_free (path);
    }
  else
    display = g_file_get_uri (hit);
  
  g_print ("%s%s\n", display, (is_dir)?"/":"");
  g_free (display);
}
Exemplo n.º 16
0
static void
print_diff_item (char        prefix,
                 GFile      *base,
                 GFile      *file)
{
  if (g_file_is_native (file))
    {
      g_autofree char *relpath = g_file_get_relative_path (base, file);
      g_print ("%c    %s\n", prefix, relpath);
    }
  else
    {
      g_print ("%c    %s\n", prefix, gs_file_get_path_cached (file));
    }
}
Exemplo n.º 17
0
static void
output_file_scanned (GFile *scanned_file,
                     GFileInfo *scanned_file_info,
                     gpointer user_data)
{
  ExtractTest *extract_test = user_data;
  char *relative_path;

  relative_path = scanned_file == extract_test->output ?
                                  g_strdup ("") :
                                  g_file_get_relative_path (extract_test->output,
                                                            scanned_file);

  g_hash_table_insert (extract_test->unmatched_files, relative_path,
                       g_object_ref (scanned_file_info));
}
Exemplo n.º 18
0
static void
list_source (IAnjutaProject *project, AnjutaProjectNode *root, AnjutaProjectNode *source, gint indent, const gchar *path)
{
	GFile *file;
	gchar *rel_path;

	if (source == NULL) return;

	file = anjuta_project_node_get_file (source);
	rel_path = g_file_get_relative_path (anjuta_project_node_get_file (root), file);
	print ("%*sSOURCE (%s): %s", indent * INDENT, "", no_id ? "" : path, rel_path);

	list_property (project, source, indent + 1);

	g_free (rel_path);
}
Exemplo n.º 19
0
static GInputStream *
tmpl_template_locator_locate_in_path (TmplTemplateLocator *self,
                                      const gchar         *path_base,
                                      const gchar         *path)
{
  GInputStream *ret = NULL;
  gchar *full_path;

  g_assert (TMPL_IS_TEMPLATE_LOCATOR (self));
  g_assert (path_base != NULL);
  g_assert (path != NULL);

  full_path = g_build_path (path_base, path, NULL);

  if (g_str_has_prefix (full_path, "resource://"))
    {
      /*
       * A mediocre attempt to prevent escapes using ../
       */
      if (strstr (full_path, "..") == NULL)
        ret = g_resources_open_stream (full_path + strlen ("resource://"), 0, NULL);
    }
  else
    {
      GFile *parent = g_file_new_for_path (path_base);
      GFile *file = g_file_new_for_path (full_path);
      gchar *relative;

      /*
       * If the path tries to escape the search path, using ../../ or
       * something clever, we will get an invalid path here.
       */
      if ((relative = g_file_get_relative_path (parent, file)))
        {
          g_free (relative);
          ret = (GInputStream *)g_file_read (file, NULL, NULL);
        }

      g_object_unref (parent);
      g_object_unref (file);
    }

  g_free (full_path);

  return ret;
}
Exemplo n.º 20
0
static void
list_group (IAnjutaProject *project, AnjutaProjectNode *root, AnjutaProjectNode *group, gint indent, const gchar *path)
{
	AnjutaProjectNode *parent;
	gchar *rel_path;
	GFile *file;

	parent = anjuta_project_node_parent (group);
	file = anjuta_project_node_get_file (parent);
	rel_path = g_file_get_relative_path (file, anjuta_project_node_get_file (group));
	print ("%*sGROUP (%s): %s", indent * INDENT, "", no_id ? "" : path, rel_path);
	g_free (rel_path);

	list_property (project, group, indent + 1);

	list_children (project, root, group, indent, path);
}
Exemplo n.º 21
0
void
gb_project_tree_reveal (GbProjectTree *self,
                        GFile         *file)
{
  g_autofree gchar *relpath = NULL;
  g_auto(GStrv) parts = NULL;
  IdeContext *context;
  IdeTreeNode *node;
  IdeVcs *vcs;
  GFile *workdir;
  guint i;

  g_return_if_fail (GB_IS_PROJECT_TREE (self));
  g_return_if_fail (G_IS_FILE (file));

  context = gb_project_tree_get_context (self);
  g_assert (IDE_IS_CONTEXT (context));

  if (context == NULL)
    return;

  vcs = ide_context_get_vcs (context);
  workdir = ide_vcs_get_working_directory (vcs);
  relpath = g_file_get_relative_path (workdir, file);

  if (relpath == NULL)
    return;

  node = ide_tree_find_child_node (IDE_TREE (self), NULL, find_files_node, NULL);
  if (node == NULL)
    return;

  parts = g_strsplit (relpath, G_DIR_SEPARATOR_S, 0);

  for (i = 0; parts [i]; i++)
    {
      node = ide_tree_find_child_node (IDE_TREE (self), node, find_child_node, parts [i]);
      if (node == NULL)
        return;
    }

  ide_tree_expand_to_node (IDE_TREE (self), node);
  ide_tree_scroll_to_node (IDE_TREE (self), node);
  ide_tree_node_select (node);
}
Exemplo n.º 22
0
/**
 * gs_file_get_relpath:
 * @one: The first #GFile
 * @two: The second #GFile
 *
 * Like gs_file_get_relative_path(), but does not mandate that
 * the two files have any parent in common. This function will
 * instead insert "../" where appropriate.
 *
 * Returns: (transfer full): The relative path between the two.
 */
gchar *
gs_file_get_relpath (GFile *one,
                     GFile *two)
{
    gchar *simple_path;
    gchar *one_path, *one_suffix;
    gchar *two_path, *two_suffix;
    GString *path;
    int i;

    simple_path = g_file_get_relative_path (one, two);
    if (simple_path)
        return simple_path;

    one_path = g_file_get_path (one);
    two_path = g_file_get_path (two);

    i = path_common_directory (one_path, two_path);
    one_suffix = one_path + i;
    two_suffix = two_path + i;

    path = g_string_new ("");

    /* For every leftover path segment one has, append "../" so
     * that we reach the same directory. */
    while (*one_suffix)
    {
        g_string_append (path, "../");
        one_suffix = strchr (one_suffix, '/');
        if (one_suffix == NULL)
            break;
        one_suffix++;
    }

    /* And now append the leftover stuff on two's side. */
    g_string_append (path, two_suffix);

    g_free (one_path);
    g_free (two_path);

    return g_string_free (path, FALSE);
}
Exemplo n.º 23
0
static gboolean
ide_git_vcs_is_ignored (IdeVcs  *vcs,
                        GFile   *file,
                        GError **error)
{
  g_autofree gchar *name = NULL;
  IdeGitVcs *self = (IdeGitVcs *)vcs;
  gboolean ret = FALSE;

  g_assert (IDE_IS_GIT_VCS (self));
  g_assert (G_IS_FILE (file));

  name = g_file_get_relative_path (self->working_directory, file);
  if (g_strcmp0 (name, ".git") == 0)
    return TRUE;

  if (name != NULL)
    return ggit_repository_path_is_ignored (self->repository, name, error);

  return ret;
}
Exemplo n.º 24
0
/**
 * ide_project_files_find_file:
 * @self: (in): A #IdeProjectFiles.
 * @file: A #GFile.
 *
 * Tries to locate an #IdeProjectFile matching the given file.
 * If @file is the working directory, @self is returned.
 *
 * Returns: (transfer none) (nullable): An #IdeProjectItem or %NULL.
 */
IdeProjectItem *
ide_project_files_find_file (IdeProjectFiles *self,
                             GFile           *file)
{
  IdeProjectItem *item;
  IdeContext *context;
  IdeVcs *vcs;
  GFile *workdir;
  gchar **parts;
  gchar *path;
  gsize i;

  g_return_val_if_fail (IDE_IS_PROJECT_FILES (self), NULL);
  g_return_val_if_fail (G_IS_FILE (file), NULL);

  item = IDE_PROJECT_ITEM (self);
  context = ide_object_get_context (IDE_OBJECT (self));
  vcs = ide_context_get_vcs (context);
  workdir = ide_vcs_get_working_directory (vcs);

  if (g_file_equal (workdir, file))
    return IDE_PROJECT_ITEM (self);

  path = g_file_get_relative_path (workdir, file);
  if (path == NULL)
    return NULL;

  parts = g_strsplit (path, G_DIR_SEPARATOR_S, 0);

  for (i = 0; parts [i]; i++)
    {
      if (!(item = ide_project_files_find_child (item, parts [i])))
        break;
    }

  g_strfreev (parts);
  g_free (path);

  return item;
}
Exemplo n.º 25
0
/* utf8 */
gchar *get_relative_path(const gchar *utf8_parent, const gchar *utf8_descendant)
{
	GFile *gf_parent, *gf_descendant;
	gchar *locale_parent, *locale_descendant;
	gchar *locale_ret, *utf8_ret;

	locale_parent = utils_get_locale_from_utf8(utf8_parent);
	locale_descendant = utils_get_locale_from_utf8(utf8_descendant);
	gf_parent = g_file_new_for_path(locale_parent);
	gf_descendant = g_file_new_for_path(locale_descendant);

	locale_ret = g_file_get_relative_path(gf_parent, gf_descendant);
	utf8_ret = utils_get_utf8_from_locale(locale_ret);

	g_object_unref(gf_parent);
	g_object_unref(gf_descendant);
	g_free(locale_parent);
	g_free(locale_descendant);
	g_free(locale_ret);

	return utf8_ret;
}
Exemplo n.º 26
0
/**
 * ggit_index_entries_get_by_path:
 * @entries: a #GgitIndexEntries.
 * @file: the path to search.
 * @stage: stage to search.
 *
 * Get a #GgitIndexEntry by index. Note that the returned #GgitIndexEntry is
 * _only_ valid as long as:
 *
 * 1) The associated index has not been closed
 * 2) The entry has not been removed (see ggit_index_remove())
 * 3) The index has not been refreshed (see ggit_index_read())
 *
 * Changes to the #GgitIndexEntry will be reflected in the index once written
 * back to disk using ggit_index_write().
 *
 * @stage indicates the stage to search the file for. Stages are used in the
 * index when merge conflicts occur, such that multiple versions of the same
 * file can be represented in the index. Stage 0 is associated with the working
 * tree, while stages 1 to 3 are associated with the various versions of the
 * file in a merge conflict. The special value -1 can be used to match the first
 * file encountered in any stage.
 *
 * Returns: (transfer full): a #GgitIndexEntry or %NULL if it was not found.
 *
 **/
GgitIndexEntry *
ggit_index_entries_get_by_path (GgitIndexEntries *entries,
                                GFile            *file,
                                gint              stage)
{
	git_index *gidx;
	const git_index_entry *entry;
	gchar *path;
	GgitRepository *repo;
	GFile *wd;

	g_return_val_if_fail (entries != NULL, NULL);
	g_return_val_if_fail (G_IS_FILE (file), NULL);
	g_return_val_if_fail (stage >= 0 && stage <= 3, NULL);

	repo = ggit_index_get_owner (entries->owner);

	wd = ggit_repository_get_workdir (repo);
	path = g_file_get_relative_path (wd, file);

	g_object_unref (wd);
	g_object_unref (repo);

	g_return_val_if_fail (path != NULL, NULL);

	gidx = _ggit_index_get_index (entries->owner);
	entry = git_index_get_bypath (gidx, path, stage);
	g_free (path);

	if (entry)
	{
		return ggit_index_entry_wrap ((git_index_entry *)entry, FALSE);
	}
	else
	{
		return NULL;
	}
}
Exemplo n.º 27
0
static gboolean
ide_git_vcs_is_ignored (IdeVcs  *vcs,
                        GFile   *file,
                        GError **error)
{
  g_autofree gchar *name = NULL;
  IdeGitVcs *self = (IdeGitVcs *)vcs;
  gboolean ret = FALSE;

  g_assert (IDE_IS_GIT_VCS (self));
  g_assert (G_IS_FILE (file));

  /* Note: this function is required to be thread-safe so that workers
   *       can check if files are ignored from a thread without
   *       round-tripping to the main thread.
   */

  /* self->working_directory is not changed after creation, so safe
   * to access it from a thread.
   */
  name = g_file_get_relative_path (self->working_directory, file);
  if (g_strcmp0 (name, ".git") == 0)
    return TRUE;

  /*
   * If we have a valid name to work with, we want to query the
   * repository. But this could be called from a thread, so ensure
   * we are the only thread accessing self->repository right now.
   */
  if (name != NULL)
    {
      g_mutex_lock (&self->repository_mutex);
      ret = ggit_repository_path_is_ignored (self->repository, name, error);
      g_mutex_unlock (&self->repository_mutex);
    }

  return ret;
}
Exemplo n.º 28
0
/* Return build path from a source directory */
static GFile *
build_file_from_directory (BasicAutotoolsPlugin *plugin, GFile *directory)
{
	GFile *build_file;

	if ((plugin->project_root_dir == NULL) || (plugin->project_build_dir == NULL))
	{
		/* No change if there is no project or no build directory */
		build_file = g_object_ref (directory);
	}
	else if (g_file_has_prefix (directory, plugin->project_build_dir) || g_file_equal (directory, plugin->project_build_dir))
	{
		/* No change, already in build directory */
		build_file = g_object_ref (directory);
	}
	else if (g_file_equal (directory, plugin->project_root_dir))
	{
		/* Use build directory instead of source directory */
		build_file = g_object_ref (plugin->project_build_dir);
	}
	else if (g_file_has_prefix (directory, plugin->project_root_dir))
	{
		/* Get corresponding file in build directory */
		gchar *relative;

		relative = g_file_get_relative_path (plugin->project_root_dir, directory);
		build_file = g_file_resolve_relative_path (plugin->project_build_dir, relative);
		g_free (relative);
	}
	else
	{
		/* File outside the project directory */
		build_file = g_object_ref (directory);
	}

	return build_file;
}
Exemplo n.º 29
0
void
watcher_event(GFileMonitor *monitor, GFile *file, GFile *other_file,
    GFileMonitorEvent event_type, gpointer user_data)
{
  GFile *parent, *top, *tmp;
  gboolean depth = 1;
  watcher_t *watcher;
  watcher_event_t *event;

  if (!user_data)
    return;

  watcher = (watcher_t *) user_data;

  parent = g_file_new_for_path(watcher->path);

  event = (watcher_event_t *) g_new0(watcher_event_t, 1);
  event->watcher = watcher;
  event->file = g_file_get_path(file);
  event->rfile = g_file_get_relative_path(parent, file);
  if (!event->rfile)
    event->rfile = g_strdup("");

  if (watcher->recursive)
    {
      for (top = g_file_dup(file), depth = 1;
          !g_file_equal(top, parent) && !g_file_has_parent(top, parent); top =
              tmp, depth++)
        {
          tmp = g_file_get_parent(top);

          g_object_unref(top);
        }

      g_object_unref(top);

      LOG_DEBUG("%s: file depth to watcher path is '%d'", watcher->name, depth);
    }

  g_object_unref(parent);

  LOG_DEBUG("%s: %s (event_type=%d, file=%s)",
      watcher->name, N_("watcher event received"), event_type, event->file);

  switch (event_type)
  {
  case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
    {
      event->event = g_strdup(CONFIG_KEY_WATCHER_EVENT_CHANGING);

      break;
    }

  case G_FILE_MONITOR_EVENT_CHANGED:
    {
      event->event = g_strdup(CONFIG_KEY_WATCHER_EVENT_CHANGED);

      break;
    }

  case G_FILE_MONITOR_EVENT_CREATED:
    {
      event->event = g_strdup(CONFIG_KEY_WATCHER_EVENT_CREATED);

      break;
    }

  case G_FILE_MONITOR_EVENT_DELETED:
    {
      event->event = g_strdup(CONFIG_KEY_WATCHER_EVENT_DELETED);

      break;
    }

  case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
    {
      event->event = g_strdup(CONFIG_KEY_WATCHER_EVENT_ATTRIBUTECHANGED);

      break;
    }

  default:
    {
      LOG_DEBUG("%s: %s (event_type=%d)",
          watcher->name, N_("unknown event"), event_type);

      g_free(event->file);
      g_free(event->rfile);
      g_free(event);

      return;

      break;
    }
  }

  if ((event_type == G_FILE_MONITOR_EVENT_DELETED) && watcher->recursive
      && (g_strcmp0(event->file, watcher->path) != 0))
    {
      watcher_remove_monitor_for_recursive_path(watcher, event->file);
    }

  if (!watcher_event_test(watcher, event))
    {
      LOG_DEBUG("%s: %s (event=%s, file=%s)",
          watcher->name, N_("event ignored"), event->event, event->file);

      g_free(event->event);
      g_free(event->file);
      g_free(event->rfile);
      g_free(event);

      return;
    }

  if (event_type == G_FILE_MONITOR_EVENT_CREATED && watcher->recursive
      && g_file_test(event->file, G_FILE_TEST_IS_DIR)
  && (g_strcmp0(event->file, watcher->path) != 0))
    {
      watcher_add_monitor_for_recursive_path(watcher, event->file, depth);
    }

  watcher_event_fired(watcher, event);

  g_free(event->event);
  g_free(event->file);
  g_free(event->rfile);
  g_free(event);
}
Exemplo n.º 30
0
/* Return build path and target from a GFile */
GFile *
build_file_from_file (BasicAutotoolsPlugin *plugin, GFile *file, gchar **target)
{

	if (target != NULL) *target = NULL;

	if (file == NULL)
	{
		/* Use project root directory */
		return build_file_from_directory (plugin, plugin->project_root_dir);
	}
	else if (g_file_query_file_type (file, 0, NULL) == G_FILE_TYPE_DIRECTORY)
	{
		return build_file_from_directory (plugin, file);
	}
	else
	{
		GFile *parent = NULL;
		GFile *build_file;
		IAnjutaProjectManager* projman;

		projman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
					                            IAnjutaProjectManager,
					                            NULL);

		if (projman != NULL)
		{
			/* Use the project manager to find the group file */
			GFile *child;

			for (child = normalize_project_file (file, plugin->project_root_dir); child != NULL;)
			{
				GFile *group;
				AnjutaProjectNodeType type;

				type = ianjuta_project_manager_get_target_type (projman, child, NULL);
				if (type == ANJUTA_PROJECT_GROUP) break;
				group = ianjuta_project_manager_get_parent (projman, child, NULL);
				g_object_unref (child);
				child = group;
			}
			parent = child;
		}

		if (parent == NULL)
		{
			/* Fallback use parent directory */
			parent = g_file_get_parent (file);
		}

		if (parent != NULL)
		{
			if (target != NULL) *target = g_file_get_relative_path (parent, file);
			build_file = build_file_from_directory (plugin, parent);
			g_object_unref (parent);

			return build_file;
		}
		else
		{
			return NULL;
		}
	}
}