diff options
| author | Shawn O. Pearce <spearce@spearce.org> | 2008-12-02 09:56:23 -0800 |
|---|---|---|
| committer | Shawn O. Pearce <spearce@spearce.org> | 2008-12-02 10:02:29 -0800 |
| commit | af795e498d411142ddb073e8ca2c5447c3295a4c (patch) | |
| tree | 166bd0fc25eac4f184a3185eb5994988cfd6c201 /src/oid.c | |
| parent | b72ca26740c71371258bd178188a3d981e39701e (diff) | |
| download | libgit2-af795e498d411142ddb073e8ca2c5447c3295a4c.tar.gz | |
Add routines to convert git_oid to hex strings
[sp: Credit for some of this implementation goes to Pieter, I
started off a patch he proposed for libgit2 but reworked
enough of it that I don't want to blame him for any bugs.]
Suggested-by: Pieter de Bie <pdebie@ai.rug.nl>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/oid.c')
| -rw-r--r-- | src/oid.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -44,6 +44,7 @@ static signed char from_hex[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */ }; +static char to_hex[] = "0123456789abcdef"; int git_oid_mkstr(git_oid *out, const char *str) { @@ -57,3 +58,38 @@ int git_oid_mkstr(git_oid *out, const char *str) } return GIT_SUCCESS; } + +static inline char *fmt_one(char *str, unsigned int val) +{ + *str++ = to_hex[val >> 4]; + *str++ = to_hex[val & 0xf]; + return str; +} + +void git_oid_fmt(char *str, const git_oid *oid) +{ + int i; + + for (i = 0; i < sizeof(oid->id); i++) + str = fmt_one(str, oid->id[i]); +} + +void git_oid_pathfmt(char *str, const git_oid *oid) +{ + int i; + + str = fmt_one(str, oid->id[0]); + *str++ = '/'; + for (i = 1; i < sizeof(oid->id); i++) + str = fmt_one(str, oid->id[i]); +} + +char *git_oid_allocfmt(const git_oid *oid) +{ + char *str = malloc(GIT_OID_HEXSZ + 1); + if (!str) + return NULL; + git_oid_fmt(str, oid); + str[GIT_OID_HEXSZ] = '\0'; + return str; +} |
