forked from p-ranav/cppgit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit_file.cpp
38 lines (31 loc) · 1.04 KB
/
commit_file.cpp
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
#include <cppgit2/repository.hpp>
#include <fstream>
#include <iostream>
using namespace cppgit2;
int main(int argc, char **argv) {
if (argc == 2) {
// Create new repo
auto repo = repository::init(argv[1], false);
// Write README file
std::ofstream readme;
readme.open(std::string{argv[1]} + "/README.md");
readme << "Hello, World!";
readme.close();
// Get repo index and write as tree
auto index = repo.index();
index.add_entry_by_path("README.md");
index.write();
auto tree_oid = index.write_tree();
// Prepare signatures
auto author = signature("foobar", "[email protected]");
auto committer = signature("foobar", "[email protected]");
// Create commit
auto commit_oid =
repo.create_commit("HEAD", author, committer, "utf-8", "Update README",
repo.lookup_tree(tree_oid), {});
std::cout << "Created commit with ID: " << commit_oid.to_hex_string()
<< std::endl;
} else {
std::cout << "Usage: ./executable <new_repo_path>\n";
}
}