Skip to content

Commit dc2beb7

Browse files
examples: additions and fixes
add example for git commit fix example for git add add example for git push
1 parent 705f4e8 commit dc2beb7

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

examples/add.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ int lg2_add(git_repository *repo, int argc, char **argv)
4848
git_index_matched_path_cb matched_cb = NULL;
4949
git_index *index;
5050
git_strarray array = {0};
51-
struct index_options options;
51+
struct index_options options = {0};
5252
struct args_info args = ARGS_INFO_INIT;
5353

54+
options.mode = INDEX_ADD;
55+
5456
/* Parse the options & arguments. */
5557
parse_opts(NULL, &options, &args);
5658
strarray_from_args(&array, &args);

examples/commit.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* libgit2 "commit" example - shows how to create a git commit
3+
*
4+
* Written by the libgit2 contributors
5+
*
6+
* To the extent possible under law, the author(s) have dedicated all copyright
7+
* and related and neighboring rights to this software to the public domain
8+
* worldwide. This software is distributed without any warranty.
9+
*
10+
* You should have received a copy of the CC0 Public Domain Dedication along
11+
* with this software. If not, see
12+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
*/
14+
15+
#include "common.h"
16+
17+
/**
18+
* This example demonstrates the libgit2 commit APIs to roughly
19+
* simulate `git commit` with the commit message argument.
20+
*
21+
* This does not have:
22+
*
23+
* - Robust error handling
24+
* - Most of the `git commit` options
25+
*
26+
* This does have:
27+
*
28+
* - Example of performing a git commit with a comment
29+
*
30+
*/
31+
int lg2_commit(git_repository *repo, int argc, char **argv)
32+
{
33+
const char *opt = argv[1];
34+
const char *comment = argv[2];
35+
int error;
36+
37+
git_oid commit_oid,tree_oid;
38+
git_tree *tree;
39+
git_index *index;
40+
git_object *parent = NULL;
41+
git_reference *ref = NULL;
42+
git_signature *signature;
43+
44+
/* Validate args */
45+
if (argc < 3 || strcmp(opt, "-m") != 0) {
46+
printf ("USAGE: %s -m <comment>\n", argv[0]);
47+
return -1;
48+
}
49+
50+
error = git_revparse_ext(&parent, &ref, repo, "HEAD");
51+
if (error == GIT_ENOTFOUND) {
52+
printf("HEAD not found. Creating first commit\n");
53+
error = 0;
54+
} else if (error != 0) {
55+
const git_error *err = git_error_last();
56+
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
57+
else printf("ERROR %d: no detailed info\n", error);
58+
}
59+
60+
check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
61+
check_lg2(git_index_write_tree(&tree_oid, index), "Could not write tree", NULL);;
62+
check_lg2(git_index_write(index), "Could not write index", NULL);;
63+
64+
check_lg2(git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", NULL);
65+
66+
check_lg2(git_signature_default(&signature, repo), "Error creating signature", NULL);
67+
68+
check_lg2(git_commit_create_v(
69+
&commit_oid,
70+
repo,
71+
"HEAD",
72+
signature,
73+
signature,
74+
NULL,
75+
comment,
76+
tree,
77+
parent ? 1 : 0, parent), "Error creating commit", NULL);
78+
79+
git_index_free(index);
80+
git_signature_free(signature);
81+
git_tree_free(tree);
82+
83+
return error;
84+
}

examples/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ extern int lg2_blame(git_repository *repo, int argc, char **argv);
5959
extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
6060
extern int lg2_checkout(git_repository *repo, int argc, char **argv);
6161
extern int lg2_clone(git_repository *repo, int argc, char **argv);
62+
extern int lg2_commit(git_repository *repo, int argc, char **argv);
6263
extern int lg2_config(git_repository *repo, int argc, char **argv);
6364
extern int lg2_describe(git_repository *repo, int argc, char **argv);
6465
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);
7172
extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
7273
extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
7374
extern int lg2_merge(git_repository *repo, int argc, char **argv);
75+
extern int lg2_push(git_repository *repo, int argc, char **argv);
7476
extern int lg2_remote(git_repository *repo, int argc, char **argv);
7577
extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
7678
extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);

examples/lg2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct {
1515
{ "cat-file", lg2_cat_file, 1 },
1616
{ "checkout", lg2_checkout, 1 },
1717
{ "clone", lg2_clone, 0 },
18+
{ "commit", lg2_commit, 1 },
1819
{ "config", lg2_config, 1 },
1920
{ "describe", lg2_describe, 1 },
2021
{ "diff", lg2_diff, 1 },
@@ -27,6 +28,7 @@ struct {
2728
{ "ls-files", lg2_ls_files, 1 },
2829
{ "ls-remote", lg2_ls_remote, 1 },
2930
{ "merge", lg2_merge, 1 },
31+
{ "push", lg2_push, 1 },
3032
{ "remote", lg2_remote, 1 },
3133
{ "rev-list", lg2_rev_list, 1 },
3234
{ "rev-parse", lg2_rev_parse, 1 },

examples/push.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* libgit2 "push" example - shows how to push to remote
3+
*
4+
* Written by the libgit2 contributors
5+
*
6+
* To the extent possible under law, the author(s) have dedicated all copyright
7+
* and related and neighboring rights to this software to the public domain
8+
* worldwide. This software is distributed without any warranty.
9+
*
10+
* You should have received a copy of the CC0 Public Domain Dedication along
11+
* with this software. If not, see
12+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
*/
14+
15+
#include "common.h"
16+
17+
/**
18+
* This example demonstrates the libgit2 push API to roughly
19+
* simulate `git push`.
20+
*
21+
* This does not have:
22+
*
23+
* - Robust error handling
24+
* - Any of the `git push` options
25+
*
26+
* This does have:
27+
*
28+
* - Example of push to origin/master
29+
*
30+
*/
31+
32+
/** Entry point for this command */
33+
int lg2_push(git_repository *repo, int argc, char **argv) {
34+
git_push_options options;
35+
git_remote* remote = NULL;
36+
char *refspec = "refs/heads/master";
37+
const git_strarray refspecs = {
38+
&refspec,
39+
1
40+
};
41+
42+
/* Validate args */
43+
if (argc > 1) {
44+
printf ("USAGE: %s\n\nsorry, no arguments supported yet\n", argv[0]);
45+
return -1;
46+
}
47+
48+
check_lg2(git_remote_lookup(&remote, repo, "origin" ), "Unable to lookup remote", NULL);
49+
50+
check_lg2(git_push_options_init(&options, GIT_PUSH_OPTIONS_VERSION ), "Error initializing push", NULL);
51+
52+
check_lg2(git_remote_push(remote, &refspecs, &options), "Error pushing", NULL);
53+
54+
printf("pushed\n");
55+
return 0;
56+
}

0 commit comments

Comments
 (0)