summaryrefslogtreecommitdiff
path: root/include/git2/tree.h
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2016-05-02 17:36:58 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2016-05-17 17:41:05 +0200
commit9464f9ebc1794314421353e10eeddfa8a950f7ab (patch)
treeb01f97aa81d5b0e9108bd45747f1584c96e1bacb /include/git2/tree.h
parentc148533024319689fca016f7c452d556265bb13f (diff)
downloadlibgit2-cmn/tree-update.tar.gz
Introduce a function to create a tree based on a different onecmn/tree-update
Instead of going through the usual steps of reading a tree recursively into an index, modifying it and writing it back out as a tree, introduce a function to perform simple updates more efficiently. `git_tree_create_updated` avoids reading trees which are not modified and supports upsert and delete operations. It is not as versatile as modifying the index, but it makes some common operations much more efficient.
Diffstat (limited to 'include/git2/tree.h')
-rw-r--r--include/git2/tree.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 8a2be2102..2e4735c4b 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -418,6 +418,52 @@ GIT_EXTERN(int) git_tree_walk(
*/
GIT_EXTERN(int) git_tree_dup(git_tree **out, git_tree *source);
+/**
+ * The kind of update to perform
+ */
+typedef enum {
+ /** Update or insert an entry at the specified path */
+ GIT_TREE_UPDATE_UPSERT,
+ /** Remove an entry from the specified path */
+ GIT_TREE_UPDATE_REMOVE,
+} git_tree_update_t;
+
+/**
+ * An action to perform during the update of a tree
+ */
+typedef struct {
+ /** Update action. If it's an removal, only the path is looked at */
+ git_tree_update_t action;
+ /** The entry's id */
+ git_oid id;
+ /** The filemode/kind of object */
+ git_filemode_t filemode;
+ /** The full path from the root tree */
+ const char *path;
+} git_tree_update;
+
+/**
+ * Create a tree based on another one with the specified modifications
+ *
+ * Given the `baseline` perform the changes described in the list of
+ * `updates` and create a new tree.
+ *
+ * This function is optimized for common file/directory addition, removal and
+ * replacement in trees. It is much more efficient than reading the tree into a
+ * `git_index` and modifying that, but in exchange it is not as flexible.
+ *
+ * Deleting and adding the same entry is undefined behaviour, changing
+ * a tree to a blob or viceversa is not supported.
+ *
+ * @param out id of the new tree
+ * @param repo the repository in which to create the tree, must be the
+ * same as for `baseline`
+ * @param baseline the tree to base these changes on
+ * @param nupdates the number of elements in the update list
+ * @param updates the list of updates to perform
+ */
+GIT_EXTERN(int) git_tree_create_updated(git_oid *out, git_repository *repo, git_tree *baseline, size_t nupdates, const git_tree_update *updates);
+
/** @} */
GIT_END_DECL