KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_branch_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 "git_branch_handler.h"
27#include <trace_helpers.h>
28#include <wx/log.h>
29
31{}
32
34{}
35
36bool GIT_BRANCH_HANDLER::BranchExists( const wxString& aBranchName )
37{
38 git_repository* repo = GetRepo();
39
40 if( !repo )
41 return false;
42
43 git_reference* branchRef = nullptr;
44 bool exists = LookupBranchReference( aBranchName, &branchRef );
45
46 if( branchRef )
47 git_reference_free( branchRef );
48
49 return exists;
50}
51
52bool GIT_BRANCH_HANDLER::LookupBranchReference( const wxString& aBranchName, git_reference** aReference )
53{
54 git_repository* repo = GetRepo();
55
56 if( !repo )
57 return false;
58
59 // Try direct lookup first
60 if( git_reference_lookup( aReference, repo, aBranchName.mb_str() ) == GIT_OK )
61 return true;
62
63 // Try dwim (Do What I Mean) lookup for short branch names
64 if( git_reference_dwim( aReference, repo, aBranchName.mb_str() ) == GIT_OK )
65 return true;
66
67 return false;
68}
69
71{
72 git_repository* repo = GetRepo();
73
74 if( !repo )
75 {
76 AddErrorString( _( "No repository available" ) );
77 return BranchResult::Error;
78 }
79
80 // Look up the branch reference
81 git_reference* branchRef = nullptr;
82
83 if( !LookupBranchReference( aBranchName, &branchRef ) )
84 {
85 AddErrorString( wxString::Format( _( "Failed to lookup branch '%s': %s" ),
86 aBranchName, KIGIT_COMMON::GetLastGitError() ) );
87 return BranchResult::BranchNotFound;
88 }
89
90 KIGIT::GitReferencePtr branchRefPtr( branchRef );
91 const char* branchRefName = git_reference_name( branchRef );
92 git_object* branchObj = nullptr;
93
94 if( git_revparse_single( &branchObj, repo, aBranchName.mb_str() ) != GIT_OK )
95 {
96 AddErrorString( wxString::Format( _( "Failed to find branch head for '%s': %s" ),
97 aBranchName, KIGIT_COMMON::GetLastGitError() ) );
98 return BranchResult::Error;
99 }
100
101 KIGIT::GitObjectPtr branchObjPtr( branchObj );
102
103 // Switch to the branch
104 if( git_checkout_tree( repo, branchObj, nullptr ) != GIT_OK )
105 {
106 AddErrorString( wxString::Format( _( "Failed to switch to branch '%s': %s" ),
107 aBranchName, KIGIT_COMMON::GetLastGitError() ) );
108 return BranchResult::CheckoutFailed;
109 }
110
111 // Update the HEAD reference
112 if( git_repository_set_head( repo, branchRefName ) != GIT_OK )
113 {
114 AddErrorString( wxString::Format( _( "Failed to update HEAD reference for branch '%s': %s" ),
115 aBranchName, KIGIT_COMMON::GetLastGitError() ) );
116 return BranchResult::Error;
117 }
118
119 wxLogTrace( traceGit, "Successfully switched to branch '%s'", aBranchName );
120 return BranchResult::Success;
121}
122
123void GIT_BRANCH_HANDLER::UpdateProgress( int aCurrent, int aTotal, const wxString& aMessage )
124{
125 ReportProgress( aCurrent, aTotal, aMessage );
126}
BranchResult SwitchToBranch(const wxString &aBranchName)
Switch to the specified branch.
void UpdateProgress(int aCurrent, int aTotal, const wxString &aMessage) override
bool BranchExists(const wxString &aBranchName)
Check if a branch exists.
bool LookupBranchReference(const wxString &aBranchName, git_reference **aReference)
Look up a branch reference by name.
GIT_BRANCH_HANDLER(KIGIT_COMMON *aCommon)
void ReportProgress(int aCurrent, int aTotal, const wxString &aMessage)
Definition: git_progress.h:45
static wxString GetLastGitError()
void AddErrorString(const wxString aErrorString)
git_repository * GetRepo() const
Get a pointer to the git repository.
#define _(s)
BranchResult
const wxChar *const traceGit
Flag to enable Git debugging output.
std::unique_ptr< git_object, decltype([](git_object *aObject) { git_object_free(aObject) GitObjectPtr
A unique pointer for git_object objects with automatic cleanup.
std::unique_ptr< git_reference, decltype([](git_reference *aRef) { git_reference_free(aRef) GitReferencePtr
A unique pointer for git_reference objects with automatic cleanup.
wxLogTrace helper definitions.