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;
67 git_checkout_init_options(&opts, GIT_CHECKOUT_OPTIONS_VERSION);
68
69 // Get the HEAD commit
70 if (git_revparse_single(&head_commit, m_repository, "HEAD") != 0) {
71 // Handle error. If we cannot get the HEAD, then there's no point proceeding.
72 return;
73 }
74
75 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
76 char** paths = new char*[m_filesToRevert.size()];
77
78 for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
79 {
80 // Set paths to the specific file
81 paths[ii] = wxStrdup( m_filesToRevert[ii].ToUTF8() );
82 }
83
84 git_strarray arr = { paths, m_filesToRevert.size() };
85
86 opts.paths = arr;
87 opts.progress_cb = checkout_progress_cb;
88 opts.notify_cb = checkout_notify_cb;
89 opts.notify_payload = static_cast<void*>(this);
90
91 // Attempt to checkout the file(s)
92 if (git_checkout_tree(m_repository, head_commit, &opts) != 0)
93 {
94 const git_error *e = git_error_last();
95 if (e)
96 {
97 wxLogError( "Checkout failed: %d: %s", e->klass, e->message );
98 }
99 }
100
101 // Free the HEAD commit
102 for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
103 delete( paths[ii] );
104
105 delete[] paths;
106
107 git_object_free(head_commit);
108}
109
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)