KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_add_to_index_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 (C) 2023 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
25
26#include <iterator>
27
28#include <wx/string.h>
29#include <wx/log.h>
30
32{
33 m_repository = aRepository;
34 m_filesToAdd.clear();
35}
36
38{
39}
40
41
42bool GIT_ADD_TO_INDEX_HANDLER::AddToIndex( const wxString& aFilePath )
43{
44 // Test if file is currently in the index
45
46 git_index* index = nullptr;
47 size_t at_pos = 0;
48
49 if( git_repository_index( &index, m_repository ) != 0 )
50 {
51 wxLogError( "Failed to get repository index" );
52 return false;
53 }
54
55 if( git_index_find( &at_pos, index, aFilePath.ToUTF8().data() ) == GIT_OK )
56 {
57 git_index_free( index );
58 wxLogError( "%s already in index", aFilePath );
59 return false;
60 }
61
62 git_index_free( index );
63
64 // Add file to index if not already there
65 m_filesToAdd.push_back( aFilePath );
66
67 return true;
68}
69
70
72{
73 git_index* index = nullptr;
74
75 m_filesFailedToAdd.clear();
76
77 if( git_repository_index( &index, m_repository ) != 0 )
78 {
79 wxLogError( "Failed to get repository index" );
80 std::copy( m_filesToAdd.begin(), m_filesToAdd.end(), std::back_inserter( m_filesFailedToAdd ) );
81 return false;
82 }
83
84 for( auto& file : m_filesToAdd )
85 {
86 if( git_index_add_bypath( index, file.ToUTF8().data() ) != 0 )
87 {
88 wxLogError( "Failed to add %s to index", file );
89 m_filesFailedToAdd.push_back( file );
90 continue;
91 }
92 }
93
94
95 if( git_index_write( index ) != 0 )
96 {
97 wxLogError( "Failed to write index" );
98 m_filesFailedToAdd.clear();
99 std::copy( m_filesToAdd.begin(), m_filesToAdd.end(), std::back_inserter( m_filesFailedToAdd ) );
100 git_index_free( index );
101 return false;
102 }
103
104 git_index_free( index );
105
106 return true;
107}
GIT_ADD_TO_INDEX_HANDLER(git_repository *aRepository)
std::vector< wxString > m_filesToAdd
std::vector< wxString > m_filesFailedToAdd
bool AddToIndex(const wxString &aFilePath)