2023-09-14 21:39:42 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2024-05-23 11:59:45 +00:00
|
|
|
* Copyright (C) 2023, 2024 KiCad Developers, see AUTHORS.TXT for contributors.
|
2023-09-14 21:39:42 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "git_revert_handler.h"
|
|
|
|
|
|
|
|
#include <wx/log.h>
|
|
|
|
#include <wx/string.h>
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
#include <trace_helpers.h>
|
|
|
|
|
|
|
|
|
2023-09-14 21:39:42 +00:00
|
|
|
GIT_REVERT_HANDLER::GIT_REVERT_HANDLER( git_repository* aRepository )
|
|
|
|
{
|
|
|
|
m_repository = aRepository;
|
|
|
|
}
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
|
2023-09-14 21:39:42 +00:00
|
|
|
GIT_REVERT_HANDLER::~GIT_REVERT_HANDLER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
|
2023-09-14 21:39:42 +00:00
|
|
|
bool GIT_REVERT_HANDLER::Revert( const wxString& aFilePath )
|
|
|
|
{
|
|
|
|
m_filesToRevert.push_back( aFilePath );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
|
|
|
|
static void checkout_progress_cb( const char *path, size_t cur, size_t tot, void *payload )
|
2023-09-14 21:39:42 +00:00
|
|
|
{
|
2024-05-23 11:59:45 +00:00
|
|
|
wxLogTrace( traceGit, wxS( "checkout_progress_cb: %s %zu/%zu" ), path, cur, tot );
|
2023-09-14 21:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
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 )
|
2023-09-14 21:39:42 +00:00
|
|
|
{
|
|
|
|
GIT_REVERT_HANDLER* handler = static_cast<GIT_REVERT_HANDLER*>(payload);
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
if( why & ( GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_IGNORED
|
|
|
|
| GIT_CHECKOUT_NOTIFY_UPDATED ) )
|
2023-09-14 21:39:42 +00:00
|
|
|
handler->PushFailedFile( path );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
|
2023-09-14 21:39:42 +00:00
|
|
|
void GIT_REVERT_HANDLER::PerformRevert()
|
|
|
|
{
|
|
|
|
git_object* head_commit = NULL;
|
2024-05-06 17:18:38 +00:00
|
|
|
git_checkout_options opts;
|
2024-05-23 11:59:45 +00:00
|
|
|
git_checkout_init_options( &opts, GIT_CHECKOUT_OPTIONS_VERSION );
|
2023-09-14 21:39:42 +00:00
|
|
|
|
|
|
|
// Get the HEAD commit
|
2024-05-23 11:59:45 +00:00
|
|
|
if( git_revparse_single( &head_commit, m_repository, "HEAD" ) != 0 )
|
|
|
|
{
|
2023-09-14 21:39:42 +00:00
|
|
|
// Handle error. If we cannot get the HEAD, then there's no point proceeding.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
|
|
|
|
char** paths = new char*[m_filesToRevert.size()];
|
|
|
|
|
|
|
|
for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
|
|
|
|
{
|
|
|
|
// Set paths to the specific file
|
|
|
|
paths[ii] = wxStrdup( m_filesToRevert[ii].ToUTF8() );
|
|
|
|
}
|
|
|
|
|
|
|
|
git_strarray arr = { paths, m_filesToRevert.size() };
|
|
|
|
|
|
|
|
opts.paths = arr;
|
|
|
|
opts.progress_cb = checkout_progress_cb;
|
|
|
|
opts.notify_cb = checkout_notify_cb;
|
2024-05-23 11:59:45 +00:00
|
|
|
opts.notify_payload = static_cast<void*>( this );
|
2023-09-14 21:39:42 +00:00
|
|
|
|
|
|
|
// Attempt to checkout the file(s)
|
2024-05-23 11:59:45 +00:00
|
|
|
if( git_checkout_tree(m_repository, head_commit, &opts ) != 0 )
|
2023-09-14 21:39:42 +00:00
|
|
|
{
|
|
|
|
const git_error *e = git_error_last();
|
2024-05-23 11:59:45 +00:00
|
|
|
|
|
|
|
if( e )
|
2023-09-14 21:39:42 +00:00
|
|
|
{
|
2024-05-23 11:59:45 +00:00
|
|
|
wxLogTrace( traceGit, wxS( "Checkout failed: %d: %s" ), e->klass, e->message );
|
2023-09-14 21:39:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the HEAD commit
|
|
|
|
for( size_t ii = 0; ii < m_filesToRevert.size(); ii++ )
|
|
|
|
delete( paths[ii] );
|
|
|
|
|
|
|
|
delete[] paths;
|
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
git_object_free( head_commit );
|
2023-09-14 21:39:42 +00:00
|
|
|
}
|
|
|
|
|