Commit d71d75ad16058715e286e90bf48ab4f3d76e2127

Stefan Sperling 2017-11-05T18:09:59

print the head revision

diff --git a/include/got_object.h b/include/got_object.h
index eeca74e..fdfff2e 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -17,3 +17,5 @@
 struct got_object_id {
 	u_int8_t sha1[SHA1_DIGEST_LENGTH];
 };
+
+const char * got_object_id_str(struct got_object_id *, char *, size_t);
diff --git a/lib/object.c b/lib/object.c
new file mode 100644
index 0000000..4e9d9cf
--- /dev/null
+++ b/lib/object.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <sha1.h>
+
+#include "got_object.h"
+
+const char *
+got_object_id_str(struct got_object_id *id, char *buf, size_t size)
+{
+	char *p = buf;
+	char hex[3];
+	int i;
+
+	if (size < SHA1_DIGEST_STRING_LENGTH)
+		return NULL;
+
+	for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
+		snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
+		p[0] = hex[0];
+		p[1] = hex[1];
+		p += 2;
+	}
+	p[0] = '\0';
+
+	return buf;
+}
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index 141f5fd..4a4a51f 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -1,7 +1,7 @@
 .PATH:${.CURDIR}/../../lib
 
 PROG = repository_test
-SRCS = path.c repository.c error.c refs.c repository_test.c
+SRCS = path.c repository.c error.c refs.c object.c repository_test.c
 
 CPPFLAGS = -I${.CURDIR}/../../include
 LDADD = -lutil
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 7734cd5..eb48aec 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -35,6 +35,7 @@ repo_resolve_head_ref(const char *repo_path)
 	struct got_repository *repo;
 	struct got_reference *head_ref;
 	struct got_object_id *id;
+	char buf[SHA1_DIGEST_STRING_LENGTH];
 	int ret;
 
 	err = got_repo_open(&repo, repo_path);
@@ -46,6 +47,7 @@ repo_resolve_head_ref(const char *repo_path)
 	err = got_ref_resolve(&id, repo, head_ref);
 	if (err != NULL || head_ref == NULL)
 		return 0;
+	printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
 	free(id);
 	got_ref_close(head_ref);
 	got_repo_close(repo);