KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_commit_handler.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "git_commit_handler.h"
26#include <wx/log.h>
27
29 KIGIT_COMMON( aRepo )
30{}
31
32
34{}
35
36
38GIT_COMMIT_HANDLER::PerformCommit( const std::vector<wxString>& aFiles,
39 const wxString& aMessage,
40 const wxString& aAuthorName,
41 const wxString& aAuthorEmail )
42{
43 git_repository* repo = GetRepo();
44
45 if( !repo )
47
48 git_index* index = nullptr;
49
50 if( git_repository_index( &index, repo ) != 0 )
51 {
52 AddErrorString( wxString::Format( _( "Failed to get repository index: %s" ),
55 }
56
57 KIGIT::GitIndexPtr indexPtr( index );
58
59 for( const wxString& file : aFiles )
60 {
61 if( git_index_add_bypath( index, file.mb_str() ) != 0 )
62 {
63 AddErrorString( wxString::Format( _( "Failed to add file to index: %s" ),
66 }
67 }
68
69 if( git_index_write( index ) != 0 )
70 {
71 AddErrorString( wxString::Format( _( "Failed to write index: %s" ),
74 }
75
76 git_oid tree_id;
77
78 if( git_index_write_tree( &tree_id, index ) != 0 )
79 {
80 AddErrorString( wxString::Format( _( "Failed to write tree: %s" ),
83 }
84
85 git_tree* tree = nullptr;
86
87 if( git_tree_lookup( &tree, repo, &tree_id ) != 0 )
88 {
89 AddErrorString( wxString::Format( _( "Failed to lookup tree: %s" ),
92 }
93
94 KIGIT::GitTreePtr treePtr( tree );
95 git_commit* parent = nullptr;
96
97 if( git_repository_head_unborn( repo ) == 0 )
98 {
99 git_reference* headRef = nullptr;
100
101 if( git_repository_head( &headRef, repo ) != 0 )
102 {
103 AddErrorString( wxString::Format( _( "Failed to get HEAD reference: %s" ),
105 return CommitResult::Error;
106 }
107
108 KIGIT::GitReferencePtr headRefPtr( headRef );
109
110 if( git_reference_peel( (git_object**) &parent, headRef, GIT_OBJECT_COMMIT ) != 0 )
111 {
112 AddErrorString( wxString::Format( _( "Failed to get commit: %s" ),
114 return CommitResult::Error;
115 }
116 }
117
118 KIGIT::GitCommitPtr parentPtr( parent );
119
120 git_signature* author = nullptr;
121
122 if( git_signature_now( &author, aAuthorName.mb_str(), aAuthorEmail.mb_str() ) != 0 )
123 {
124 AddErrorString( wxString::Format( _( "Failed to create author signature: %s" ),
126 return CommitResult::Error;
127 }
128
129 KIGIT::GitSignaturePtr authorPtr( author );
130 git_oid oid;
131 size_t parentsCount = parent ? 1 : 0;
132#if( LIBGIT2_VER_MAJOR == 1 && LIBGIT2_VER_MINOR == 8 \
133 && ( LIBGIT2_VER_REVISION < 2 || LIBGIT2_VER_REVISION == 3 ) )
134 git_commit* const parents[1] = { parent };
135 git_commit** const parentsPtr = parent ? parents : nullptr;
136#else
137 const git_commit* parents[1] = { parent };
138 const git_commit** parentsPtr = parent ? parents : nullptr;
139#endif
140
141
142
143 if( git_commit_create( &oid, repo, "HEAD", author, author, nullptr,
144 aMessage.mb_str(), tree, parentsCount, parentsPtr ) != 0 )
145 {
146 AddErrorString( wxString::Format( _( "Failed to create commit: %s" ),
148 return CommitResult::Error;
149 }
150
152}
153
154
156{
157 return m_errorString;
158}
159
160
161void GIT_COMMIT_HANDLER::AddErrorString( const wxString& aErrorString )
162{
163 m_errorString += aErrorString;
164}
165
CommitResult PerformCommit(const std::vector< wxString > &aFiles, const wxString &aMessage, const wxString &aAuthorName, const wxString &aAuthorEmail)
void AddErrorString(const wxString &aErrorString)
wxString GetErrorString() const
GIT_COMMIT_HANDLER(git_repository *aRepo)
static wxString GetLastGitError()
git_repository * GetRepo() const
#define _(s)
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit) GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree) GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex) GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
std::unique_ptr< git_signature, decltype([](git_signature *aSignature) { git_signature_free(aSignature) GitSignaturePtr
A unique pointer for git_signature objects with automatic cleanup.
std::unique_ptr< git_reference, decltype([](git_reference *aRef) { git_reference_free(aRef) GitReferencePtr
A unique pointer for git_reference objects with automatic cleanup.