-
Notifications
You must be signed in to change notification settings - Fork 2
/
Commit.cpp
98 lines (78 loc) · 2.99 KB
/
Commit.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Commit.h"
#include "Repository.h"
#include "LibGitException.h"
#include "SmartLibGitHeapPointerTemplate.hpp"
using Commit = Git::Commit;
using Repository = Git::Repository;
Commit::Commit(const std::string& _repositoryKey
, const std::string& _sha
) : RepositoryDependant(_repositoryKey)
, sha(_sha)
{
Repository repository(_repositoryKey);
if(!repository.isOwnedByRepo(*this))
{
throw std::invalid_argument("Not found in repository.");
}
git_commit* commit = nullptr;
commit = getLibGitCommit(sha).get();
message = std::string(git_commit_message(commit));
author = getAuthor(commit);
committer = getCommitter(commit);
parentShas = getParentShas(commit);
commitDate = committer.when;
if(parentShas.size() > 2)
{
throw std::domain_error("Currently only commits with at most two parents are supported.");
}
}
/*private*/ Commit::Signature Commit::getAuthor(const git_commit* commit) const
{
uniquePtr<git_signature> signature = getAuthorLibGitSignature(commit);
Signature author( signature->name
, signature->email
, static_cast<std::time_t>(signature->when.time) + signature->when.offset * 60
);
return author;
}
/*private*/ Commit::Signature Commit::getCommitter(const git_commit* commit) const
{
uniquePtr<git_signature> signature = getCommitterLibGitSignature(commit);
Signature committer( signature->name
, signature->email
, static_cast<std::time_t>(signature->when.time) + signature->when.offset * 60
);
return committer;
}
/* private */ std::vector<std::string> Commit::getParentShas(const git_commit* commit) const
{
unsigned int parentCount = git_commit_parentcount(commit);
if(parentCount > 2)
{
throw std::domain_error("Currently commits with more than two parents are not supported.");
}
std::vector<std::string> shas;
for(std::size_t i = 0; i < parentCount; ++i)
{
shas.push_back(libGitOidToSha(git_commit_parent_id(commit, i)));
}
return shas;
}
/* private */ std::unique_ptr< git_signature
, void(*)(git_signature*)
> Commit::getAuthorLibGitSignature(const git_commit* commit) const
{
git_signature* signatureRaw = nullptr;
checkLibGitError( git_signature_dup(&signatureRaw, git_commit_author(commit)) );
uniquePtr<git_signature> author(signatureRaw, git_signature_free);
return author;
}
/* private */ std::unique_ptr< git_signature
, void(*)(git_signature*)
> Commit::getCommitterLibGitSignature(const git_commit* commit) const
{
git_signature* signatureRaw = nullptr;
checkLibGitError( git_signature_dup(&signatureRaw, git_commit_committer(commit)) );
uniquePtr<git_signature> committer(signatureRaw, git_signature_free);
return committer;
}