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
0adfa20a
Commit
0adfa20a
authored
Sep 11, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refspec: introduce git_refspec__parse()
parent
77e06d7e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
211 additions
and
3 deletions
+211
-3
src/refs.c
+10
-3
src/refs.h
+1
-0
src/refspec.c
+113
-0
src/refspec.h
+4
-0
tests-clar/network/refspecs.c
+83
-0
No files found.
src/refs.c
View file @
0adfa20a
...
...
@@ -1952,11 +1952,18 @@ cleanup:
return
error
;
}
int
git_reference__is_valid_name
(
const
char
*
refname
,
unsigned
int
flags
)
{
giterr_clear
();
return
git_reference__normalize_name
(
NULL
,
refname
,
flags
)
==
0
;
}
int
git_reference_is_valid_name
(
const
char
*
refname
)
{
return
git_reference__normalize_name
(
NULL
,
return
git_reference__is_valid_name
(
refname
,
GIT_REF_FORMAT_ALLOW_ONELEVEL
)
==
0
;
GIT_REF_FORMAT_ALLOW_ONELEVEL
);
}
src/refs.h
View file @
0adfa20a
...
...
@@ -55,6 +55,7 @@ void git_repository__refcache_free(git_refcache *refs);
int
git_reference__normalize_name_lax
(
char
*
buffer_out
,
size_t
out_size
,
const
char
*
name
);
int
git_reference__normalize_name
(
git_buf
*
buf
,
const
char
*
name
,
unsigned
int
flags
);
int
git_reference__is_valid_name
(
const
char
*
refname
,
unsigned
int
flags
);
int
git_reference__update
(
git_repository
*
repo
,
const
git_oid
*
oid
,
const
char
*
ref_name
);
/**
...
...
src/refspec.c
View file @
0adfa20a
...
...
@@ -11,6 +11,119 @@
#include "refspec.h"
#include "util.h"
#include "posix.h"
#include "refs.h"
int
git_refspec__parse
(
git_refspec
*
refspec
,
const
char
*
input
,
bool
is_fetch
)
{
// Ported from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/remote.c#L518-636
size_t
llen
;
int
is_glob
=
0
;
const
char
*
lhs
,
*
rhs
;
int
flags
;
assert
(
refspec
&&
input
);
memset
(
refspec
,
0x0
,
sizeof
(
git_refspec
));
lhs
=
input
;
if
(
*
lhs
==
'+'
)
{
refspec
->
force
=
1
;
lhs
++
;
}
rhs
=
strrchr
(
lhs
,
':'
);
/*
* Before going on, special case ":" (or "+:") as a refspec
* for matching refs.
*/
if
(
!
is_fetch
&&
rhs
==
lhs
&&
rhs
[
1
]
==
'\0'
)
{
refspec
->
matching
=
1
;
return
0
;
}
if
(
rhs
)
{
size_t
rlen
=
strlen
(
++
rhs
);
is_glob
=
(
1
<=
rlen
&&
strchr
(
rhs
,
'*'
));
refspec
->
dst
=
git__strndup
(
rhs
,
rlen
);
}
llen
=
(
rhs
?
(
size_t
)(
rhs
-
lhs
-
1
)
:
strlen
(
lhs
));
if
(
1
<=
llen
&&
memchr
(
lhs
,
'*'
,
llen
))
{
if
((
rhs
&&
!
is_glob
)
||
(
!
rhs
&&
is_fetch
))
goto
invalid
;
is_glob
=
1
;
}
else
if
(
rhs
&&
is_glob
)
goto
invalid
;
refspec
->
pattern
=
is_glob
;
refspec
->
src
=
git__strndup
(
lhs
,
llen
);
flags
=
GIT_REF_FORMAT_ALLOW_ONELEVEL
|
(
is_glob
?
GIT_REF_FORMAT_REFSPEC_PATTERN
:
0
);
if
(
is_fetch
)
{
/*
* LHS
* - empty is allowed; it means HEAD.
* - otherwise it must be a valid looking ref.
*/
if
(
!*
refspec
->
src
)
;
/* empty is ok */
else
if
(
!
git_reference__is_valid_name
(
refspec
->
src
,
flags
))
goto
invalid
;
/*
* RHS
* - missing is ok, and is same as empty.
* - empty is ok; it means not to store.
* - otherwise it must be a valid looking ref.
*/
if
(
!
refspec
->
dst
)
;
/* ok */
else
if
(
!*
refspec
->
dst
)
;
/* ok */
else
if
(
!
git_reference__is_valid_name
(
refspec
->
dst
,
flags
))
goto
invalid
;
}
else
{
/*
* LHS
* - empty is allowed; it means delete.
* - when wildcarded, it must be a valid looking ref.
* - otherwise, it must be an extended SHA-1, but
* there is no existing way to validate this.
*/
if
(
!*
refspec
->
src
)
;
/* empty is ok */
else
if
(
is_glob
)
{
if
(
!
git_reference__is_valid_name
(
refspec
->
src
,
flags
))
goto
invalid
;
}
else
{
;
/* anything goes, for now */
}
/*
* RHS
* - missing is allowed, but LHS then must be a
* valid looking ref.
* - empty is not allowed.
* - otherwise it must be a valid looking ref.
*/
if
(
!
refspec
->
dst
)
{
if
(
!
git_reference__is_valid_name
(
refspec
->
src
,
flags
))
goto
invalid
;
}
else
if
(
!*
refspec
->
dst
)
{
goto
invalid
;
}
else
{
if
(
!
git_reference__is_valid_name
(
refspec
->
dst
,
flags
))
goto
invalid
;
}
}
return
0
;
invalid:
return
-
1
;
}
int
git_refspec_parse
(
git_refspec
*
refspec
,
const
char
*
str
)
{
...
...
src/refspec.h
View file @
0adfa20a
...
...
@@ -20,6 +20,10 @@ struct git_refspec {
};
int
git_refspec_parse
(
struct
git_refspec
*
refspec
,
const
char
*
str
);
int
git_refspec__parse
(
struct
git_refspec
*
refspec
,
const
char
*
str
,
bool
is_fetch
);
/**
* Transform a reference to its target following the refspec's rules,
...
...
tests-clar/network/refspecs.c
0 → 100644
View file @
0adfa20a
#include "clar_libgit2.h"
#include "refspec.h"
#include "remote.h"
static
void
assert_refspec
(
unsigned
int
direction
,
const
char
*
input
,
bool
is_expected_to_be_valid
)
{
git_refspec
refspec
;
int
error
;
error
=
git_refspec__parse
(
&
refspec
,
input
,
direction
==
GIT_DIR_FETCH
);
if
(
is_expected_to_be_valid
)
cl_assert_equal_i
(
0
,
error
);
else
cl_assert_equal_i
(
GIT_ERROR
,
error
);
}
void
test_network_refspecs__parsing
(
void
)
{
// Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh
assert_refspec
(
GIT_DIR_PUSH
,
""
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
":"
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
"::"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"+:"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
""
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
":"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
"::"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/*:refs/remotes/frotz/*"
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/*:refs/remotes/frotz"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads:refs/remotes/frotz/*"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/master:refs/remotes/frotz/xyzzy"
,
true
);
/*
* These have invalid LHS, but we do not have a formal "valid sha-1
* expression syntax checker" so they are not checked with the current
* code. They will be caught downstream anyway, but we may want to
* have tighter check later...
*/
//assert_refspec(GIT_DIR_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false);
//assert_refspec(GIT_DIR_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/*:refs/remotes/frotz/*"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/*:refs/remotes/frotz"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads:refs/remotes/frotz/*"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/master:refs/remotes/frotz/xyzzy"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/master::refs/remotes/frotz/xyzzy"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/maste :refs/remotes/frotz/xyzzy"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"master~1:refs/remotes/frotz/backup"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
"master~1:refs/remotes/frotz/backup"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"HEAD~4:refs/remotes/frotz/new"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
"HEAD~4:refs/remotes/frotz/new"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"HEAD"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
"HEAD"
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/ nitfol"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/ nitfol"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"HEAD:"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"HEAD:"
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/ nitfol:"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/ nitfol:"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
":refs/remotes/frotz/deleteme"
,
true
);
assert_refspec
(
GIT_DIR_FETCH
,
":refs/remotes/frotz/HEAD-to-me"
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
":refs/remotes/frotz/delete me"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
":refs/remotes/frotz/HEAD to me"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/*/for-linus:refs/remotes/mine/*-blah"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/*/for-linus:refs/remotes/mine/*-blah"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads*/for-linus:refs/remotes/mine/*"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads*/for-linus:refs/remotes/mine/*"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/*/*/for-linus:refs/remotes/mine/*"
,
false
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/*/*/for-linus:refs/remotes/mine/*"
,
false
);
assert_refspec
(
GIT_DIR_FETCH
,
"refs/heads/*/for-linus:refs/remotes/mine/*"
,
true
);
assert_refspec
(
GIT_DIR_PUSH
,
"refs/heads/*/for-linus:refs/remotes/mine/*"
,
true
);
}
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