forked from AndreyG/libgit2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-graph-generator.cpp
More file actions
76 lines (63 loc) · 1.84 KB
/
Copy pathcommit-graph-generator.cpp
File metadata and controls
76 lines (63 loc) · 1.84 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <fstream>
#include <git2cpp/repo.h>
#include <git2cpp/initializer.h>
namespace
{
struct visitor_t
{
void operator() (git::RevWalker & walker)
{
walker.sort(git::RevWalker::sorting::topological);
walker.simplify_first_parent();
while (git::Commit commit = walker.next())
{
output_commit(commit);
for (size_t i = 0; i != commit.parents_num(); ++i)
{
output_hash(commit.id()) << " -> ";
output_hash(commit.parent_id(i)) << "\n";
}
for (size_t i = 1; i < commit.parents_num(); ++i)
{
auto branch_walker = repo_.rev_walker();
branch_walker.push(commit.parent_id(i));
branch_walker.hide(commit.merge_base(0, i));
(*this)(branch_walker);
}
}
}
visitor_t(git::Repository const & repo, std::ostream & out)
: repo_(repo)
, out_(out)
{}
private:
std::ostream& output_hash(git_oid const & id) const
{
out_ << "C" << git::id_to_str(id, 6);
return out_;
}
void output_commit(git::Commit const & commit) const
{
output_hash(commit.id()) << " [label=\"" << commit.summary() << "\"];" << "\n";
}
private:
git::Repository const & repo_;
std::ostream & out_;
};
void visit(git::Repository const & repo, std::ostream & out)
{
visitor_t visitor(repo, out);
git::RevWalker walker = repo.rev_walker();
walker.push_head();
visitor(walker);
}
}
int main(int argc, char * argv[])
{
auto_git_initializer;
git::Repository repo (argc >= 2 ? argv[1] : ".");
std::ofstream out (argc >= 3 ? argv[2] : "commit-graph.dot");
out << "digraph {\n";
visit(repo, out);
out << "}\n";
}