summaryrefslogtreecommitdiff
path: root/examples/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/common.c')
-rw-r--r--examples/common.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/common.c b/examples/common.c
index 118072044..8c1042471 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -12,6 +12,8 @@
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
+#include <assert.h>
+
#include "common.h"
void check_lg2(int error, const char *message, const char *extra)
@@ -182,6 +184,25 @@ static int match_int_internal(
return 1;
}
+int match_bool_arg(int *out, struct args_info *args, const char *opt)
+{
+ const char *found = args->argv[args->pos];
+
+ if (!strcmp(found, opt)) {
+ *out = 1;
+ return 1;
+ }
+
+ if (!strncmp(found, "--no-", strlen("--no-")) &&
+ !strcmp(found + strlen("--no-"), opt + 2)) {
+ *out = 0;
+ return 1;
+ }
+
+ *out = -1;
+ return 0;
+}
+
int is_integer(int *out, const char *str, int allow_negative)
{
return match_int_internal(out, str, allow_negative, NULL);
@@ -245,3 +266,26 @@ void *xrealloc(void *oldp, size_t newsz)
return p;
}
+int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish)
+{
+ git_reference *ref;
+ git_object *obj;
+ int err = 0;
+
+ assert(commit != NULL);
+
+ err = git_reference_dwim(&ref, repo, refish);
+ if (err == GIT_OK) {
+ git_annotated_commit_from_ref(commit, repo, ref);
+ git_reference_free(ref);
+ return 0;
+ }
+
+ err = git_revparse_single(&obj, repo, refish);
+ if (err == GIT_OK) {
+ err = git_annotated_commit_lookup(commit, repo, git_object_id(obj));
+ git_object_free(obj);
+ }
+
+ return err;
+}