filter: Precache the filter config options on load
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
diff --git a/src/filter.c b/src/filter.c
index ed24ce2..03189ee 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -9,6 +9,8 @@
#include "fileops.h"
#include "hash.h"
#include "filter.h"
+#include "repository.h"
+#include "git2/config.h"
/* Fresh from Core Git. I wonder what we could use this for... */
void git_text__stat(git_text_stats *stats, const git_buf *text)
@@ -163,3 +165,42 @@ int git_filter__apply(git_buf *dest, git_buf *source, git_vector *filters)
return GIT_SUCCESS;
}
+int git_filter__load_settings(git_repository *repo)
+{
+ static git_cvar_map map_eol[] = {
+ {GIT_CVAR_FALSE, NULL, GIT_EOL_UNSET},
+ {GIT_CVAR_STRING, "lf", GIT_EOL_LF},
+ {GIT_CVAR_STRING, "crlf", GIT_EOL_CRLF},
+ {GIT_CVAR_STRING, "native", GIT_EOL_NATIVE}
+ };
+
+ static git_cvar_map map_crlf[] = {
+ {GIT_CVAR_FALSE, NULL, GIT_AUTO_CRLF_FALSE},
+ {GIT_CVAR_TRUE, NULL, GIT_AUTO_CRLF_TRUE},
+ {GIT_CVAR_STRING, "input", GIT_AUTO_CRLF_INPUT}
+ };
+
+ git_config *config;
+ int error;
+
+ repo->filter_options.eol = GIT_EOL_DEFAULT;
+ repo->filter_options.auto_crlf = GIT_AUTO_CRLF_DEFAULT;
+
+ error = git_repository_config__weakptr(&config, repo);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ error = git_config_get_mapped(
+ config, "core.eol", map_eol, ARRAY_SIZE(map_eol), &repo->filter_options.eol);
+
+ if (error < GIT_SUCCESS && error != GIT_ENOTFOUND)
+ return error;
+
+ error = git_config_get_mapped(
+ config, "core.auto_crlf", map_crlf, ARRAY_SIZE(map_crlf), &repo->filter_options.auto_crlf);
+
+ if (error < GIT_SUCCESS && error != GIT_ENOTFOUND)
+ return error;
+
+ return 0;
+}
diff --git a/src/filter.h b/src/filter.h
index 9055fc0..0cf92bd 100644
--- a/src/filter.h
+++ b/src/filter.h
@@ -37,6 +37,7 @@ typedef enum {
GIT_AUTO_CRLF_FALSE = 0,
GIT_AUTO_CRLF_TRUE = 1,
GIT_AUTO_CRLF_INPUT = -1,
+ GIT_AUTO_CRLF_DEFAULT = GIT_AUTO_CRLF_FALSE,
} git_crlf_t;
typedef enum {
@@ -44,10 +45,11 @@ typedef enum {
GIT_EOL_CRLF,
GIT_EOL_LF,
#ifdef GIT_WIN32
- GIT_EOL_NATIVE = GIT_EOL_CRLF
+ GIT_EOL_NATIVE = GIT_EOL_CRLF,
#else
- GIT_EOL_NATIVE = GIT_EOL_LF
+ GIT_EOL_NATIVE = GIT_EOL_LF,
#endif
+ GIT_EOL_DEFAULT = GIT_EOL_NATIVE
} git_eol_t;
typedef struct {
@@ -58,6 +60,7 @@ typedef struct {
unsigned int printable, nonprintable;
} git_text_stats;
+extern int git_filter__load_settings(git_repository *repo);
extern int git_filter__load_for_file(git_vector *filters, git_repository *repo, const char *full_path, int mode);
extern void git_filter__free(git_vector *filters);
extern int git_filter__apply(git_buf *dest, git_buf *source, git_vector *filters);
diff --git a/src/repository.h b/src/repository.h
index fa19d2e..4850502 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -48,7 +48,7 @@ struct git_repository {
unsigned int lru_counter;
struct {
- int core_eol;
+ int eol;
int auto_crlf;
} filter_options;
};