KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_revert_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, 2024 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_revert_handler.h"
25
26#include <wx/log.h>
27#include <wx/string.h>
28
29#include <trace_helpers.h>
30
31
32GIT_REVERT_HANDLER::GIT_REVERT_HANDLER( git_repository* aRepository )
33{
34 m_repository = aRepository;
35}
36
37
39{
40}
41
42
43bool GIT_REVERT_HANDLER::Revert( const wxString& aFilePath )
44{
45 m_filesToRevert.push_back( aFilePath );
46 return true;
47}
48
49
50static void checkout_progress_cb( const char *path, size_t cur, size_t tot, void *payload )
51{
52 wxLogTrace( traceGit, wxS( "checkout_progress_cb: %s %zu/%zu" ), path, cur, tot );
53}
54
55
56static int checkout_notify_cb( git_checkout_notify_t why, const char *path,
57 const git_diff_file *baseline,
58 const git_diff_file *target,
59 const git_diff_file *workdir, void *payload )
60{
61 GIT_REVERT_HANDLER* handler = static_cast<GIT_REVERT_HANDLER*>(payload);
62
63 if( why & ( GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_IGNORED
64 | GIT_CHECKOUT_NOTIFY_UPDATED ) )
65 handler->PushFailedFile( path );
66
67 return 0;
68}
69
70
72{
73 git_object* head_commit = NULL;
74 git_checkout_options opts;
75 git_checkout_init_options( &opts, GIT_CHECKOUT_OPTIONS_VERSION );
76
77 // Get the HEAD commit
78 if( git_revparse_single( &head_commit, m_repository, "HEAD" ) != 0 )
79 {
80 // Handle error. If we cannot get the HEAD, then there's no point proceeding.
81 return;
82 }
83
84 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
85 char** paths = new char*[m_filesToRevert.size()];
86
87 for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
88 {
89 // Set paths to the specific file
90 paths[ii] = wxStrdup( m_filesToRevert[ii].ToUTF8() );
91 }
92
93 git_strarray arr = { paths, m_filesToRevert.size() };
94
95 opts.paths = arr;
96 opts.progress_cb = checkout_progress_cb;
97 opts.notify_cb = checkout_notify_cb;
98 opts.notify_payload = static_cast<void*>( this );
99
100 // Attempt to checkout the file(s)
101 if( git_checkout_tree(m_repository, head_commit, &opts ) != 0 )
102 {
103 const git_error *e = git_error_last();
104
105 if( e )
106 {
107 wxLogTrace( traceGit, wxS( "Checkout failed: %d: %s" ), e->klass, e->message );
108 }
109 }
110
111 // Free the HEAD commit
112 for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
113 delete( paths[ii] );
114
115 delete[] paths;
116
117 git_object_free( head_commit );
118}
119
GIT_REVERT_HANDLER(git_repository *aRepository)
std::vector< wxString > m_filesToRevert
git_repository * m_repository
bool Revert(const wxString &aFilePath)
void PushFailedFile(const wxString &aFilePath)
static int checkout_notify_cb(git_checkout_notify_t why, const char *path, const git_diff_file *baseline, const git_diff_file *target, const git_diff_file *workdir, void *payload)
static void checkout_progress_cb(const char *path, size_t cur, size_t tot, void *payload)
const wxChar *const traceGit
Flag to enable Git debugging output.
wxLogTrace helper definitions.