From bd56fcc200b3220f3f916a1ba25f4be984cee32a Mon Sep 17 00:00:00 2001 From: pauljoohyunkim Date: Sun, 20 Aug 2023 10:48:47 +0900 Subject: [PATCH 1/2] Piece of code to a concrete program - Global initialization added. - "#include " added - Comments about how to compile added --- book/B-embedding-git/sections/libgit2.asc | 51 +++++++++++++++-------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/book/B-embedding-git/sections/libgit2.asc b/book/B-embedding-git/sections/libgit2.asc index 2e006fdc0..36cb54963 100644 --- a/book/B-embedding-git/sections/libgit2.asc +++ b/book/B-embedding-git/sections/libgit2.asc @@ -10,24 +10,39 @@ Here's a whirlwind tour: [source,c] ---- -// Open a repository -git_repository *repo; -int error = git_repository_open(&repo, "/path/to/repository"); - -// Dereference HEAD to a commit -git_object *head_commit; -error = git_revparse_single(&head_commit, repo, "HEAD^{commit}"); -git_commit *commit = (git_commit*)head_commit; - -// Print some of the commit's properties -printf("%s", git_commit_message(commit)); -const git_signature *author = git_commit_author(commit); -printf("%s <%s>\n", author->name, author->email); -const git_oid *tree_id = git_commit_tree_id(commit); - -// Cleanup -git_commit_free(commit); -git_repository_free(repo); +// If the name of this file is "head_commit_print.c", Compile this with: +// CC head_commit_print.c -o head_commit_print -lgit2 +// where CC is the C compiler of your choice (eg. gcc, clang, ...) +#include +#include + +int main() +{ + // Initialize global state of git + git_libgit2_init(); + + // Open a repository + git_repository *repo; + int error = git_repository_open(&repo, "/path/to/repository"); + + // Dereference HEAD to a commit + git_object *head_commit; + error = git_revparse_single(&head_commit, repo, "HEAD^{commit}"); + git_commit *commit = (git_commit*)head_commit; + + // Print some of the commit's properties + printf("%s", git_commit_message(commit)); + const git_signature *author = git_commit_author(commit); + printf("%s <%s>\n", author->name, author->email); + const git_oid *tree_id = git_commit_tree_id(commit); + + // Cleanup + git_commit_free(commit); + git_repository_free(repo); + + // Freeing resources from git_libgit2_init() + git_libgit2_shutdown(); +} ---- The first couple of lines open a Git repository. From e938e16286f8e5804fd5f21a05111dc80285ad53 Mon Sep 17 00:00:00 2001 From: pauljoohyunkim Date: Sun, 20 Aug 2023 10:56:43 +0900 Subject: [PATCH 2/2] Minor paragraph edit from code modification. Parts of the paragraph referencing lines from code changed. --- book/B-embedding-git/sections/libgit2.asc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/book/B-embedding-git/sections/libgit2.asc b/book/B-embedding-git/sections/libgit2.asc index 36cb54963..cd29ebc05 100644 --- a/book/B-embedding-git/sections/libgit2.asc +++ b/book/B-embedding-git/sections/libgit2.asc @@ -45,7 +45,8 @@ int main() } ---- -The first couple of lines open a Git repository. +The first line in the main function initializes the global state for using libgit2 library functions. +The next couple of lines open a Git repository. The `git_repository` type represents a handle to a repository with a cache in memory. This is the simplest method, for when you know the exact path to a repository's working directory or `.git` folder. There's also the `git_repository_open_ext` which includes options for searching, `git_clone` and friends for making a local clone of a remote repository, and `git_repository_init` for creating an entirely new repository.