Add higher level pathspec API Right now, setting up a pathspec to be parsed and processed requires several data structures and a couple of API calls. This adds a new high level data structure that contains all the items that you'll need and high-level APIs that do all of the setup and all of the teardown. This will make it easier to use pathspecs in various places with less repeated code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
diff --git a/src/pathspec.c b/src/pathspec.c
index 35c79ce..f029836 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -166,3 +166,28 @@ bool git_pathspec_match_path(
return false;
}
+
+int git_pathspec_context_init(
+ git_pathspec_context *ctxt, const git_strarray *paths)
+{
+ int error = 0;
+
+ memset(ctxt, 0, sizeof(*ctxt));
+
+ ctxt->prefix = git_pathspec_prefix(paths);
+
+ if ((error = git_pool_init(&ctxt->pool, 1, 0)) < 0 ||
+ (error = git_pathspec_init(&ctxt->pathspec, paths, &ctxt->pool)) < 0)
+ git_pathspec_context_free(ctxt);
+
+ return error;
+}
+
+void git_pathspec_context_free(
+ git_pathspec_context *ctxt)
+{
+ git__free(ctxt->prefix);
+ git_pathspec_free(&ctxt->pathspec);
+ git_pool_clear(&ctxt->pool);
+ memset(ctxt, 0, sizeof(*ctxt));
+}
diff --git a/src/pathspec.h b/src/pathspec.h
index 43a94ba..f6509df 100644
--- a/src/pathspec.h
+++ b/src/pathspec.h
@@ -37,4 +37,18 @@ extern bool git_pathspec_match_path(
bool casefold,
const char **matched_pathspec);
+/* easy pathspec setup */
+
+typedef struct {
+ char *prefix;
+ git_vector pathspec;
+ git_pool pool;
+} git_pathspec_context;
+
+extern int git_pathspec_context_init(
+ git_pathspec_context *ctxt, const git_strarray *paths);
+
+extern void git_pathspec_context_free(
+ git_pathspec_context *ctxt);
+
#endif