[libgit2-glib] Move index entry construction API to repository



commit 00e89328fc71af3e4b5e0545a5ad3e5a1bed1876
Author: Jesse van den Kieboom <jessevdk gmail com>
Date:   Sat Jun 29 13:49:51 2013 +0200

    Move index entry construction API to repository

 libgit2-glib/ggit-index-entry.c |   93 +++++++++++--------------------
 libgit2-glib/ggit-index-entry.h |   22 ++++----
 libgit2-glib/ggit-repository.c  |  117 +++++++++++++++++++++++++++++++++++++++
 libgit2-glib/ggit-repository.h  |   12 ++++
 4 files changed, 174 insertions(+), 70 deletions(-)
---
diff --git a/libgit2-glib/ggit-index-entry.c b/libgit2-glib/ggit-index-entry.c
index 56b68c5..53ad1cc 100644
--- a/libgit2-glib/ggit-index-entry.c
+++ b/libgit2-glib/ggit-index-entry.c
@@ -73,38 +73,19 @@ _ggit_index_entries_wrap (GgitIndex *owner)
 }
 
 GgitIndexEntry *
-ggit_index_entry_new_for_file (GFile   *file,
-                               GgitOId *id)
+_ggit_index_entry_new (const gchar *path,
+                       GgitOId     *id)
 {
        git_index_entry *entry;
        GgitIndexEntry *ret;
 
-       g_return_val_if_fail (G_IS_FILE (file), NULL);
-
        entry = g_slice_new0 (git_index_entry);
 
        ret = ggit_index_entry_wrap (entry, TRUE);
 
-       ggit_index_entry_set_file (ret, file);
+       ggit_index_entry_set_path (ret, path);
        ggit_index_entry_set_id (ret, id);
 
-       ggit_index_entry_stat (ret);
-       return ret;
-}
-
-GgitIndexEntry *
-ggit_index_entry_new_for_path (const gchar *path,
-                               GgitOId     *id)
-{
-       GFile *f;
-       GgitIndexEntry *ret;
-
-       g_return_val_if_fail (path != NULL, NULL);
-
-       f = g_file_new_for_path (path);
-       ret = ggit_index_entry_new_for_file (f, id);
-       g_object_unref (f);
-
        return ret;
 }
 
@@ -623,38 +604,34 @@ ggit_index_entry_set_flags_extended (GgitIndexEntry *entry,
  * ggit_index_entry_get_file:
  * @entry: a #GgitIndexEntry.
  *
- * Get the file of the index entry.
+ * Get the path of the index entry. The path is relative to the working
+ * directory.
  *
- * Returns: (transfer full): a #GFile.
+ * Returns: the path.
  *
  **/
-GFile *
-ggit_index_entry_get_file (GgitIndexEntry *entry)
+const gchar *
+ggit_index_entry_get_path (GgitIndexEntry *entry)
 {
        g_return_val_if_fail (entry != NULL, 0);
 
-       if (entry->entry->path == NULL)
-       {
-               return NULL;
-       }
-
-       return g_file_new_for_path (entry->entry->path);
+       return entry->entry->path;
 }
 
 /**
- * ggit_index_entry_set_file:
+ * ggit_index_entry_set_path:
  * @entry: a #GgitIndexEntry.
- * @file: (allow-none): a #GFile.
+ * @path: (allow-none): the path.
  *
- * Set the file of the index entry.
+ * Set the path of the index entry. The path should be relative to the working
+ * directory.
  *
  **/
 void
-ggit_index_entry_set_file (GgitIndexEntry *entry,
-                           GFile          *file)
+ggit_index_entry_set_path (GgitIndexEntry *entry,
+                           const gchar    *path)
 {
        g_return_if_fail (entry != NULL);
-       g_return_if_fail (file == NULL || G_IS_FILE (file));
        g_return_if_fail (entry->owned);
 
        if (entry->entry->path)
@@ -663,37 +640,36 @@ ggit_index_entry_set_file (GgitIndexEntry *entry,
                entry->entry->path = NULL;
        }
 
-       if (file)
+       if (path)
        {
-               entry->entry->path = g_file_get_path (file);
+               entry->entry->path = g_strdup (path);
        }
 }
 
 /**
  * ggit_index_entry_stat:
  * @entry: a #GgitIndexEntry.
+ * @file: the file to stat.
+ * @error: a #GError
  *
- * Fill the entry fields from statting the entry file. Note that the entry
- * file must be set correctly and the file must exist.
+ * Fill the entry fields from statting @file.
+ *
+ * Returns: %TRUE if the entry was filled from statting @file successfully, %FALSE otherwise.
  *
  **/
-void
-ggit_index_entry_stat (GgitIndexEntry *entry)
+gboolean
+ggit_index_entry_stat (GgitIndexEntry  *entry,
+                       GFile           *file,
+                       GError         **error)
 {
-       GFile *f;
        GFileInfo *info;
 
-       g_return_if_fail (entry != NULL);
-       g_return_if_fail (entry->owned);
-
-       if (entry->entry->path == NULL)
-       {
-               return;
-       }
-
-       f = g_file_new_for_path (entry->entry->path);
+       g_return_val_if_fail (entry != NULL, FALSE);
+       g_return_val_if_fail (G_IS_FILE (file), FALSE);
+       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+       g_return_val_if_fail (entry->owned, FALSE);
 
-       info = g_file_query_info (f,
+       info = g_file_query_info (file,
                                  G_FILE_ATTRIBUTE_STANDARD_SIZE ","
                                  G_FILE_ATTRIBUTE_TIME_MODIFIED ","
                                  G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC ","
@@ -706,13 +682,11 @@ ggit_index_entry_stat (GgitIndexEntry *entry)
                                  G_FILE_ATTRIBUTE_UNIX_GID,
                                  G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                  NULL,
-                                 NULL);
-
-       g_object_unref (f);
+                                 error);
 
        if (!info)
        {
-               return;
+               return FALSE;
        }
 
        entry->entry->file_size = g_file_info_get_size (info);
@@ -754,6 +728,7 @@ ggit_index_entry_stat (GgitIndexEntry *entry)
                                                  G_FILE_ATTRIBUTE_UNIX_GID);
 
        g_object_unref (info);
+       return TRUE;
 }
 
 const git_index_entry *
