Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit a7a6eab

Browse files
authored
Merge pull request #2 from jhasse/code-readme
Use code block in README.md instead of screenshot
2 parents 7c06968 + 77a73bc commit a7a6eab

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

README.md

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<img height="100" src="img/logo.png"/>
2+
<img height="100" src="img/logo.png"/>
33
</p>
44

55
<p align="center">
@@ -15,11 +15,33 @@
1515
<img src="https://img.shields.io/badge/version-0.1.0-blue.svg?cacheSeconds=2592000" alt="version"/>
1616
</p>
1717

18-
`cppgit2` is a `libgit2` wrapper library for use in modern C++ `( >= C++11)`. See the [Build and Integration](#build-and-integration) section for details on how to build and integrate `cppgit2` in your projects.
18+
`cppgit2` is a `libgit2` wrapper library for use in modern C++ `( >= C++11)`. See the [Build and Integration](#build-and-integration) section for details on how to build and integrate `cppgit2` in your projects.
1919

20-
<p align="center">
21-
<img src="img/init_add_commit.png"/>
22-
</p>
20+
```cpp
21+
// Create new repo
22+
std::string repo_name = "my_project";
23+
auto repo = repository::init(repo_name, false);
24+
25+
// Write README file
26+
std::string file_name = "README.md";
27+
auto readme = std::ofstream(repo_name + "/" + file_name);
28+
readme << "Hello, World!\n";
29+
readme.close();
30+
31+
// Stage README.md
32+
auto index = repo.index();
33+
index.add_entry_by_path(file_name);
34+
index.write();
35+
36+
// Prepare signatures
37+
auto author = signature("foobar", "foo.bar@baz.com");
38+
auto committer = author;
39+
40+
// Create commit
41+
auto tree_oid = index.write_tree();
42+
repo.create_commit("HEAD", author, committer, "utf-8", "Update README",
43+
repo.lookup_tree(tree_oid), {});
44+
```
2345

2446
## Table of Contents
2547

@@ -45,7 +67,7 @@
4567

4668
## Build and Integration
4769

48-
Run the following commands to build `cppgit2`.
70+
Run the following commands to build `cppgit2`.
4971

5072
**NOTE**: This also builds `libgit2` from source. `libgit2` is a submodule in the `ext/` directory that points to a stable release commit, e.g., [v0.99.0](https://github.com/libgit2/libgit2/releases/tag/v0.99.0).
5173

@@ -83,7 +105,7 @@ For integration in your projects,
83105
* Add `build/include` to your `include_directories`
84106
* Add `build/lib` to your `link_directories`
85107
* Build your application, linking with `cppgit2`
86-
* Add `build/lib` to your `LD_LIBRARY_PATH` to load the shared libraries at runtime.
108+
* Add `build/lib` to your `LD_LIBRARY_PATH` to load the shared libraries at runtime.
87109

88110
Here's an example using `g++`:
89111

@@ -108,11 +130,11 @@ SET_PROPERTY(TARGET my_sample PROPERTY CXX_STANDARD 11)
108130

109131
## Sample Programs
110132

111-
This section presents some simple examples illustrating various `cppgit2` features. You can find the full set of available examples in the `/samples` directory. Samples are still a work-in-progress. Pull requests are welcome here.
133+
This section presents some simple examples illustrating various `cppgit2` features. You can find the full set of available examples in the `/samples` directory. Samples are still a work-in-progress. Pull requests are welcome here.
112134

113135
### Initialize a new repository (`git init`)
114136

115-
To initialize a new repository, simply call `repository::init`.
137+
To initialize a new repository, simply call `repository::init`.
116138

117139
```cpp
118140
#include <cppgit2/repository.hpp>
@@ -123,11 +145,11 @@ int main() {
123145
}
124146
```
125147

126-
If you want to create a bare repository, set the second argument to `true`.
148+
If you want to create a bare repository, set the second argument to `true`.
127149

128150
### Clone a repository and checkout specific branch (`git clone --branch`)
129151

130-
Let's say you want to clone a repository and checkout a specific branch. Construct an `options` object using `clone::options`, set the checkout branch name, and then use `repository::clone` to clone the repository.
152+
Let's say you want to clone a repository and checkout a specific branch. Construct an `options` object using `clone::options`, set the checkout branch name, and then use `repository::clone` to clone the repository.
131153

132154
```cpp
133155
#include <cppgit2/repository.hpp>
@@ -256,7 +278,7 @@ Date: Thu Mar 19 20:48:07 2020 -0500
256278
257279
README.md | 1 +
258280
1 file changed, 1 insertion(+)
259-
281+
260282
261283
```
262284

@@ -399,7 +421,7 @@ int main(int argc, char **argv) {
399421
auto repo = repository::open(argv[1]);
400422

401423
repo.for_each_commit([](const commit &c) {
402-
std::cout << c.id().to_hex_string(8)
424+
std::cout << c.id().to_hex_string(8)
403425
<< " [" << c.committer().name() << "]"
404426
<< " " << c.summary() << std::endl;
405427
});
@@ -515,10 +537,10 @@ void show_tree(const tree &tree) {
515537
for (size_t i = 0; i < tree.size(); ++i) {
516538
auto entry = tree.lookup_entry_by_index(i);
517539

518-
std::cout << std::setfill('0') <<
540+
std::cout << std::setfill('0') <<
519541
std::oct << std::setw(6) << static_cast<git_filemode_t>(entry.filemode());
520542
std::cout << " " << object::object_type_to_string(entry.type())
521-
<< " " << entry.id().to_hex_string()
543+
<< " " << entry.id().to_hex_string()
522544
<< "\t" << entry.filename() << std::endl;
523545
}
524546
}
@@ -657,7 +679,7 @@ $ ./cat_file -s 8765a97b5b120259dd59262865ce166f382c0f9e
657679
658680
### Interoperability with `libgit2`
659681
660-
Most `cppgit2` data structures can be constructed using a `libgit2` C pointer.
682+
Most `cppgit2` data structures can be constructed using a `libgit2` C pointer.
661683
662684
```cpp
663685
// Construct libgit2 signature
@@ -692,18 +714,18 @@ REQUIRE(oid1.to_hex_string(8) == std::string(oid1_formatted)); // f9de917
692714
693715
### Ownership and Memory Management
694716
695-
`libgit2` sometimes allocates memory and returns pointers to data structures that are owned by the user (required to be free'd by the user), and at other times returns a pointer to memory that is managed by the `libgit2` layer.
717+
`libgit2` sometimes allocates memory and returns pointers to data structures that are owned by the user (required to be free'd by the user), and at other times returns a pointer to memory that is managed by the `libgit2` layer.
696718
697719
To properly cleanup memory that is owned by the user, use the `ownership` enum to explicitly specify the ownership when wrapping.
698720
699721
```cpp
700722
cppgit2::tree tree1(&tree_cptr, ownership::user);
701723
```
702724

703-
If the pointer being wrapped is owned by the user, the class destructor will call `git_<type>_free` on the pointer and clean up properly. If you specify the ownership as `ownership::libgit2`, the pointer is left alone.
725+
If the pointer being wrapped is owned by the user, the class destructor will call `git_<type>_free` on the pointer and clean up properly. If you specify the ownership as `ownership::libgit2`, the pointer is left alone.
704726

705727
```cpp
706-
tree::tree(git_tree *c_ptr, ownership owner = ownership::libgit2)
728+
tree::tree(git_tree *c_ptr, ownership owner = ownership::libgit2)
707729
: c_ptr_(c_ptr), owner_(owner) {}
708730

709731
tree::~tree() {
@@ -1642,7 +1664,7 @@ virtual const char *what() const throw() { return message_; }
16421664
| `git_submodule_add_setup` | `repository::setup_submodule` |
16431665
| `git_submodule_add_to_index` | `submodule::add_to_index` |
16441666
| `git_submodule_branch` | `submodule::branch_name` |
1645-
| `git_submodule_clone` | `submodule::clone` |
1667+
| `git_submodule_clone` | `submodule::clone` |
16461668
| `git_submodule_fetch_recurse_submodules` | `submodule::recuse_submodules_option` |
16471669
| `git_submodule_foreach` | `repository::for_each_submodule` |
16481670
| `git_submodule_free` | `submodule::~submodule` |

img/init_add_commit.png

-409 KB
Binary file not shown.

0 commit comments

Comments
 (0)