Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
libgit2/src/filter.c at development · feinstaub/libgit2 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
feinstaub
libgit2
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
development
Breadcrumbs
libgit2
/
src
/
filter.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
166 lines (135 loc) · 3.6 KB
development
Breadcrumbs
libgit2
/
src
/
filter.c
Copy path
Top
File metadata and controls
Code
Blame
166 lines (135 loc) · 3.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include
"common.h"
#include
"fileops.h"
#include
"hash.h"
#include
"filter.h"
#include
"repository.h"
#include
"git2/config.h"
#include
"blob.h"
/* Tweaked from Core Git. I wonder what we could use this for... */
void
git_text_gather_stats
(
git_text_stats
*
stats
,
const
git_buf
*
text
)
{
size_t
i
;
memset
(
stats
,
0
,
sizeof
(
*
stats
));
for
(
i
=
0
;
i
<
git_buf_len
(
text
);
i
++
) {
unsigned
char
c
=
text
->
ptr
[
i
];
if
(
c
==
'\r'
) {
stats
->
cr
++
;
if
(
i
+
1
<
git_buf_len
(
text
)
&&
text
->
ptr
[
i
+
1
]
==
'\n'
)
stats
->
crlf
++
;
}
else
if
(
c
==
'\n'
)
stats
->
lf
++
;
else
if
(
c
==
0x85
)
/* Unicode CR+LF */
stats
->
crlf
++
;
else
if
(
c
==
127
)
/* DEL */
stats
->
nonprintable
++
;
else
if
(
c
<=
0x1F
||
(
c
>=
0x80
&&
c
<=
0x9F
)) {
switch
(
c
) {
/* BS, HT, ESC and FF */
case
'\b'
:
case
'\t'
:
case
'\033'
:
case
'\014'
:
stats
->
printable
++
;
break
;
case
0
:
stats
->
nul
++
;
/* fall through */
default
:
stats
->
nonprintable
++
;
}
}
else
stats
->
printable
++
;
}
/* If file ends with EOF then don't count this EOF as non-printable. */
if
(
git_buf_len
(
text
) >=
1
&&
text
->
ptr
[
text
->
size
-
1
]
==
'\032'
)
stats
->
nonprintable
--
;
}
/*
* Fresh from Core Git
*/
int
git_text_is_binary
(
git_text_stats
*
stats
)
{
if
(
stats
->
nul
)
return
1
;
if
((
stats
->
printable
>>
7
)
<
stats
->
nonprintable
)
return
1
;
/*
* Other heuristics? Average line length might be relevant,
* as might LF vs CR vs CRLF counts..
*
* NOTE! It might be normal to have a low ratio of CRLF to LF
* (somebody starts with a LF-only file and edits it with an editor
* that adds CRLF only to lines that are added..). But do we
* want to support CR-only? Probably not.
*/
return
0
;
}
int
git_filters_load
(
git_vector
*
filters
,
git_repository
*
repo
,
const
char
*
path
,
int
mode
)
{
int
error
;
if
(
mode
==
GIT_FILTER_TO_ODB
) {
/* Load the CRLF cleanup filter when writing to the ODB */
error
=
git_filter_add__crlf_to_odb
(
filters
,
repo
,
path
);
if
(
error
<
0
)
return
error
;
}
else
{
error
=
git_filter_add__crlf_to_workdir
(
filters
,
repo
,
path
);
if
(
error
<
0
)
return
error
;
}
return
(
int
)
filters
->
length
;
}
void
git_filters_free
(
git_vector
*
filters
)
{
size_t
i
;
git_filter
*
filter
;
git_vector_foreach
(
filters
,
i
,
filter
) {
if
(
filter
->
do_free
!=
NULL
)
filter
->
do_free
(
filter
);
else
git__free
(
filter
);
}
git_vector_free
(
filters
);
}
int
git_filters_apply
(
git_buf
*
dest
,
git_buf
*
source
,
git_vector
*
filters
)
{
unsigned
int
i
,
src
;
git_buf
*
dbuffer
[
2
];
dbuffer
[
0
]
=
source
;
dbuffer
[
1
]
=
dest
;
src
=
0
;
if
(
git_buf_len
(
source
)
==
0
) {
git_buf_clear
(
dest
);
return
0
;
}
/* Pre-grow the destination buffer to more or less the size
* we expect it to have */
if
(
git_buf_grow
(
dest
,
git_buf_len
(
source
))
<
0
)
return
-1
;
for
(
i
=
0
;
i
<
filters
->
length
;
++
i
) {
git_filter
*
filter
=
git_vector_get
(
filters
,
i
);
unsigned
int
dst
=
1
-
src
;
git_buf_clear
(
dbuffer
[
dst
]);
/* Apply the filter from dbuffer[src] to the other buffer;
* if the filtering is canceled by the user mid-filter,
* we skip to the next filter without changing the source
* of the double buffering (so that the text goes through
* cleanly).
*/
if
(
filter
->
apply
(
filter
,
dbuffer
[
dst
],
dbuffer
[
src
])
==
0
)
src
=
dst
;
if
(
git_buf_oom
(
dbuffer
[
dst
]))
return
-1
;
}
/* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */
if
(
src
!=
1
)
git_buf_swap
(
dest
,
source
);
return
0
;
}
You can’t perform that action at this time.