examples: move refish resolution function in common
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
diff --git a/examples/common.c b/examples/common.c
index 1180720..2b4a062 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)
@@ -245,3 +247,25 @@ 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;
+ int err = 0;
+ git_oid oid;
+
+ 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_oid_fromstr(&oid, refish);
+ if (err == GIT_OK) {
+ err = git_annotated_commit_lookup(commit, repo, &oid);
+ }
+
+ return err;
+}
diff --git a/examples/common.h b/examples/common.h
index af42324..503c8fb 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -108,3 +108,8 @@ extern void treeish_to_tree(
* A realloc that exits on failure
*/
extern void *xrealloc(void *oldp, size_t newsz);
+
+/**
+ * Convert a refish to an annotated commit.
+ */
+extern int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish);
diff --git a/examples/merge.c b/examples/merge.c
index 59982cb..c819ad2 100644
--- a/examples/merge.c
+++ b/examples/merge.c
@@ -87,29 +87,6 @@ static void parse_options(const char **repo_path, merge_options *opts, int argc,
}
}
-static int resolve_refish(git_annotated_commit **commit, git_repository *repo, const char *refish)
-{
- git_reference *ref;
- int err = 0;
- git_oid oid;
-
- 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_oid_fromstr(&oid, refish);
- if (err == GIT_OK) {
- err = git_annotated_commit_lookup(commit, repo, &oid);
- }
-
- return err;
-}
-
static int resolve_heads(git_repository *repo, merge_options *opts)
{
git_annotated_commit **annotated = calloc(opts->heads_count, sizeof(git_annotated_commit *));