KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
git_remove_from_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 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 <wx/string.h>
25#include <wx/log.h>
26
29
31{
32 m_repository = aRepository;
33 m_filesToRemove.clear();
34}
35
36
38{
39}
40
41
42bool GIT_REMOVE_FROM_INDEX_HANDLER::RemoveFromIndex( 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 KIGIT::GitIndexPtr indexPtr( index );
56
57 if( git_index_find( &at_pos, index, aFilePath.ToUTF8().data() ) != 0 )
58 {
59 wxLogError( "Failed to find index entry for %s", aFilePath );
60 return false;
61 }
62
63 m_filesToRemove.push_back( aFilePath );
64 return true;
65}
66
67
69{
70 for( auto& file : m_filesToRemove )
71 {
72 git_index* index = nullptr;
73 git_oid oid;
74
75 if( git_repository_index( &index, m_repository ) != 0 )
76 {
77 wxLogError( "Failed to get repository index" );
78 return;
79 }
80
81 KIGIT::GitIndexPtr indexPtr( index );
82
83 if( git_index_remove_bypath( index, file.ToUTF8().data() ) != 0 )
84 {
85 wxLogError( "Failed to remove index entry for %s", file );
86 return;
87 }
88
89 if( git_index_write( index ) != 0 )
90 {
91 wxLogError( "Failed to write index" );
92 return;
93 }
94
95 if( git_index_write_tree( &oid, index ) != 0 )
96 {
97 wxLogError( "Failed to write index tree" );
98 return;
99 }
100 }
101}
bool RemoveFromIndex(const wxString &aFilePath)
GIT_REMOVE_FROM_INDEX_HANDLER(git_repository *aRepository)
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex) GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.