Merge pull request #5421 from petersalomonsen/examples-fixes-and-additions examples: additions and fixes
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
diff --git a/examples/add.c b/examples/add.c
index 6e3c239..542360e 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -48,9 +48,11 @@ int lg2_add(git_repository *repo, int argc, char **argv)
git_index_matched_path_cb matched_cb = NULL;
git_index *index;
git_strarray array = {0};
- struct index_options options;
+ struct index_options options = {0};
struct args_info args = ARGS_INFO_INIT;
+ options.mode = INDEX_ADD;
+
/* Parse the options & arguments. */
parse_opts(NULL, &options, &args);
strarray_from_args(&array, &args);
diff --git a/examples/commit.c b/examples/commit.c
new file mode 100644
index 0000000..cd9782d
--- /dev/null
+++ b/examples/commit.c
@@ -0,0 +1,84 @@
+/*
+ * libgit2 "commit" example - shows how to create a git commit
+ *
+ * Written by the libgit2 contributors
+ *
+ * To the extent possible under law, the author(s) have dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide. This software is distributed without any warranty.
+ *
+ * You should have received a copy of the CC0 Public Domain Dedication along
+ * with this software. If not, see
+ * <http://creativecommons.org/publicdomain/zero/1.0/>.
+ */
+
+#include "common.h"
+
+/**
+ * This example demonstrates the libgit2 commit APIs to roughly
+ * simulate `git commit` with the commit message argument.
+ *
+ * This does not have:
+ *
+ * - Robust error handling
+ * - Most of the `git commit` options
+ *
+ * This does have:
+ *
+ * - Example of performing a git commit with a comment
+ *
+ */
+int lg2_commit(git_repository *repo, int argc, char **argv)
+{
+ const char *opt = argv[1];
+ const char *comment = argv[2];
+ int error;
+
+ git_oid commit_oid,tree_oid;
+ git_tree *tree;
+ git_index *index;
+ git_object *parent = NULL;
+ git_reference *ref = NULL;
+ git_signature *signature;
+
+ /* Validate args */
+ if (argc < 3 || strcmp(opt, "-m") != 0) {
+ printf ("USAGE: %s -m <comment>\n", argv[0]);
+ return -1;
+ }
+
+ error = git_revparse_ext(&parent, &ref, repo, "HEAD");
+ if (error == GIT_ENOTFOUND) {
+ printf("HEAD not found. Creating first commit\n");
+ error = 0;
+ } else if (error != 0) {
+ const git_error *err = git_error_last();
+ if (err) printf("ERROR %d: %s\n", err->klass, err->message);
+ else printf("ERROR %d: no detailed info\n", error);
+ }
+
+ check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
+ check_lg2(git_index_write_tree(&tree_oid, index), "Could not write tree", NULL);;
+ check_lg2(git_index_write(index), "Could not write index", NULL);;
+
+ check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", NULL);
+
+ check_lg2(git_signature_default(&signature, repo), "Error creating signature", NULL);
+
+ check_lg2(git_commit_create_v(
+ &commit_oid,
+ repo,
+ "HEAD",
+ signature,
+ signature,
+ NULL,
+ comment,
+ tree,
+ parent ? 1 : 0, parent), "Error creating commit", NULL);
+
+ git_index_free(index);
+ git_signature_free(signature);
+ git_tree_free(tree);
+
+ return error;
+}
diff --git a/examples/common.h b/examples/common.h
index c01561b..0126568 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -59,6 +59,7 @@ extern int lg2_blame(git_repository *repo, int argc, char **argv);
extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
extern int lg2_checkout(git_repository *repo, int argc, char **argv);
extern int lg2_clone(git_repository *repo, int argc, char **argv);
+extern int lg2_commit(git_repository *repo, int argc, char **argv);
extern int lg2_config(git_repository *repo, int argc, char **argv);
extern int lg2_describe(git_repository *repo, int argc, char **argv);
extern int lg2_diff(git_repository *repo, int argc, char **argv);
@@ -71,6 +72,7 @@ extern int lg2_log(git_repository *repo, int argc, char **argv);
extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
extern int lg2_merge(git_repository *repo, int argc, char **argv);
+extern int lg2_push(git_repository *repo, int argc, char **argv);
extern int lg2_remote(git_repository *repo, int argc, char **argv);
extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);
diff --git a/examples/lg2.c b/examples/lg2.c
index a3987c3..7946bc2 100644
--- a/examples/lg2.c
+++ b/examples/lg2.c
@@ -15,6 +15,7 @@ struct {
{ "cat-file", lg2_cat_file, 1 },
{ "checkout", lg2_checkout, 1 },
{ "clone", lg2_clone, 0 },
+ { "commit", lg2_commit, 1 },
{ "config", lg2_config, 1 },
{ "describe", lg2_describe, 1 },
{ "diff", lg2_diff, 1 },
@@ -27,6 +28,7 @@ struct {
{ "ls-files", lg2_ls_files, 1 },
{ "ls-remote", lg2_ls_remote, 1 },
{ "merge", lg2_merge, 1 },
+ { "push", lg2_push, 1 },
{ "remote", lg2_remote, 1 },
{ "rev-list", lg2_rev_list, 1 },
{ "rev-parse", lg2_rev_parse, 1 },
diff --git a/examples/push.c b/examples/push.c
new file mode 100644
index 0000000..bcf3076
--- /dev/null
+++ b/examples/push.c
@@ -0,0 +1,56 @@
+/*
+ * libgit2 "push" example - shows how to push to remote
+ *
+ * Written by the libgit2 contributors
+ *
+ * To the extent possible under law, the author(s) have dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide. This software is distributed without any warranty.
+ *
+ * You should have received a copy of the CC0 Public Domain Dedication along
+ * with this software. If not, see
+ * <http://creativecommons.org/publicdomain/zero/1.0/>.
+ */
+
+#include "common.h"
+
+/**
+ * This example demonstrates the libgit2 push API to roughly
+ * simulate `git push`.
+ *
+ * This does not have:
+ *
+ * - Robust error handling
+ * - Any of the `git push` options
+ *
+ * This does have:
+ *
+ * - Example of push to origin/master
+ *
+ */
+
+/** Entry point for this command */
+int lg2_push(git_repository *repo, int argc, char **argv) {
+ git_push_options options;
+ git_remote* remote = NULL;
+ char *refspec = "refs/heads/master";
+ const git_strarray refspecs = {
+ &refspec,
+ 1
+ };
+
+ /* Validate args */
+ if (argc > 1) {
+ printf ("USAGE: %s\n\nsorry, no arguments supported yet\n", argv[0]);
+ return -1;
+ }
+
+ check_lg2(git_remote_lookup(&remote, repo, "origin" ), "Unable to lookup remote", NULL);
+
+ check_lg2(git_push_options_init(&options, GIT_PUSH_OPTIONS_VERSION ), "Error initializing push", NULL);
+
+ check_lg2(git_remote_push(remote, &refspecs, &options), "Error pushing", NULL);
+
+ printf("pushed\n");
+ return 0;
+}