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/pathspec.c at development · barrbrain/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 }}
barrbrain
libgit2
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
development
Breadcrumbs
libgit2
/
src
/
pathspec.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
152 lines (120 loc) · 3.53 KB
development
Breadcrumbs
libgit2
/
src
/
pathspec.c
Copy path
Top
File metadata and controls
Code
Blame
152 lines (120 loc) · 3.53 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
/*
* 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
"pathspec.h"
#include
"buf_text.h"
#include
"attr_file.h"
/* what is the common non-wildcard prefix for all items in the pathspec */
char
*
git_pathspec_prefix
(
const
git_strarray
*
pathspec
)
{
git_buf
prefix
=
GIT_BUF_INIT
;
const
char
*
scan
;
if
(!
pathspec
||
!
pathspec
->
count
||
git_buf_text_common_prefix
(
&
prefix
,
pathspec
)
<
0
)
return
NULL
;
/* diff prefix will only be leading non-wildcards */
for
(
scan
=
prefix
.
ptr
;
*
scan
;
++
scan
) {
if
(
git__iswildcard
(
*
scan
)
&&
(
scan
==
prefix
.
ptr
||
(
*
(
scan
-
1
)
!=
'\\'
)))
break
;
}
git_buf_truncate
(
&
prefix
,
scan
-
prefix
.
ptr
);
if
(
prefix
.
size
<=
0
) {
git_buf_free
(
&
prefix
);
return
NULL
;
}
git_buf_text_unescape
(
&
prefix
);
return
git_buf_detach
(
&
prefix
);
}
/* is there anything in the spec that needs to be filtered on */
bool
git_pathspec_is_interesting
(
const
git_strarray
*
pathspec
)
{
const
char
*
str
;
if
(
pathspec
==
NULL
||
pathspec
->
count
==
0
)
return
false;
if
(
pathspec
->
count
>
1
)
return
true;
str
=
pathspec
->
strings
[
0
];
if
(!
str
||
!
str
[
0
]
||
(!
str
[
1
]
&&
(
str
[
0
]
==
'*'
||
str
[
0
]
==
'.'
)))
return
false;
return
true;
}
/* build a vector of fnmatch patterns to evaluate efficiently */
int
git_pathspec_init
(
git_vector
*
vspec
,
const
git_strarray
*
strspec
,
git_pool
*
strpool
)
{
size_t
i
;
memset
(
vspec
,
0
,
sizeof
(
*
vspec
));
if
(!
git_pathspec_is_interesting
(
strspec
))
return
0
;
if
(
git_vector_init
(
vspec
,
strspec
->
count
,
NULL
)
<
0
)
return
-1
;
for
(
i
=
0
;
i
<
strspec
->
count
;
++
i
) {
int
ret
;
const
char
*
pattern
=
strspec
->
strings
[
i
];
git_attr_fnmatch
*
match
=
git__calloc
(
1
,
sizeof
(
git_attr_fnmatch
));
if
(!
match
)
return
-1
;
match
->
flags
=
GIT_ATTR_FNMATCH_ALLOWSPACE
;
ret
=
git_attr_fnmatch__parse
(
match
,
strpool
,
NULL
,
&
pattern
);
if
(
ret
==
GIT_ENOTFOUND
) {
git__free
(
match
);
continue
;
}
else
if
(
ret
<
0
)
return
ret
;
if
(
git_vector_insert
(
vspec
,
match
)
<
0
)
return
-1
;
}
return
0
;
}
/* free data from the pathspec vector */
void
git_pathspec_free
(
git_vector
*
vspec
)
{
git_attr_fnmatch
*
match
;
unsigned
int
i
;
git_vector_foreach
(
vspec
,
i
,
match
) {
git__free
(
match
);
vspec
->
contents
[
i
]
=
NULL
;
}
git_vector_free
(
vspec
);
}
/* match a path against the vectorized pathspec */
bool
git_pathspec_match_path
(
git_vector
*
vspec
,
const
char
*
path
,
bool
disable_fnmatch
,
bool
casefold
)
{
unsigned
int
i
;
git_attr_fnmatch
*
match
;
int
fnmatch_flags
=
0
;
int
(
*
use_strcmp
)(
const
char
*
,
const
char
*
);
int
(
*
use_strncmp
)(
const
char
*
,
const
char
*
,
size_t
);
if
(!
vspec
||
!
vspec
->
length
)
return
true;
if
(
disable_fnmatch
)
fnmatch_flags
=
-1
;
else
if
(
casefold
)
fnmatch_flags
=
FNM_CASEFOLD
;
if
(
casefold
) {
use_strcmp
=
git__strcasecmp
;
use_strncmp
=
git__strncasecmp
;
}
else
{
use_strcmp
=
git__strcmp
;
use_strncmp
=
git__strncmp
;
}
git_vector_foreach
(
vspec
,
i
,
match
) {
int
result
=
use_strcmp
(
match
->
pattern
,
path
) ?
FNM_NOMATCH
:
0
;
if
(
fnmatch_flags
>=
0
&&
result
==
FNM_NOMATCH
)
result
=
p_fnmatch
(
match
->
pattern
,
path
,
fnmatch_flags
);
/* if we didn't match, look for exact dirname prefix match */
if
(
result
==
FNM_NOMATCH
&&
(
match
->
flags
&
GIT_ATTR_FNMATCH_HASWILD
)
==
0
&&
use_strncmp
(
path
,
match
->
pattern
,
match
->
length
)
==
0
&&
path
[
match
->
length
]
==
'/'
)
result
=
0
;
if
(
result
==
0
)
return
(
match
->
flags
&
GIT_ATTR_FNMATCH_NEGATIVE
) ? false : true;
}
return
false;
}
You can’t perform that action at this time.