Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
863dabda
Commit
863dabda
authored
Jul 11, 2014
by
Vicent Marti
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2465 from libgit2/cmn/refspec-start-middle
Support refspecs with the asterisk in the middle
parents
356b891e
9fef46de
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
18 deletions
+68
-18
CHANGELOG.md
+3
-0
src/refspec.c
+42
-18
tests/network/refspecs.c
+23
-0
No files found.
CHANGELOG.md
View file @
863dabda
...
...
@@ -32,3 +32,6 @@ v0.21 + 1
*
git_clone_into and git_clone_local_into have been removed from the
public API in favour of git_clone callbacks
*
Add support for refspecs with the asterisk in the middle of a
pattern.
src/refspec.c
View file @
863dabda
...
...
@@ -181,39 +181,63 @@ int git_refspec_dst_matches(const git_refspec *refspec, const char *refname)
static
int
refspec_transform
(
git_buf
*
out
,
const
char
*
from
,
const
char
*
to
,
const
char
*
name
)
{
size_t
to_len
=
to
?
strlen
(
to
)
:
0
;
size_t
from_len
=
from
?
strlen
(
from
)
:
0
;
size_t
name_len
=
name
?
strlen
(
name
)
:
0
;
const
char
*
from_star
,
*
to_star
;
const
char
*
name_slash
,
*
from_slash
;
size_t
replacement_len
,
star_offset
;
git_buf_sanitize
(
out
);
git_buf_clear
(
out
);
if
(
git_buf_set
(
out
,
to
,
to_len
)
<
0
)
return
-
1
;
/*
* There are two parts to each side of a refspec, the bit
* before the star and the bit after it. The star can be in
* the middle of the pattern, so we need to look at each bit
* individually.
*/
from_star
=
strchr
(
from
,
'*'
);
to_star
=
strchr
(
to
,
'*'
);
if
(
to_len
>
0
)
{
/* No '*' at the end of 'to' means that refspec is mapped to one
* specific branch, so no actual transformation is needed.
*/
if
(
out
->
ptr
[
to_len
-
1
]
!=
'*'
)
return
0
;
git_buf_shorten
(
out
,
1
);
/* remove trailing '*' copied from 'to' */
}
assert
(
from_star
&&
to_star
);
/* star offset, both in 'from' and in 'name' */
star_offset
=
from_star
-
from
;
/* the first half is copied over */
git_buf_put
(
out
,
to
,
to_star
-
to
);
/* then we copy over the replacement, from the star's offset to the next slash in 'name' */
name_slash
=
strchr
(
name
+
star_offset
,
'/'
);
if
(
!
name_slash
)
name_slash
=
strrchr
(
name
,
'\0'
);
if
(
from_len
>
0
)
/* ignore trailing '*' from 'from'
*/
from_len
--
;
if
(
from_len
>
name_len
)
from_len
=
name_len
;
/* if there is no slash after the star in 'from', we want to copy everything over
*/
from_slash
=
strchr
(
from
+
star_offset
,
'/'
)
;
if
(
!
from_slash
)
name_slash
=
strrchr
(
name
,
'\0'
)
;
return
git_buf_put
(
out
,
name
+
from_len
,
name_len
-
from_len
);
replacement_len
=
(
name_slash
-
name
)
-
star_offset
;
git_buf_put
(
out
,
name
+
star_offset
,
replacement_len
);
return
git_buf_puts
(
out
,
to_star
+
1
);
}
int
git_refspec_transform
(
git_buf
*
out
,
const
git_refspec
*
spec
,
const
char
*
name
)
{
git_buf_sanitize
(
out
);
if
(
!
spec
->
pattern
)
return
git_buf_puts
(
out
,
spec
->
dst
);
return
refspec_transform
(
out
,
spec
->
src
,
spec
->
dst
,
name
);
}
int
git_refspec_rtransform
(
git_buf
*
out
,
const
git_refspec
*
spec
,
const
char
*
name
)
{
git_buf_sanitize
(
out
);
if
(
!
spec
->
pattern
)
return
git_buf_puts
(
out
,
spec
->
src
);
return
refspec_transform
(
out
,
spec
->
dst
,
spec
->
src
,
name
);
}
...
...
tests/network/refspecs.c
View file @
863dabda
...
...
@@ -84,4 +84,27 @@ void test_network_refspecs__parsing(void)
assert_refspec
(
GIT_DIRECTION_FETCH
,
"master"
,
true
);
assert_refspec
(
GIT_DIRECTION_PUSH
,
"master"
,
true
);
assert_refspec
(
GIT_DIRECTION_FETCH
,
"refs/pull/*/head:refs/remotes/origin/pr/*"
,
true
);
}
void
assert_transform
(
const
char
*
refspec
,
const
char
*
name
,
const
char
*
result
)
{
git_refspec
spec
;
git_buf
buf
=
GIT_BUF_INIT
;
git_refspec__parse
(
&
spec
,
refspec
,
true
);
cl_git_pass
(
git_refspec_transform
(
&
buf
,
&
spec
,
name
));
cl_assert_equal_s
(
result
,
buf
.
ptr
);
git_buf_free
(
&
buf
);
git_refspec__free
(
&
spec
);
}
void
test_network_refspecs__transform_mid_star
(
void
)
{
assert_transform
(
"refs/pull/*/head:refs/remotes/origin/pr/*"
,
"refs/pull/23/head"
,
"refs/remotes/origin/pr/23"
);
assert_transform
(
"refs/heads/*:refs/remotes/origin/*"
,
"refs/heads/master"
,
"refs/remotes/origin/master"
);
assert_transform
(
"refs/heads/*:refs/heads/*"
,
"refs/heads/master"
,
"refs/heads/master"
);
assert_transform
(
"refs/*:refs/*"
,
"refs/heads/master"
,
"refs/heads/master"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment