KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_init_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_init_handler.h"
27#include <trace_helpers.h>
28#include <wx/log.h>
29
31{}
32
34{}
35
36bool GIT_INIT_HANDLER::IsRepository( const wxString& aPath )
37{
38 git_repository* repo = nullptr;
39 int error = git_repository_open( &repo, aPath.mb_str() );
40
41 if( error == 0 )
42 {
43 git_repository_free( repo );
44 return true;
45 }
46
47 return false;
48}
49
51{
52 // Check if directory is already a git repository
53 if( IsRepository( aPath ) )
54 {
55 return InitResult::AlreadyExists;
56 }
57
58 git_repository* repo = nullptr;
59
60 if( git_repository_init( &repo, aPath.mb_str(), 0 ) != GIT_OK )
61 {
62 if( repo )
63 git_repository_free( repo );
64
65 AddErrorString( wxString::Format( _( "Failed to initialize Git repository: %s" ),
67 return InitResult::Error;
68 }
69
70 // Update the common repository pointer
71 GetCommon()->SetRepo( repo );
72
73 wxLogTrace( traceGit, "Successfully initialized Git repository at %s", aPath );
74 return InitResult::Success;
75}
76
78{
79 // This is an optional step
80 if( aConfig.url.IsEmpty() )
81 return true;
82
83 git_repository* repo = GetRepo();
84
85 if( !repo )
86 {
87 AddErrorString( _( "No repository available to set up remote" ) );
88 return false;
89 }
90
91 // Update connection settings in common
92 GetCommon()->SetUsername( aConfig.username );
93 GetCommon()->SetPassword( aConfig.password );
94 GetCommon()->SetSSHKey( aConfig.sshKey );
95
96 git_remote* remote = nullptr;
97 wxString fullURL;
98
99 // Build the full URL based on connection type
101 {
102 fullURL = aConfig.username + "@" + aConfig.url;
103 }
105 {
106 fullURL = aConfig.url.StartsWith( "https" ) ? "https://" : "http://";
107
108 if( !aConfig.username.empty() )
109 {
110 fullURL.append( aConfig.username );
111
112 if( !aConfig.password.empty() )
113 {
114 fullURL.append( wxS( ":" ) );
115 fullURL.append( aConfig.password );
116 }
117
118 fullURL.append( wxS( "@" ) );
119 }
120
121 // Extract the bare URL (without protocol prefix)
122 wxString bareURL = aConfig.url;
123 if( bareURL.StartsWith( "https://" ) )
124 bareURL = bareURL.Mid( 8 );
125 else if( bareURL.StartsWith( "http://" ) )
126 bareURL = bareURL.Mid( 7 );
127
128 fullURL.append( bareURL );
129 }
130 else
131 {
132 fullURL = aConfig.url;
133 }
134
135 int error = git_remote_create_with_fetchspec( &remote, repo, "origin",
136 fullURL.ToStdString().c_str(),
137 "+refs/heads/*:refs/remotes/origin/*" );
138
139 KIGIT::GitRemotePtr remotePtr( remote );
140
141 if( error != GIT_OK )
142 {
143 AddErrorString( wxString::Format( _( "Failed to create remote: %s" ),
145 return false;
146 }
147
148 wxLogTrace( traceGit, "Successfully set up remote origin" );
149 return true;
150}
151
152void GIT_INIT_HANDLER::UpdateProgress( int aCurrent, int aTotal, const wxString& aMessage )
153{
154 ReportProgress( aCurrent, aTotal, aMessage );
155}
bool IsRepository(const wxString &aPath)
Check if a directory is already a git repository.
virtual ~GIT_INIT_HANDLER()
void UpdateProgress(int aCurrent, int aTotal, const wxString &aMessage) override
GIT_INIT_HANDLER(KIGIT_COMMON *aCommon)
InitResult InitializeRepository(const wxString &aPath)
Initialize a new git repository in the specified directory.
bool SetupRemote(const RemoteConfig &aConfig)
Set up a remote for the repository.
void ReportProgress(int aCurrent, int aTotal, const wxString &aMessage)
Definition: git_progress.h:45
static wxString GetLastGitError()
void SetSSHKey(const wxString &aSSHKey)
void SetUsername(const wxString &aUsername)
void SetPassword(const wxString &aPassword)
void SetRepo(git_repository *aRepo)
void AddErrorString(const wxString aErrorString)
git_repository * GetRepo() const
Get a pointer to the git repository.
KIGIT_COMMON * GetCommon() const
Get the common object.
#define _(s)
InitResult
const wxChar *const traceGit
Flag to enable Git debugging output.
std::unique_ptr< git_remote, decltype([](git_remote *aRemote) { git_remote_free(aRemote) GitRemotePtr
A unique pointer for git_remote objects with automatic cleanup.
KIGIT_COMMON::GIT_CONN_TYPE connType
wxString username
wxString password
wxLogTrace helper definitions.