Skip to content

Commit d7e8b93

Browse files
committed
filter: add git_filter_options
Allow filter users to provide an options structure instead of simply flags. This allows for future growth for filter options.
1 parent 1db5b21 commit d7e8b93

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

include/git2/filter.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ typedef enum {
5151
GIT_FILTER_ATTRIBUTES_FROM_HEAD = (1u << 2),
5252
} git_filter_flag_t;
5353

54+
/**
55+
* Filtering options
56+
*/
57+
typedef struct {
58+
unsigned int version;
59+
60+
/** See `git_filter_flag_t` above */
61+
uint32_t flags;
62+
} git_filter_options;
63+
64+
#define GIT_FILTER_OPTIONS_VERSION 1
65+
#define GIT_FILTER_OPTIONS_INIT {GIT_FILTER_OPTIONS_VERSION}
66+
5467
/**
5568
* A filter that can transform file data
5669
*
@@ -103,6 +116,29 @@ GIT_EXTERN(int) git_filter_list_load(
103116
git_filter_mode_t mode,
104117
uint32_t flags);
105118

119+
/**
120+
* Load the filter list for a given path.
121+
*
122+
* This will return 0 (success) but set the output git_filter_list to NULL
123+
* if no filters are requested for the given file.
124+
*
125+
* @param filters Output newly created git_filter_list (or NULL)
126+
* @param repo Repository object that contains `path`
127+
* @param blob The blob to which the filter will be applied (if known)
128+
* @param path Relative path of the file to be filtered
129+
* @param mode Filtering direction (WT->ODB or ODB->WT)
130+
* @param opts The `git_filter_options` to use when loading filters
131+
* @return 0 on success (which could still return NULL if no filters are
132+
* needed for the requested file), <0 on error
133+
*/
134+
GIT_EXTERN(int) git_filter_list_load_ext(
135+
git_filter_list **filters,
136+
git_repository *repo,
137+
git_blob *blob,
138+
const char *path,
139+
git_filter_mode_t mode,
140+
git_filter_options *opts);
141+
106142
/**
107143
* Query the filter list to see if a given filter (by name) will run.
108144
* The built-in filters "crlf" and "ident" can be queried, otherwise this

src/filter.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
#include "array.h"
2020

2121
struct git_filter_source {
22-
git_repository *repo;
23-
const char *path;
24-
git_oid oid; /* zero if unknown (which is likely) */
25-
uint16_t filemode; /* zero if unknown */
26-
git_filter_mode_t mode;
27-
uint32_t flags;
22+
git_repository *repo;
23+
const char *path;
24+
git_oid oid; /* zero if unknown (which is likely) */
25+
uint16_t filemode; /* zero if unknown */
26+
git_filter_mode_t mode;
27+
git_filter_options options;
2828
};
2929

3030
typedef struct {
@@ -396,7 +396,7 @@ git_filter_mode_t git_filter_source_mode(const git_filter_source *src)
396396

397397
uint32_t git_filter_source_flags(const git_filter_source *src)
398398
{
399-
return src->flags;
399+
return src->options.flags;
400400
}
401401

402402
static int filter_list_new(
@@ -416,7 +416,8 @@ static int filter_list_new(
416416
fl->source.repo = src->repo;
417417
fl->source.path = fl->path;
418418
fl->source.mode = src->mode;
419-
fl->source.flags = src->flags;
419+
420+
memcpy(&fl->source.options, &src->options, sizeof(git_filter_options));
420421

421422
*out = fl;
422423
return 0;
@@ -436,10 +437,10 @@ static int filter_list_check_attributes(
436437

437438
GIT_ERROR_CHECK_ALLOC(strs);
438439

439-
if ((src->flags & GIT_FILTER_NO_SYSTEM_ATTRIBUTES) != 0)
440+
if ((src->options.flags & GIT_FILTER_NO_SYSTEM_ATTRIBUTES) != 0)
440441
attr_opts.flags |= GIT_ATTR_CHECK_NO_SYSTEM;
441442

442-
if ((src->flags & GIT_FILTER_ATTRIBUTES_FROM_HEAD) != 0)
443+
if ((src->options.flags & GIT_FILTER_ATTRIBUTES_FROM_HEAD) != 0)
443444
attr_opts.flags |= GIT_ATTR_CHECK_INCLUDE_HEAD;
444445

445446
error = git_attr_get_many_with_session(
@@ -488,7 +489,7 @@ int git_filter_list_new(
488489
src.repo = repo;
489490
src.path = NULL;
490491
src.mode = mode;
491-
src.flags = flags;
492+
src.options.flags = flags;
492493
return filter_list_new(out, &src);
493494
}
494495

@@ -515,7 +516,8 @@ int git_filter_list__load(
515516
src.repo = repo;
516517
src.path = path;
517518
src.mode = mode;
518-
src.flags = filter_session->flags;
519+
520+
memcpy(&src.options, &filter_session->options, sizeof(git_filter_options));
519521

520522
if (blob)
521523
git_oid_cpy(&src.oid, git_blob_id(blob));
@@ -581,6 +583,23 @@ int git_filter_list__load(
581583
return error;
582584
}
583585

586+
int git_filter_list_load_ext(
587+
git_filter_list **filters,
588+
git_repository *repo,
589+
git_blob *blob, /* can be NULL */
590+
const char *path,
591+
git_filter_mode_t mode,
592+
git_filter_options *opts)
593+
{
594+
git_filter_session filter_session = GIT_FILTER_SESSION_INIT;
595+
596+
if (opts)
597+
memcpy(&filter_session.options, opts, sizeof(git_filter_options));
598+
599+
return git_filter_list__load(
600+
filters, repo, blob, path, mode, &filter_session);
601+
}
602+
584603
int git_filter_list_load(
585604
git_filter_list **filters,
586605
git_repository *repo,
@@ -591,7 +610,7 @@ int git_filter_list_load(
591610
{
592611
git_filter_session filter_session = GIT_FILTER_SESSION_INIT;
593612

594-
filter_session.flags = flags;
613+
filter_session.options.flags = flags;
595614

596615
return git_filter_list__load(
597616
filters, repo, blob, path, mode, &filter_session);

src/filter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
#define GIT_FILTER_BYTES_TO_CHECK_NUL 8000
1717

1818
typedef struct {
19-
uint32_t flags;
19+
git_filter_options options;
2020
git_attr_session *attr_session;
2121
git_buf *temp_buf;
2222
} git_filter_session;
2323

24-
#define GIT_FILTER_SESSION_INIT {0}
24+
#define GIT_FILTER_SESSION_INIT {GIT_FILTER_OPTIONS_INIT, 0}
2525

2626
extern int git_filter_global_init(void);
2727

0 commit comments

Comments
 (0)