Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eadfec

Browse files
committed
start implementing diff driver registry
1 parent 2f77d8f commit 3eadfec

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/diff_driver.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "map.h"
1717
#include "buf_text.h"
1818

19+
GIT__USE_STRMAP;
20+
1921
typedef enum {
2022
DIFF_DRIVER_AUTO = 0,
2123
DIFF_DRIVER_FALSE = 1,
@@ -33,7 +35,7 @@ enum {
3335
struct git_diff_driver {
3436
git_diff_driver_t type;
3537
git_strarray fn_patterns;
36-
int binary;
38+
int binary; /* 0 => treat as text, 1 => treat as binary, -1 => auto */
3739
};
3840

3941
struct git_diff_driver_registry {
@@ -49,26 +51,54 @@ static git_diff_driver global_drivers[3] = {
4951

5052
git_diff_driver_registry *git_diff_driver_registry_new()
5153
{
52-
return git__calloc(1, sizeof(git_diff_driver_registry));
54+
git_diff_driver_registry *reg =
55+
git__calloc(1, sizeof(git_diff_driver_registry));
56+
if (!reg)
57+
return NULL;
58+
59+
if (git_pool_init(&reg->strings, 1, 0) < 0 ||
60+
(reg->drivers = git_strmap_alloc()) == NULL)
61+
{
62+
git_diff_driver_registry_free(reg);
63+
return NULL;
64+
}
65+
66+
return reg;
5367
}
5468

5569
void git_diff_driver_registry_free(git_diff_driver_registry *reg)
5670
{
71+
if (!reg)
72+
return;
73+
74+
git_strmap_free(reg->drivers);
75+
git_pool_clear(&reg->strings);
5776
git__free(reg);
5877
}
5978

79+
static int git_diff_driver_load(
80+
git_diff_driver **out, git_repository *repo, const char *name)
81+
{
82+
GIT_UNUSED(out);
83+
GIT_UNUSED(repo);
84+
GIT_UNUSED(name);
85+
86+
return GIT_ENOTFOUND;
87+
}
88+
6089
int git_diff_driver_lookup(
6190
git_diff_driver **out, git_repository *repo, const char *path)
6291
{
92+
int error = 0;
6393
const char *value;
6494

6595
assert(out);
6696

6797
if (!repo || !path || !strlen(path))
6898
goto use_auto;
6999

70-
if (git_attr_get(&value, repo, 0, path, "diff") < 0)
71-
return -1;
100+
if ((error = git_attr_get(&value, repo, 0, path, "diff")) < 0)
101+
return error;
72102

73103
if (GIT_ATTR_FALSE(value)) {
74104
*out = &global_drivers[DIFF_DRIVER_FALSE];
@@ -81,6 +111,12 @@ int git_diff_driver_lookup(
81111
}
82112

83113
/* otherwise look for driver information in config and build driver */
114+
if ((error = git_diff_driver_load(out, repo, value)) < 0) {
115+
if (error != GIT_ENOTFOUND)
116+
return error;
117+
else
118+
giterr_clear();
119+
}
84120

85121
use_auto:
86122
*out = &global_drivers[DIFF_DRIVER_AUTO];

src/repository.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ void git_repository_free(git_repository *repo)
111111

112112
git_cache_free(&repo->objects);
113113
git_submodule_config_free(repo);
114+
114115
git_diff_driver_registry_free(repo->diff_drivers);
116+
repo->diff_drivers = NULL;
115117

116118
git__free(repo->path_repository);
117119
git__free(repo->workdir);

0 commit comments

Comments
 (0)