summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRamsay Jones <ramsay@ramsay1.demon.co.uk>2009-08-28 21:22:46 +0100
committerRamsay Jones <ramsay@ramsay1.demon.co.uk>2009-10-13 16:27:33 +0100
commit960ca1d7799e02b72ca828373c3fff04e2cf0334 (patch)
treef85c76fe4882c7a4befdea9224fa8c01574fdfce /src
parente45535849cb7187e22c9fd11d14e0eb37f5d7dc8 (diff)
downloadlibgit2-960ca1d7799e02b72ca828373c3fff04e2cf0334.tar.gz
Add the git_oid_to_string() utility function
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Diffstat (limited to 'src')
-rw-r--r--src/git/oid.h19
-rw-r--r--src/oid.c20
2 files changed, 38 insertions, 1 deletions
diff --git a/src/git/oid.h b/src/git/oid.h
index ce0220699..f3d6802e9 100644
--- a/src/git/oid.h
+++ b/src/git/oid.h
@@ -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.
- * @param oid theoid structure to format
+ * @param oid the oid structure to format
* @return the c-string; NULL if memory is exhausted. Caller must
* deallocate the string with free().
*/
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.
* @param out oid structure the result is written into.
* @param src oid structure to copy from.
diff --git a/src/oid.c b/src/oid.c
index b8fce12b6..97603e2d1 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -94,3 +94,23 @@ char *git_oid_allocfmt(const git_oid *oid)
str[GIT_OID_HEXSZ] = '\0';
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;
+}
+