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
960ca1d7
Commit
960ca1d7
authored
Aug 28, 2009
by
Ramsay Jones
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the git_oid_to_string() utility function
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
parent
e4553584
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
1 deletions
+82
-1
src/git/oid.h
+18
-1
src/oid.c
+20
-0
tests/t0101-oid.c
+44
-0
No files found.
src/git/oid.h
View file @
960ca1d7
...
@@ -74,13 +74,30 @@ GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid);
...
@@ -74,13 +74,30 @@ GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid);
/**
/**
* Format a gid_oid into a newly allocated c-string.
* Format a gid_oid into a newly allocated c-string.
* @param oid theoid structure to format
* @param oid the
oid structure to format
* @return the c-string; NULL if memory is exhausted. Caller must
* @return the c-string; NULL if memory is exhausted. Caller must
* deallocate the string with free().
* deallocate the string with free().
*/
*/
GIT_EXTERN
(
char
*
)
git_oid_allocfmt
(
const
git_oid
*
oid
);
GIT_EXTERN
(
char
*
)
git_oid_allocfmt
(
const
git_oid
*
oid
);
/**
/**
* Format a git_oid into a buffer as a hex format c-string.
* <p>
* If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting
* oid c-string will be truncated to n-1 characters. If there are
* any input parameter errors (out == NULL, n == 0, oid == NULL),
* then a pointer to an empty string is returned, so that the return
* value can always be printed.
*
* @param out the buffer into which the oid string is output.
* @param n the size of the out buffer.
* @param oid the oid structure to format.
* @return the out buffer pointer, assuming no input parameter
* errors, otherwise a pointer to an empty string.
*/
GIT_EXTERN
(
char
*
)
git_oid_to_string
(
char
*
out
,
size_t
n
,
const
git_oid
*
oid
);
/**
* Copy an oid from one structure to another.
* Copy an oid from one structure to another.
* @param out oid structure the result is written into.
* @param out oid structure the result is written into.
* @param src oid structure to copy from.
* @param src oid structure to copy from.
...
...
src/oid.c
View file @
960ca1d7
...
@@ -94,3 +94,23 @@ char *git_oid_allocfmt(const git_oid *oid)
...
@@ -94,3 +94,23 @@ char *git_oid_allocfmt(const git_oid *oid)
str
[
GIT_OID_HEXSZ
]
=
'\0'
;
str
[
GIT_OID_HEXSZ
]
=
'\0'
;
return
str
;
return
str
;
}
}
char
*
git_oid_to_string
(
char
*
out
,
size_t
n
,
const
git_oid
*
oid
)
{
char
str
[
GIT_OID_HEXSZ
];
if
(
!
out
||
n
==
0
||
!
oid
)
return
""
;
n
--
;
/* allow room for terminating NUL */
if
(
n
>
0
)
{
git_oid_fmt
(
str
,
oid
);
memcpy
(
out
,
str
,
n
>
GIT_OID_HEXSZ
?
GIT_OID_HEXSZ
:
n
);
}
out
[
n
]
=
'\0'
;
return
out
;
}
tests/t0101-oid.c
View file @
960ca1d7
...
@@ -208,3 +208,47 @@ BEGIN_TEST(cmp_oid_pathfmt)
...
@@ -208,3 +208,47 @@ BEGIN_TEST(cmp_oid_pathfmt)
must_pass
(
strcmp
(
exp2
,
out
));
must_pass
(
strcmp
(
exp2
,
out
));
END_TEST
END_TEST
BEGIN_TEST
(
oid_to_string
)
const
char
*
exp
=
"16a0123456789abcdef4b775213c23a8bd74f5e0"
;
git_oid
in
;
char
out
[
GIT_OID_HEXSZ
+
1
];
char
*
str
;
int
i
;
must_pass
(
git_oid_mkstr
(
&
in
,
exp
));
/* NULL buffer pointer, returns static empty string */
str
=
git_oid_to_string
(
NULL
,
sizeof
(
out
),
&
in
);
must_be_true
(
str
&&
*
str
==
'\0'
&&
str
!=
out
);
/* zero buffer size, returns static empty string */
str
=
git_oid_to_string
(
out
,
0
,
&
in
);
must_be_true
(
str
&&
*
str
==
'\0'
&&
str
!=
out
);
/* NULL oid pointer, returns static empty string */
str
=
git_oid_to_string
(
out
,
sizeof
(
out
),
NULL
);
must_be_true
(
str
&&
*
str
==
'\0'
&&
str
!=
out
);
/* n == 1, returns out as an empty string */
str
=
git_oid_to_string
(
out
,
1
,
&
in
);
must_be_true
(
str
&&
*
str
==
'\0'
&&
str
==
out
);
for
(
i
=
1
;
i
<
GIT_OID_HEXSZ
;
i
++
)
{
out
[
i
+
1
]
=
'Z'
;
str
=
git_oid_to_string
(
out
,
i
+
1
,
&
in
);
/* returns out containing c-string */
must_be_true
(
str
&&
str
==
out
);
/* must be '\0' terminated */
must_be_true
(
*
(
str
+
i
)
==
'\0'
);
/* must not touch bytes past end of string */
must_be_true
(
*
(
str
+
(
i
+
1
))
==
'Z'
);
/* i == n-1 charaters of string */
must_pass
(
strncmp
(
exp
,
out
,
i
));
}
/* returns out as hex formatted c-string */
str
=
git_oid_to_string
(
out
,
sizeof
(
out
),
&
in
);
must_be_true
(
str
&&
str
==
out
&&
*
(
str
+
GIT_OID_HEXSZ
)
==
'\0'
);
must_pass
(
strcmp
(
exp
,
out
));
END_TEST
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