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 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
29GIT_REVERT_HANDLER::GIT_REVERT_HANDLER( git_repository* aRepository )
30{
31 m_repository = aRepository;
32}
33
35{
36}
37
38bool GIT_REVERT_HANDLER::Revert( const wxString& aFilePath )
39{
40 m_filesToRevert.push_back( aFilePath );
41 return true;
42}
43
44static void checkout_progress_cb(const char *path, size_t cur, size_t tot, void *payload)
45{
46 wxLogDebug( "checkout_progress_cb: %s %zu/%zu", path, cur, tot );
47}
48
49
50static int checkout_notify_cb(git_checkout_notify_t why, const char *path,
51 const git_diff_file *baseline,
52 const git_diff_file *target,
53 const git_diff_file *workdir, void *payload)
54{
55 GIT_REVERT_HANDLER* handler = static_cast<GIT_REVERT_HANDLER*>(payload);
56
57 if( why & ( GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_IGNORED | GIT_CHECKOUT_NOTIFY_UPDATED ) )
58 handler->PushFailedFile( path );
59
60 return 0;
61}
62
64{
65 git_object* head_commit = NULL;
66 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
67
68 // Get the HEAD commit
69 if (git_revparse_single(&head_commit, m_repository, "HEAD") != 0) {
70 // Handle error. If we cannot get the HEAD, then there's no point proceeding.
71 return;
72 }
73
74 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
75 char** paths = new char*[m_filesToRevert.size()];
76
77 for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
78 {
79 // Set paths to the specific file
80 paths[ii] = wxStrdup( m_filesToRevert[ii].ToUTF8() );
81 }
82
83 git_strarray arr = { paths, m_filesToRevert.size() };
84
85 opts.paths = arr;
86 opts.progress_cb = checkout_progress_cb;
87 opts.notify_cb = checkout_notify_cb;
88 opts.notify_payload = static_cast<void*>(this);
89
90 // Attempt to checkout the file(s)
91 if (git_checkout_tree(m_repository, head_commit, &opts) != 0)
92 {
93 const git_error *e = git_error_last();
94 if (e)
95 {
96 wxLogError( "Checkout failed: %d: %s", e->klass, e->message );
97 }
98 }
99
100 // Free the HEAD commit
101 for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
102 delete( paths[ii] );
103
104 delete[] paths;
105
106 git_object_free(head_commit);
107}
108
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)