FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
libgit2/examples/commit.c at v1.3.0 · libgit2/libgit2 · GitHub
libgit2
/
libgit2
Public
Notifications
You must be signed in to change notification settings
Fork
2.7k
Star
10.6k
Code
Issues
424
Pull requests
128
Discussions
Actions
Projects
Security and quality
11
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
libgit2
/
examples
/
commit.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (73 loc) · 2.23 KB
Breadcrumbs
libgit2
/
examples
/
commit.c
Copy path
File metadata and controls
84 lines (73 loc) · 2.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* 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
;
}
Back
|
FazBrowse Home
|
New Git URL