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
2e0c8816
Commit
2e0c8816
authored
Aug 26, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refs: expose git_reference_normalize_name()
parent
11684104
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
38 deletions
+118
-38
include/git2/refs.h
+48
-0
src/refs.c
+70
-38
tests-clar/refs/normalize.c
+0
-0
No files found.
include/git2/refs.h
View file @
2e0c8816
...
...
@@ -376,6 +376,54 @@ GIT_EXTERN(int) git_reference_has_log(git_reference *ref);
*/
GIT_EXTERN
(
int
)
git_reference_is_branch
(
git_reference
*
ref
);
enum
{
GIT_REF_FORMAT_NORMAL
=
0
,
/**
* Control whether one-level refnames are accepted
* (i.e., refnames that do not contain multiple /-separated
* components)
*/
GIT_REF_FORMAT_ALLOW_ONELEVEL
=
(
1
<<
0
),
/**
* Interpret the provided name as a reference pattern for a
* refspec (as used with remote repositories). If this option
* is enabled, the name is allowed to contain a single * (<star>)
* in place of a one full pathname component
* (e.g., foo/<star>/bar but not foo/bar<star>).
*/
GIT_REF_FORMAT_REFSPEC_PATTERN
=
(
1
<<
1
),
};
/**
* Normalize the reference name by removing any leading
* slash (/) characters and collapsing runs of adjacent slashes
* between name components into a single slash.
*
* Once normalized, if the reference name is valid, it will be
* returned in the user allocated buffer.
*
* TODO: Implement handling of GIT_REF_FORMAT_REFSPEC_PATTERN
*
* @param buffer_out The user allocated buffer where the
* normalized name will be stored.
*
* @param buffer_size buffer_out size
*
* @param name name to be checked.
*
* @param flags Flags to determine the options to be applied while
* checking the validatity of the name.
*
* @return 0 or an error code.
*/
GIT_EXTERN
(
int
)
git_reference_normalize_name
(
char
*
buffer_out
,
size_t
buffer_size
,
const
char
*
name
,
unsigned
int
flags
);
/** @} */
GIT_END_DECL
#endif
src/refs.c
View file @
2e0c8816
...
...
@@ -68,11 +68,6 @@ static int reference_path_available(git_repository *repo,
static
int
reference_delete
(
git_reference
*
ref
);
static
int
reference_lookup
(
git_reference
*
ref
);
/* name normalization */
static
int
normalize_name
(
char
*
buffer_out
,
size_t
out_size
,
const
char
*
name
,
int
is_oid_ref
);
void
git_reference_free
(
git_reference
*
reference
)
{
if
(
reference
==
NULL
)
...
...
@@ -1099,9 +1094,12 @@ int git_reference_lookup_resolved(
scan
->
name
=
git__calloc
(
GIT_REFNAME_MAX
+
1
,
sizeof
(
char
));
GITERR_CHECK_ALLOC
(
scan
->
name
);
if
((
result
=
normalize_name
(
scan
->
name
,
GIT_REFNAME_MAX
,
name
,
0
))
<
0
)
{
git_reference_free
(
scan
);
return
result
;
if
((
result
=
git_reference__normalize_name
(
scan
->
name
,
GIT_REFNAME_MAX
,
name
))
<
0
)
{
git_reference_free
(
scan
);
return
result
;
}
scan
->
target
.
symbolic
=
git__strdup
(
scan
->
name
);
...
...
@@ -1198,8 +1196,11 @@ int git_reference_create_symbolic(
char
normalized
[
GIT_REFNAME_MAX
];
git_reference
*
ref
=
NULL
;
if
(
normalize_name
(
normalized
,
sizeof
(
normalized
),
name
,
0
)
<
0
)
return
-
1
;
if
(
git_reference__normalize_name
(
normalized
,
sizeof
(
normalized
),
name
)
<
0
)
return
-
1
;
if
(
reference_can_write
(
repo
,
normalized
,
NULL
,
force
)
<
0
)
return
-
1
;
...
...
@@ -1234,8 +1235,11 @@ int git_reference_create_oid(
git_reference
*
ref
=
NULL
;
char
normalized
[
GIT_REFNAME_MAX
];
if
(
normalize_name
(
normalized
,
sizeof
(
normalized
),
name
,
1
)
<
0
)
return
-
1
;
if
(
git_reference__normalize_name_oid
(
normalized
,
sizeof
(
normalized
),
name
)
<
0
)
return
-
1
;
if
(
reference_can_write
(
repo
,
normalized
,
NULL
,
force
)
<
0
)
return
-
1
;
...
...
@@ -1314,8 +1318,11 @@ int git_reference_set_target(git_reference *ref, const char *target)
return
-
1
;
}
if
(
normalize_name
(
normalized
,
sizeof
(
normalized
),
target
,
0
))
return
-
1
;
if
(
git_reference__normalize_name
(
normalized
,
sizeof
(
normalized
),
target
))
return
-
1
;
git__free
(
ref
->
target
.
symbolic
);
ref
->
target
.
symbolic
=
git__strdup
(
normalized
);
...
...
@@ -1327,15 +1334,23 @@ int git_reference_set_target(git_reference *ref, const char *target)
int
git_reference_rename
(
git_reference
*
ref
,
const
char
*
new_name
,
int
force
)
{
int
result
;
unsigned
int
normalization_flags
;
git_buf
aux_path
=
GIT_BUF_INIT
;
char
normalized
[
GIT_REFNAME_MAX
];
const
char
*
head_target
=
NULL
;
git_reference
*
head
=
NULL
;
if
(
normalize_name
(
normalized
,
sizeof
(
normalized
),
new_name
,
ref
->
flags
&
GIT_REF_OID
)
<
0
)
return
-
1
;
normalization_flags
=
ref
->
flags
&
GIT_REF_SYMBOLIC
?
GIT_REF_FORMAT_ALLOW_ONELEVEL
:
GIT_REF_FORMAT_NORMAL
;
if
(
git_reference_normalize_name
(
normalized
,
sizeof
(
normalized
),
new_name
,
normalization_flags
)
<
0
)
return
-
1
;
if
(
reference_can_write
(
ref
->
owner
,
normalized
,
ref
->
name
,
force
)
<
0
)
return
-
1
;
...
...
@@ -1565,11 +1580,11 @@ static int is_valid_ref_char(char ch)
}
}
static
int
normalize_name
(
int
git_reference_
normalize_name
(
char
*
buffer_out
,
size_t
out
_size
,
size_t
buffer
_size
,
const
char
*
name
,
int
is_oid_ref
)
unsigned
int
flags
)
{
const
char
*
name_end
,
*
buffer_out_start
;
const
char
*
current
;
...
...
@@ -1577,12 +1592,17 @@ static int normalize_name(
assert
(
name
&&
buffer_out
);
if
(
flags
&
GIT_REF_FORMAT_REFSPEC_PATTERN
)
{
giterr_set
(
GITERR_INVALID
,
"Unimplemented"
);
return
-
1
;
}
buffer_out_start
=
buffer_out
;
current
=
name
;
name_end
=
name
+
strlen
(
name
);
/* Terminating null byte */
out
_size
--
;
buffer
_size
--
;
/* A refname can not be empty */
if
(
name_end
==
name
)
...
...
@@ -1592,7 +1612,7 @@ static int normalize_name(
if
(
*
(
name_end
-
1
)
==
'.'
||
*
(
name_end
-
1
)
==
'/'
)
goto
invalid_name
;
while
(
current
<
name_end
&&
out_size
)
{
while
(
current
<
name_end
&&
buffer_size
>
0
)
{
if
(
!
is_valid_ref_char
(
*
current
))
goto
invalid_name
;
...
...
@@ -1615,19 +1635,29 @@ static int normalize_name(
}
if
(
*
current
==
'/'
)
contains_a_slash
=
1
;
if
(
buffer_out
>
buffer_out_start
)
contains_a_slash
=
1
;
else
{
current
++
;
continue
;
}
*
buffer_out
++
=
*
current
++
;
out
_size
--
;
buffer
_size
--
;
}
if
(
!
out_size
)
goto
invalid_name
;
if
(
current
<
name_end
)
{
giterr_set
(
GITERR_REFERENCE
,
"The provided buffer is too short to hold the normalization of '%s'"
,
name
);
return
GIT_EBUFS
;
}
/* Object id refname have to contain at least one slash, except
* for HEAD in a detached state or MERGE_HEAD if we're in the
* middle of a merge */
if
(
is_oid_ref
&&
if
(
!
(
flags
&
GIT_REF_FORMAT_ALLOW_ONELEVEL
)
&&
!
contains_a_slash
&&
strcmp
(
name
,
GIT_HEAD_FILE
)
!=
0
&&
strcmp
(
name
,
GIT_MERGE_HEAD_FILE
)
!=
0
&&
...
...
@@ -1640,18 +1670,12 @@ static int normalize_name(
*
buffer_out
=
'\0'
;
/*
* For object id references, name has to start with refs/. Again,
* we need to allow HEAD to be in a detached state.
*/
if
(
is_oid_ref
&&
!
(
git__prefixcmp
(
buffer_out_start
,
GIT_REFS_DIR
)
||
strcmp
(
buffer_out_start
,
GIT_HEAD_FILE
)))
goto
invalid_name
;
return
0
;
invalid_name:
giterr_set
(
GITERR_REFERENCE
,
"The given reference name is not valid"
);
giterr_set
(
GITERR_REFERENCE
,
"The given reference name '%s' is not valid"
,
name
);
return
-
1
;
}
...
...
@@ -1660,7 +1684,11 @@ int git_reference__normalize_name(
size_t
out_size
,
const
char
*
name
)
{
return
normalize_name
(
buffer_out
,
out_size
,
name
,
0
);
return
git_reference_normalize_name
(
buffer_out
,
out_size
,
name
,
GIT_REF_FORMAT_ALLOW_ONELEVEL
);
}
int
git_reference__normalize_name_oid
(
...
...
@@ -1668,7 +1696,11 @@ int git_reference__normalize_name_oid(
size_t
out_size
,
const
char
*
name
)
{
return
normalize_name
(
buffer_out
,
out_size
,
name
,
1
);
return
git_reference_normalize_name
(
buffer_out
,
out_size
,
name
,
GIT_REF_FORMAT_NORMAL
);
}
#define GIT_REF_TYPEMASK (GIT_REF_OID | GIT_REF_SYMBOLIC)
...
...
tests-clar/refs/normalize.c
View file @
2e0c8816
This diff is collapsed.
Click to expand it.
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