diff --git a/libgit2-glib/ggit-index-entry.h b/libgit2-glib/ggit-index-entry.h
index 292c799..01c4070 100644
--- a/libgit2-glib/ggit-index-entry.h
+++ b/libgit2-glib/ggit-index-entry.h
@@ -51,12 +51,6 @@ guint             ggit_index_entries_size             (GgitIndexEntries  *entrie
 
 GType             ggit_index_entry_get_type           (void) G_GNUC_CONST;
 
-GgitIndexEntry   *ggit_index_entry_new_for_file       (GFile             *file,
-                                                       GgitOId           *id);
-
-GgitIndexEntry   *ggit_index_entry_new_for_path       (const gchar       *path,
-                                                       GgitOId           *id);
-
 GgitIndexEntry   *ggit_index_entry_ref                (GgitIndexEntry    *entry);
 void              ggit_index_entry_unref              (GgitIndexEntry    *entry);
 
@@ -96,13 +90,19 @@ guint             ggit_index_entry_get_flags_extended (GgitIndexEntry    *entry)
 void              ggit_index_entry_set_flags_extended (GgitIndexEntry    *entry,
                                                        guint              flags_extended);
 
-GFile            *ggit_index_entry_get_file           (GgitIndexEntry    *entry);
-void              ggit_index_entry_set_file           (GgitIndexEntry    *entry,
-                                                       GFile             *file);
+const gchar      *ggit_index_entry_get_path           (GgitIndexEntry    *entry);
+void              ggit_index_entry_set_path           (GgitIndexEntry    *entry,
+                                                       const gchar       *path);
+
+gboolean          ggit_index_entry_stat               (GgitIndexEntry    *entry,
+                                                       GFile             *file,
+                                                       GError           **error);
 
-void              ggit_index_entry_stat               (GgitIndexEntry    *entry);
+const git_index_entry
+                 *_ggit_index_entry_get_native        (GgitIndexEntry    *entry);
 
-const git_index_entry *_ggit_index_entry_get_native         (GgitIndexEntry    *entry);
+GgitIndexEntry   *_ggit_index_entry_new               (const gchar       *path,
+                                                       GgitOId           *id);
 
 G_END_DECLS
 
diff --git a/libgit2-glib/ggit-repository.c b/libgit2-glib/ggit-repository.c
index 3ea7023..7f62131 100644
--- a/libgit2-glib/ggit-repository.c
+++ b/libgit2-glib/ggit-repository.c
@@ -2131,4 +2131,121 @@ ggit_repository_create_tree_builder_from_tree (GgitRepository  *repository,
        return _ggit_tree_builder_wrap (builder, repository, TRUE);
 }
 
+/**
+ * ggit_repository_create_index_entry_for_file:
+ * @repository: a #GgitRepository.
+ * @file: (allow-none): a #GFile.
+ * @id: (allow-none): a #GgitOId.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Create a new index entry. When @file is not %NULL, the path of the returned
+ * entry (#ggit_index_entry_get_path) is set to the path of @file relative to
+ * the working directory of @repository. The file must reside in the working
+ * directory of @repository, must exist and be readable (otherwise %NULL is
+ * returned and @error is set accordingly). The file related
+ * fields of the returned entry are also queried from this file.
+ *
+ * If @id is not %NULL, then the id of the returned entry is set to @id
+ * (see #ggit_index_entry_get_id) which could point to a blob (for a file)
+ * or a tree (for a directory).
+ *
+ * Returns: a #GgitIndexEntry or %NULL when an error occurred.
+ *
+ **/
+GgitIndexEntry *
+ggit_repository_create_index_entry_for_file (GgitRepository  *repository,
+                                             GFile           *file,
+                                             GgitOId         *id,
+                                             GError         **error)
+{
+       gchar *path = NULL;
+       GgitIndexEntry *ret;
+
+       g_return_val_if_fail (GGIT_IS_REPOSITORY (repository), NULL);
+       g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+       if (file != NULL)
+       {
+               path = g_file_get_relative_path (repository->priv->workdir,
+                                                file);
+
+               if (!path)
+               {
+                       g_set_error_literal (error,
+                                            G_IO_ERROR,
+                                            G_IO_ERROR_NOT_FOUND,
+                                            "File is not in the working directory");
+
+                       return NULL;
+               }
+       }
+
+       ret = _ggit_index_entry_new (path, id);
+       g_free (path);
+
+       if (file && !ggit_index_entry_stat (ret, file, error))
+       {
+               ggit_index_entry_unref (ret);
+               return NULL;
+       }
+
+       return ret;
+}
+
+/**
+ * ggit_repository_create_index_entry_for_path:
+ * @repository: a #GgitRepository.
+ * @path: (allow-none): a path.
+ * @id: (allow-none): a #GgitOId.
+ * @error: a #GError for error reporting, or %NULL.
+ *
+ * Create a new index entry. When @path is not %NULL, the path of the returned
+ * entry (#ggit_index_entry_get_path) is set @path. The specified path can be
+ * either absolute or relative, but must exist and be readable (otherwise %NULL
+ * is returned and @error is set accordingly). In the case of
+ * an absolute path, the path must reside within the working directory of
+ * @repository. The file related fields of the returned entry are also queried
+ * from this path.
+ *
+ * If @id is not %NULL, then the id of the returned entry is set to @id
+ * (see #ggit_index_entry_get_id) which could point to a blob (for a file)
+ * or a tree (for a directory).
+ *
+ * Returns: a #GgitIndexEntry or %NULL when an error occurred.
+ *
+ **/
+GgitIndexEntry *
+ggit_repository_create_index_entry_for_path (GgitRepository  *repository,
+                                             const gchar     *path,
+                                             GgitOId         *id,
+                                             GError         **error)
+{
+       GFile *f;
+       GgitIndexEntry *ret;
+
+       g_return_val_if_fail (GGIT_IS_REPOSITORY (repository), NULL);
+       g_return_val_if_fail (path != NULL, NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+       if (!g_path_is_absolute (path))
+       {
+               f = g_file_resolve_relative_path (repository->priv->workdir,
+                                                 path);
+       }
+       else
+       {
+               f = g_file_new_for_path (path);
+       }
+
+       ret = ggit_repository_create_index_entry_for_file (repository,
+                                                          f,
+                                                          id,
+                                                          error);
+
+       g_object_unref (f);
+
+       return ret;
+}
+
 /* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-repository.h b/libgit2-glib/ggit-repository.h
index 53a6acd..b7ca515 100644
--- a/libgit2-glib/ggit-repository.h
+++ b/libgit2-glib/ggit-repository.h
@@ -160,6 +160,18 @@ GgitTreeBuilder    *ggit_repository_create_tree_builder_from_tree (
 GgitTreeBuilder    *ggit_repository_create_tree_builder (
                                                        GgitRepository        *repository);
 
+GgitIndexEntry     *ggit_repository_create_index_entry_for_file (
+                                                       GgitRepository        *repository,
+                                                       GFile                 *file,
+                                                       GgitOId               *id,
+                                                       GError               **error);
+
+GgitIndexEntry     *ggit_repository_create_index_entry_for_path (
+                                                       GgitRepository        *repository,
+                                                       const gchar           *path,
+                                                       GgitOId               *id,
+                                                       GError               **error);
+
 gchar             **ggit_repository_list_tags         (GgitRepository        *repository,
                                                        GError               **error);
 


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