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
33760f17
Commit
33760f17
authored
Sep 18, 2011
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #400 from boyski/fixup-examples
Fix up examples code
parents
b3c524d1
784b3b49
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
16 deletions
+23
-16
examples/Makefile
+9
-6
examples/general.c
+14
-10
No files found.
examples/Makefile
View file @
33760f17
all
:
general showindex
.PHONY
:
all
CC
=
gcc
CFLAGS
=
-g
-I
../include
LFLAGS
=
-L
../build
-lgit2
-lz
general
:
general.c
all
:
general showindex
gcc
-lgit2
-o
general general.c
showindex
:
showindex
.c
%
:
%
.c
gcc
-lgit2
-o
showindex showindex.c
$(CC)
-o
$@
$(CFLAGS)
$<
$(LFLAGS)
clean
:
clean
:
rm
general showindex
$(RM)
general showindex
examples/general.c
View file @
33760f17
...
@@ -38,7 +38,11 @@ int main (int argc, char** argv)
...
@@ -38,7 +38,11 @@ int main (int argc, char** argv)
//
//
// [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
// [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
git_repository
*
repo
;
git_repository
*
repo
;
git_repository_open
(
&
repo
,
"/opt/libgit2-test/.git"
);
if
(
argc
>
1
)
{
git_repository_open
(
&
repo
,
argv
[
1
]);
}
else
{
git_repository_open
(
&
repo
,
"/opt/libgit2-test/.git"
);
}
// ### SHA-1 Value Conversions
// ### SHA-1 Value Conversions
...
@@ -52,7 +56,7 @@ int main (int argc, char** argv)
...
@@ -52,7 +56,7 @@ int main (int argc, char** argv)
git_oid_fromstr
(
&
oid
,
hex
);
git_oid_fromstr
(
&
oid
,
hex
);
// Once we've converted the string into the oid value, we can get the raw value of the SHA.
// Once we've converted the string into the oid value, we can get the raw value of the SHA.
printf
(
"Raw 20 bytes: [%s]
\n
"
,
(
&
oid
)
->
id
);
printf
(
"Raw 20 bytes: [%
.20
s]
\n
"
,
(
&
oid
)
->
id
);
// Next we will convert the 20 byte raw SHA1 value to a human readable 40 char hex value.
// Next we will convert the 20 byte raw SHA1 value to a human readable 40 char hex value.
printf
(
"
\n
*Raw to Hex*
\n
"
);
printf
(
"
\n
*Raw to Hex*
\n
"
);
...
@@ -138,15 +142,14 @@ int main (int argc, char** argv)
...
@@ -138,15 +142,14 @@ int main (int argc, char** argv)
error
=
git_commit_lookup
(
&
commit
,
repo
,
&
oid
);
error
=
git_commit_lookup
(
&
commit
,
repo
,
&
oid
);
const
git_signature
*
author
,
*
cmtter
;
const
git_signature
*
author
,
*
cmtter
;
const
char
*
message
,
*
message_short
;
const
char
*
message
;
time_t
ctime
;
time_t
ctime
;
unsigned
int
parents
,
p
;
unsigned
int
parents
,
p
;
// Each of the properties of the commit object are accessible via methods, including commonly
// Each of the properties of the commit object are accessible via methods, including commonly
// needed variations, such as `git_commit_time` which returns the author time and `_message
_short
`
// needed variations, such as `git_commit_time` which returns the author time and `_message`
// which gives you
just the first line of
the commit message.
// which gives you the commit message.
message
=
git_commit_message
(
commit
);
message
=
git_commit_message
(
commit
);
message_short
=
git_commit_message_short
(
commit
);
author
=
git_commit_author
(
commit
);
author
=
git_commit_author
(
commit
);
cmtter
=
git_commit_committer
(
commit
);
cmtter
=
git_commit_committer
(
commit
);
ctime
=
git_commit_time
(
commit
);
ctime
=
git_commit_time
(
commit
);
...
@@ -187,9 +190,9 @@ int main (int argc, char** argv)
...
@@ -187,9 +190,9 @@ int main (int argc, char** argv)
// this to create a commit in order to specify who created it and when. Default values for the name
// this to create a commit in order to specify who created it and when. Default values for the name
// and email should be found in the `user.name` and `user.email` configuration options. See the `config`
// 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.
// section of this example file to see how to access config values.
author
=
git_signature_new
(
"Scott Chacon"
,
"schacon@gmail.com"
,
git_signature_new
((
git_signature
**
)
&
author
,
"Scott Chacon"
,
"schacon@gmail.com"
,
123456789
,
60
);
123456789
,
60
);
cmtter
=
git_signature_new
(
"Scott A Chacon"
,
"scott@github.com"
,
git_signature_new
((
git_signature
**
)
&
cmtter
,
"Scott A Chacon"
,
"scott@github.com"
,
987654321
,
90
);
987654321
,
90
);
// Commit objects need a tree to point to and optionally one or more parents. Here we're creating oid
// Commit objects need a tree to point to and optionally one or more parents. Here we're creating oid
...
@@ -207,6 +210,7 @@ int main (int argc, char** argv)
...
@@ -207,6 +210,7 @@ int main (int argc, char** argv)
NULL
,
/* do not update the HEAD */
NULL
,
/* do not update the HEAD */
author
,
author
,
cmtter
,
cmtter
,
NULL
,
/* use default message encoding */
"example commit"
,
"example commit"
,
tree
,
tree
,
1
,
parent
);
1
,
parent
);
...
@@ -294,7 +298,7 @@ int main (int argc, char** argv)
...
@@ -294,7 +298,7 @@ int main (int argc, char** argv)
// Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files):
// Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files):
// do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to
// do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to
// find out its exact size in bytes
// find out its exact size in bytes
printf
(
"Blob Size: %d
\n
"
,
git_blob_rawsize
(
blob
));
// 8
printf
(
"Blob Size: %
l
d
\n
"
,
git_blob_rawsize
(
blob
));
// 8
git_blob_rawcontent
(
blob
);
// "content"
git_blob_rawcontent
(
blob
);
// "content"
// ### Revwalking
// ### Revwalking
...
@@ -333,7 +337,7 @@ int main (int argc, char** argv)
...
@@ -333,7 +337,7 @@ int main (int argc, char** argv)
// be cached in memory
// be cached in memory
while
((
git_revwalk_next
(
&
oid
,
walk
))
==
GIT_SUCCESS
)
{
while
((
git_revwalk_next
(
&
oid
,
walk
))
==
GIT_SUCCESS
)
{
error
=
git_commit_lookup
(
&
wcommit
,
repo
,
&
oid
);
error
=
git_commit_lookup
(
&
wcommit
,
repo
,
&
oid
);
cmsg
=
git_commit_message
_short
(
wcommit
);
cmsg
=
git_commit_message
(
wcommit
);
cauth
=
git_commit_author
(
wcommit
);
cauth
=
git_commit_author
(
wcommit
);
printf
(
"%s (%s)
\n
"
,
cmsg
,
cauth
->
email
);
printf
(
"%s (%s)
\n
"
,
cmsg
,
cauth
->
email
);
git_commit_close
(
wcommit
);
git_commit_close
(
wcommit
);
...
...
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