Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
b009adad
Commit
b009adad
authored
Aug 16, 2016
by
Patrick Steinhardt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: general: extract function demonstrating commit writing
parent
15960454
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
36 deletions
+51
-36
examples/general.c
+51
-36
No files found.
examples/general.c
View file @
b009adad
...
...
@@ -43,6 +43,7 @@
#include <stdio.h>
#include <string.h>
static
void
commit_writing
(
git_repository
*
repo
);
static
void
commit_parsing
(
git_repository
*
repo
);
static
void
tag_parsing
(
git_repository
*
repo
);
static
void
tree_parsing
(
git_repository
*
repo
);
...
...
@@ -175,43 +176,69 @@ int main (int argc, char** argv)
git_oid_fmt
(
out
,
&
oid
);
printf
(
"Written Object: %s
\n
"
,
out
);
commit_writing
(
repo
);
commit_parsing
(
repo
);
tag_parsing
(
repo
);
tree_parsing
(
repo
);
blob_parsing
(
repo
);
revwalking
(
repo
);
index_walking
(
repo
);
reference_listing
(
repo
);
config_files
(
repo_path
);
// #### Writing Commits
// Finally, when you're done with the repository, you can free it as well.
git_repository_free
(
repo
);
// libgit2 provides a couple of methods to create commit objects easily as
// well. There are four different create signatures, we'll just show one
// of them here. You can read about the other ones in the [commit API
// docs][cd].
//
// [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
return
0
;
}
printf
(
"
\n
*Commit Writing*
\n
"
);
/**
* #### Writing Commits
*
* libgit2 provides a couple of methods to create commit objects easily as
* well. There are four different create signatures, we'll just show one
* of them here. You can read about the other ones in the [commit API
* docs][cd].
*
* [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
*/
static
void
commit_writing
(
git_repository
*
repo
)
{
git_oid
tree_id
,
parent_id
,
commit_id
;
git_tree
*
tree
;
git_commit
*
parent
;
const
git_signature
*
author
,
*
cmtter
;
char
oid_hex
[
GIT_OID_HEXSZ
+
1
]
=
{
0
};
// Creating signatures for an authoring identity and time is simple. You
// will need to do this to specify who created a commit and when. Default
// values for the name and email should be found in the `user.name` and
// `user.email` configuration options. See the `config` section of this
// example file to see how to access config values.
printf
(
"
\n
*Commit Writing*
\n
"
);
/**
* Creating signatures for an authoring identity and time is simple. You
* will need to do this to specify who created a commit and when. Default
* values for the name and email should be found in the `user.name` and
* `user.email` configuration options. See the `config` section of this
* example file to see how to access config values.
*/
git_signature_new
((
git_signature
**
)
&
author
,
"Scott Chacon"
,
"schacon@gmail.com"
,
123456789
,
60
);
git_signature_new
((
git_signature
**
)
&
cmtter
,
"Scott A Chacon"
,
"scott@github.com"
,
987654321
,
90
);
// Commit objects need a tree to point to and optionally one or more
// parents. Here we're creating oid objects to create the commit with,
// but you can also use
/**
* Commit objects need a tree to point to and optionally one or more
* parents. Here we're creating oid objects to create the commit with,
* but you can also use
*/
git_oid_fromstr
(
&
tree_id
,
"f60079018b664e4e79329a7ef9559c8d9e0378d1"
);
git_tree_lookup
(
&
tree
,
repo
,
&
tree_id
);
git_oid_fromstr
(
&
parent_id
,
"5b5b025afb0b4c913b4c338a42934a3863bf3644"
);
git_commit_lookup
(
&
parent
,
repo
,
&
parent_id
);
// Here we actually create the commit object with a single call with all
// the values we need to create the commit. The SHA key is written to the
// `commit_id` variable here.
/**
* Here we actually create the commit object with a single call with all
* the values we need to create the commit. The SHA key is written to the
* `commit_id` variable here.
*/
git_commit_create_v
(
&
commit_id
,
/* out id */
repo
,
...
...
@@ -223,23 +250,11 @@ int main (int argc, char** argv)
tree
,
1
,
parent
);
// Now we can take a look at the commit SHA we've generated.
git_oid_fmt
(
out
,
&
commit_id
);
printf
(
"New Commit: %s
\n
"
,
out
);
commit_parsing
(
repo
);
tag_parsing
(
repo
);
tree_parsing
(
repo
);
blob_parsing
(
repo
);
revwalking
(
repo
);
index_walking
(
repo
);
reference_listing
(
repo
);
config_files
(
repo_path
);
// Finally, when you're done with the repository, you can free it as well.
git_repository_free
(
repo
);
return
0
;
/**
* Now we can take a look at the commit SHA we've generated.
*/
git_oid_fmt
(
oid_hex
,
&
commit_id
);
printf
(
"New Commit: %s
\n
"
,
oid_hex
);
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment