KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_git_utils.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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include "project_git_utils.h"
22#include "git_backend.h"
23#include "kicad_git_common.h"
24
25#include <wx/file.h>
26#include <wx/filename.h>
27
28#include "kicad_git_memory.h"
30#include <trace_helpers.h>
31#include <wx/log.h>
32#include <wx/string.h>
33#include <string_utils.h>
34
35#ifndef __WINDOWS__
36#include <climits>
37#include <cstdlib>
38#endif
39
40namespace KIGIT
41{
42
43git_repository* PROJECT_GIT_UTILS::GetRepositoryForFile( const char* aFilename, wxString* aError,
44 wxString* aTrustCommand )
45{
46 GIT_BACKEND* backend = GetGitBackend();
47
48 if( !backend )
49 return nullptr;
50
51 return backend->GetRepositoryForFile( aFilename, aError, aTrustCommand );
52}
53
54
55int PROJECT_GIT_UTILS::CreateBranch( git_repository* aRepo, const wxString& aBranchName )
56{
57 GIT_BACKEND* backend = GetGitBackend();
58
59 if( !backend || !KIGIT::BranchNameError( aBranchName ).IsEmpty() )
60 return -1;
61
62 return backend->CreateBranch( aRepo, aBranchName, wxEmptyString );
63}
64
65
66bool PROJECT_GIT_UTILS::RemoveVCS( git_repository*& aRepo, const wxString& aProjectPath,
67 bool aRemoveGitDir, wxString* aErrors )
68{
69 GIT_BACKEND* backend = GetGitBackend();
70
71 if( !backend )
72 return false;
73
74 return backend->RemoveVCS( aRepo, aProjectPath, aRemoveGitDir, aErrors );
75}
76
77
79{
80 if( !aRepo )
81 return {};
82
83 using ENVIRONMENT = TEXT_EVAL::ENVIRONMENT;
84 const auto read = [&]() -> ENVIRONMENT::VCS_VALUE
85 {
86 git_oid oid{};
87
88 if( git_reference_name_to_id( &oid, aRepo, "HEAD" ) != 0 )
89 return {};
90
91 char hash[GIT_OID_HEXSZ + 1];
92 git_oid_tostr( hash, sizeof( hash ), &oid );
93 return { hash };
94 };
95 auto* environment = ENVIRONMENT::Current();
96 const char* path = git_repository_path( aRepo );
97 const auto value = environment && path
98 ? environment->VcsValue( { ENVIRONMENT::VCS_QUERY::HEAD, wxString::FromUTF8( path ), "", 0, false }, read )
99 : read();
100 git_oid oid{};
101
102 if( value.text.empty() || git_oid_fromstr( &oid, value.text.c_str() ) != 0 )
103 return {};
104
105 return oid;
106}
107
108
109wxString PROJECT_GIT_UTILS::GetCurrentHash( const wxString& aProjectFile, bool aShort )
110{
111 const auto read = [&]
112 {
113 GitRepositoryPtr repo( GetRepositoryForFile( TO_UTF8( aProjectFile ) ) );
114 const git_oid oid = GetCapturedHeadOid( repo.get() );
115
116 if( git_oid_is_zero( &oid ) )
117 return wxString( "no hash" );
118
119 char hash[GIT_OID_HEXSZ + 1];
120 git_oid_tostr( hash, sizeof( hash ), &oid );
121 return wxString::FromUTF8( hash );
122 };
123 auto* environment = TEXT_EVAL::ENVIRONMENT::Current();
124 const wxString hash = environment ? environment->GitHash( aProjectFile, read ) : read();
125 return aShort ? hash.Left( 8 ) : hash;
126}
127
128
129// The path with its symlinks resolved and a trailing separator, or the path unchanged when it
130// cannot be resolved. git reports paths under the resolved root, so a path only compares against
131// one of git's once both have been through this.
132static wxString canonicalDir( const wxString& aPath )
133{
134#ifdef __WINDOWS__
135 return aPath;
136#else
137 char resolved[PATH_MAX];
138
139 if( aPath.IsEmpty() || realpath( aPath.mb_str(), resolved ) == nullptr )
140 return aPath;
141
142 wxString out = wxString::FromUTF8( resolved );
143
144 if( !out.EndsWith( wxFileName::GetPathSeparator() ) && wxFileName::DirExists( out ) )
145 out += wxFileName::GetPathSeparator();
146
147 return out;
148#endif
149}
150
151
152wxString PROJECT_GIT_UTILS::ComputeSymlinkPreservingWorkDir( const wxString& aUserProjectPath,
153 const wxString& aCanonicalWorkDir )
154{
155#ifdef __WINDOWS__
156 return aCanonicalWorkDir;
157#else
158 if( aUserProjectPath.IsEmpty() || aCanonicalWorkDir.IsEmpty() )
159 return aCanonicalWorkDir;
160
161 char resolvedPath[PATH_MAX];
162
163 if( realpath( aUserProjectPath.mb_str(), resolvedPath ) == nullptr )
164 return aCanonicalWorkDir;
165
166 wxString canonicalUserPath = wxString::FromUTF8( resolvedPath );
167
168 if( !canonicalUserPath.EndsWith( wxFileName::GetPathSeparator() ) )
169 canonicalUserPath += wxFileName::GetPathSeparator();
170
171 wxString canonicalWorkDirNorm = aCanonicalWorkDir;
172
173 if( !canonicalWorkDirNorm.EndsWith( wxFileName::GetPathSeparator() ) )
174 canonicalWorkDirNorm += wxFileName::GetPathSeparator();
175
176 // The user project path resolves to the same directory as the git workdir (e.g., the
177 // project is at the repo root but opened via a symlinked path). Return the user path
178 // to preserve symlinks. When the project is in a subdirectory of the workdir, the
179 // paths will differ and we fall through to extract only the workdir portion below.
180 if( canonicalUserPath == canonicalWorkDirNorm )
181 {
182 return aUserProjectPath.EndsWith( wxFileName::GetPathSeparator() )
183 ? aUserProjectPath
184 : aUserProjectPath + wxFileName::GetPathSeparator();
185 }
186
187 // Walk both paths upward in lockstep until the canonical user path matches the canonical
188 // workdir. This correctly handles symlinks that compress multiple canonical path
189 // components into fewer visible components (e.g. /work/repo -> /real/deep/path/root).
190 wxFileName userFn;
191 userFn.AssignDir( aUserProjectPath );
192
193 wxFileName canonicalUserFn;
194 canonicalUserFn.AssignDir( canonicalUserPath );
195
196 wxFileName canonicalWorkDirFn;
197 canonicalWorkDirFn.AssignDir( canonicalWorkDirNorm );
198
199 while( canonicalUserFn.GetFullPath() != canonicalWorkDirFn.GetFullPath() )
200 {
201 if( canonicalUserFn.GetDirCount() == 0 || userFn.GetDirCount() == 0 )
202 return aCanonicalWorkDir;
203
204 canonicalUserFn.RemoveLastDir();
205 userFn.RemoveLastDir();
206 }
207
208 // One symlink can stand for several directories, so stepping up the visible path a component
209 // at a time can land somewhere that is not the repository. Only an answer that resolves back
210 // to the working directory is an alias for it.
211 if( canonicalDir( userFn.GetPathWithSep() ) != canonicalWorkDirNorm )
212 return aCanonicalWorkDir;
213
214 return userFn.GetPathWithSep();
215#endif
216}
217
218
219static bool pathIsUnder( const wxString& aAbsPath, const wxString& aProjectPath )
220{
221 wxString absPath = aAbsPath;
222 wxString projectPath = aProjectPath;
223
224#ifdef __WINDOWS__
225 absPath.Replace( wxT( "\\" ), wxT( "/" ) );
226 projectPath.Replace( wxT( "\\" ), wxT( "/" ) );
227#endif
228
229 if( !projectPath.EndsWith( wxT( "/" ) ) )
230 projectPath += wxT( "/" );
231
232#ifdef __WINDOWS__
233 return absPath.Lower().StartsWith( projectPath.Lower() );
234#else
235 return absPath.StartsWith( projectPath );
236#endif
237}
238
239
240bool PROJECT_GIT_UTILS::IsWithinProjectPath( const wxString& aAbsPath, const wxString& aProjectPath )
241{
242 if( aProjectPath.IsEmpty() )
243 return false;
244
245 if( pathIsUnder( aAbsPath, aProjectPath ) )
246 return true;
247
248 // The project can be reached through a symlink the repository path does not share, and then
249 // the two only line up once both are resolved.
250 return pathIsUnder( canonicalDir( aAbsPath ), canonicalDir( aProjectPath ) );
251}
252
253
254static std::optional<wxString> prefixUnder( const wxString& aWorkDir, const wxString& aProjectPath )
255{
256 wxString workDir = aWorkDir;
257 wxString projectPath = aProjectPath;
258
259 workDir.Replace( wxS( "\\" ), wxS( "/" ) );
260 projectPath.Replace( wxS( "\\" ), wxS( "/" ) );
261
262 if( !workDir.EndsWith( wxS( "/" ) ) )
263 workDir += wxS( "/" );
264
265 if( !projectPath.EndsWith( wxS( "/" ) ) )
266 projectPath += wxS( "/" );
267
268#ifdef __WINDOWS__
269 // Windows paths can differ in case.
270 if( !projectPath.Lower().StartsWith( workDir.Lower() ) )
271 return std::nullopt;
272#else
273 if( !projectPath.StartsWith( workDir ) )
274 return std::nullopt;
275#endif
276
277 return projectPath.Mid( workDir.length() );
278}
279
280
281std::optional<wxString> PROJECT_GIT_UTILS::ProjectPrefix( const wxString& aWorkDir, const wxString& aProjectPath )
282{
283 if( aWorkDir.IsEmpty() || aProjectPath.IsEmpty() )
284 return std::nullopt;
285
286 std::optional<wxString> prefix = prefixUnder( aWorkDir, aProjectPath );
287
288 // Resolve symlinks when the project and repository use different paths to the same folder.
289 if( !prefix )
290 prefix = prefixUnder( canonicalDir( aWorkDir ), canonicalDir( aProjectPath ) );
291
292 return prefix;
293}
294
295
296bool PROJECT_GIT_UTILS::PathUsesGitLfs( git_repository* aRepo, const wxString& aPath, const git_oid* aCommit )
297{
298 if( !aRepo || aPath.IsEmpty() )
299 return false;
300
301 const char* value = nullptr;
302
303#if LIBGIT2_VER_MAJOR > 1 || ( LIBGIT2_VER_MAJOR == 1 && LIBGIT2_VER_MINOR >= 3 )
304 // Check attributes from the index, working tree, and optionally the specified commit.
305 git_attr_options opts = {};
306 opts.version = GIT_ATTR_OPTIONS_VERSION;
307 opts.flags = GIT_ATTR_CHECK_INDEX_THEN_FILE;
308
309 if( aCommit )
310 {
311 // Include the commit's rules when checking a version before checkout.
312 opts.flags |= GIT_ATTR_CHECK_INCLUDE_COMMIT;
313 git_oid_cpy( &opts.attr_commit_id, aCommit );
314 }
315
316 if( git_attr_get_ext( &value, aRepo, &opts, aPath.utf8_string().c_str(), "filter" ) != GIT_OK )
317 return false;
318#else
319 // Only check the index and working tree.
320
321 if( git_attr_get( &value, aRepo, GIT_ATTR_CHECK_INDEX_THEN_FILE, aPath.utf8_string().c_str(), "filter" ) != GIT_OK )
322 {
323 wxLogTrace( traceGit, "Could not read Git attributes: %s", KIGIT_COMMON::GetLastGitError() );
324 return true;
325 }
326#endif
327
328 return git_attr_value( value ) == GIT_ATTR_VALUE_STRING && strcmp( value, "lfs" ) == 0;
329}
330
331
332namespace
333{
334
335 struct LFS_WALK
336 {
337 git_repository* repo;
338 std::string prefix;
339 wxString example;
340 const git_oid* commit = nullptr;
341 };
342
343
344 int lfsWalkEntry( const char* aRoot, const git_tree_entry* aEntry, void* aPayload )
345 {
346 LFS_WALK* walk = static_cast<LFS_WALK*>( aPayload );
347 std::string path = std::string( aRoot ) + git_tree_entry_name( aEntry );
348
349 if( git_tree_entry_type( aEntry ) == GIT_OBJECT_TREE )
350 {
351 std::string dir = path + "/";
352
353 // Descend only where the project's folder can be.
354 if( walk->prefix.compare( 0, dir.length(), dir ) != 0
355 && dir.compare( 0, walk->prefix.length(), walk->prefix ) != 0 )
356 {
357 return 1;
358 }
359
360 return 0;
361 }
362
363 if( git_tree_entry_type( aEntry ) != GIT_OBJECT_BLOB )
364 return 0;
365
366 if( path.compare( 0, walk->prefix.length(), walk->prefix ) != 0 )
367 return 0;
368
369 if( PROJECT_GIT_UTILS::PathUsesGitLfs( walk->repo, wxString::FromUTF8( path.c_str() ), walk->commit ) )
370 {
371 walk->example = wxString::FromUTF8( path.c_str() );
372 return -1;
373 }
374
375 return 0;
376 }
377
378} // namespace
379
380
382{
383 if( !aCommon || !aCommon->GetRepo() )
384 return wxEmptyString;
385
386 if( !aCommon->GetProjectPrefix() )
387 {
388 return _( "Could not work out which folder this project occupies in the repository, "
389 "so the operation was cancelled." );
390 }
391
392 wxString example;
393
394 if( !ProjectUsesGitLfs( *aCommon, &example ) )
395 return wxEmptyString;
396
397 return wxString::Format( _( "This project keeps '%s' in Git LFS, which KiCad cannot read or "
398 "write. Use git on the command line for this repository." ),
399 example );
400}
401
402
403bool PROJECT_GIT_UTILS::ProjectUsesGitLfs( const KIGIT_COMMON& aCommon, wxString* aExample, const git_oid* aCommit )
404{
405 const std::optional<wxString> prefix = aCommon.GetProjectPrefix();
406
407 if( !prefix )
408 return false;
409
410 git_repository* repo = aCommon.GetRepo();
411 git_oid commitOid;
412
413 if( aCommit )
414 git_oid_cpy( &commitOid, aCommit );
415 else if( git_reference_name_to_id( &commitOid, repo, "HEAD" ) != GIT_OK )
416 return false;
417
418 git_commit* commit = nullptr;
419
420 if( git_commit_lookup( &commit, repo, &commitOid ) != GIT_OK )
421 return false;
422
423 KIGIT::GitCommitPtr commitPtr( commit );
424 git_tree* tree = nullptr;
425
426 if( git_commit_tree( &tree, commit ) != GIT_OK )
427 return false;
428
429 KIGIT::GitTreePtr treePtr( tree );
430 LFS_WALK walk{ repo, prefix->utf8_string(), wxEmptyString, aCommit };
431
432 git_tree_walk( tree, GIT_TREEWALK_PRE, lfsWalkEntry, &walk );
433
434 if( aExample )
435 *aExample = walk.example;
436
437 return !walk.example.IsEmpty();
438}
439
440
442{
443 static const std::vector<wxString> patterns = {
444 wxS( ".history/" ),
445 wxS( "*-backups/" ),
446 wxS( "_autosave-*" ),
447 wxS( "fp-info-cache" ),
448 wxS( "~*" ),
449 wxS( "*.[lL][cC][kK]" ),
450 wxS( "*-bak" ),
451
452 // The symbol editor's copy of a library from before its last save
453 wxS( "*.bak" ),
454 wxS( "*.[kK][iI][cC][aA][dD]_[pP][rR][lL]" ),
455
456 // Recovery copies that earlier builds left beside the design files.
457 wxS( "*.mine" ),
458 wxS( "*.mine.[0-9]" ),
459 wxS( "*.mine.[0-9][0-9]" ),
460 wxS( "*.theirs" ),
461 wxS( "*.theirs.[0-9]" ),
462 wxS( "*.theirs.[0-9][0-9]" ),
463 };
464
465 return patterns;
466}
467
468
469namespace
470{
471
472 using PATHSPEC_PTR = std::unique_ptr<git_pathspec, decltype( &git_pathspec_free )>;
473
474 // A slash limits a rule to directories. The other rules also match directory names in Git.
475 PATHSPEC_PTR transientPathspec( bool aDirectory )
476 {
477 std::vector<std::string> patterns;
478
479 for( wxString pattern : PROJECT_GIT_UTILS::TransientGitIgnorePatterns() )
480 {
481 if( pattern.EndsWith( wxS( "/" ) ) )
482 {
483 if( !aDirectory )
484 continue;
485
486 pattern.RemoveLast();
487 }
488
489 patterns.push_back( pattern.utf8_string() );
490 }
491
492 std::vector<char*> paths;
493
494 for( std::string& pattern : patterns )
495 paths.push_back( pattern.data() );
496
497 git_strarray pathArray = { paths.data(), paths.size() };
498 git_pathspec* spec = nullptr;
499
500 if( git_pathspec_new( &spec, &pathArray ) != GIT_OK )
501 return PATHSPEC_PTR( nullptr, git_pathspec_free );
502
503 return PATHSPEC_PTR( spec, git_pathspec_free );
504 }
505
506} // namespace
507
508
509bool PROJECT_GIT_UTILS::IsTransientGitFile( const wxFileName& aFile )
510{
511 static const PATHSPEC_PTR filePatterns = transientPathspec( false );
512 static const PATHSPEC_PTR directoryPatterns = transientPathspec( true );
513
514 if( filePatterns && git_pathspec_matches_path( filePatterns.get(), 0, aFile.GetFullName().utf8_str() ) )
515 return true;
516
517 for( const wxString& dir : aFile.GetDirs() )
518 {
519 if( directoryPatterns && git_pathspec_matches_path( directoryPatterns.get(), 0, dir.utf8_str() ) )
520 return true;
521 }
522
523 return false;
524}
525
526
527void PROJECT_GIT_UTILS::SafeFreeRepository( std::shared_ptr<KIGIT_COMMON> aCommon, const std::string& aLabel )
528{
529 if( !aCommon )
530 return;
531
532 // Raised before any exit below, so a session being let go never reads as live whatever
533 // state it is in.
534 aCommon->SetShuttingDown( true );
535
536 git_repository* repo = aCommon->GetRepo();
537
538 if( !repo )
539 return;
540
541 // A null backend means libgit2 is already shut down at process exit. Freeing now would
542 // call into a torn-down library, so drop the handle and let the OS reclaim it.
543 if( !GetGitBackend() )
544 {
545 aCommon->SetRepo( nullptr );
546 return;
547 }
548
549 {
550 std::unique_lock<std::mutex> gitLock = aCommon->TryLockAction();
551
552 if( gitLock.owns_lock() )
553 {
554 aCommon->SetRepo( nullptr );
555 git_repository_free( repo );
556 return;
557 }
558 }
559
560 // An operation still holds the action mutex, so the handle must stay valid until it
561 // finishes. The orphan registry frees it once the mutex is released.
562 auto cleanup = [repo, old = std::move( aCommon )]() mutable
563 {
564 std::unique_lock<std::mutex> g = old->LockAction();
565 old->SetRepo( nullptr );
566 git_repository_free( repo );
567 };
568
569 GIT_BACKEND* backend = GetGitBackend();
570
571 if( !backend || !backend->OrphanRegistry().Register( aLabel, std::move( cleanup ) ) )
572 {
573 // No registry is available, so free synchronously rather than leak. Blocking on
574 // the mutex here is safe because libgit2 shutdown is gated on the registry.
575 cleanup();
576 }
577}
578
579
580bool PROJECT_GIT_UTILS::WriteBlobToFile( git_repository* aRepo, const wxString& aRef, const wxString& aPath,
581 const wxString& aDestPath )
582{
583 if( !aRepo || aRef.IsEmpty() || aPath.IsEmpty() )
584 return false;
585
586 KIGIT::GitTreePtr treePtr( KIGIT::ResolveRefToTree( aRepo, aRef ) );
587
588 if( !treePtr )
589 return false;
590
591 git_tree_entry* entry = nullptr;
592
593 if( git_tree_entry_bypath( &entry, treePtr.get(), aPath.ToUTF8().data() ) != 0 )
594 return false;
595
596 KIGIT::GitTreeEntryPtr entryPtr( entry );
597
598 if( git_tree_entry_type( entry ) != GIT_OBJECT_BLOB )
599 return false;
600
601 git_object* blobObj = nullptr;
602
603 if( git_tree_entry_to_object( &blobObj, aRepo, entry ) != 0 )
604 return false;
605
606 KIGIT::GitObjectPtr blobObjPtr( blobObj );
607 git_blob* blob = reinterpret_cast<git_blob*>( blobObj );
608
609 // Committed file contents can be sensitive and the destination lives in the shared temp
610 // directory, so create it readable and writable only by the current user.
611 wxFile out;
612
613 if( !out.Create( aDestPath, /* overwrite */ true, wxS_IRUSR | wxS_IWUSR ) )
614 return false;
615
616 const void* raw = git_blob_rawcontent( blob );
617 git_off_t size = git_blob_rawsize( blob );
618
619 return out.Write( raw, static_cast<size_t>( size ) ) == static_cast<size_t>( size );
620}
621
622
623bool PROJECT_GIT_UTILS::WriteTreeToDir( git_repository* aRepo, const wxString& aRef, const wxString& aDestDir )
624{
625 if( !aRepo || aRef.IsEmpty() || aDestDir.IsEmpty() )
626 return false;
627
628 KIGIT::GitTreePtr treePtr( KIGIT::ResolveRefToTree( aRepo, aRef ) );
629
630 if( !treePtr )
631 return false;
632
633 // Owner-only, matching the 0600 blob files written into it.
634 if( !wxFileName::Mkdir( aDestDir, 0700, wxPATH_MKDIR_FULL ) )
635 return false;
636
637 std::string destUtf8 = aDestDir.utf8_string();
638 git_checkout_options opts;
639 git_checkout_options_init( &opts, GIT_CHECKOUT_OPTIONS_VERSION );
640
641 // Without DONT_UPDATE_INDEX, git_checkout_tree rewrites .git/index to the checked-out
642 // tree, replacing whatever the user has staged.
643 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_RECREATE_MISSING | GIT_CHECKOUT_DONT_UPDATE_INDEX;
644 opts.target_directory = destUtf8.c_str();
645
646 return git_checkout_tree( aRepo, reinterpret_cast<git_object*>( treePtr.get() ), &opts ) == GIT_OK;
647}
648
649} // namespace KIGIT
virtual int CreateBranch(git_repository *aRepo, const wxString &aBranchName, const wxString &aFrom)=0
KIGIT_ORPHAN_REGISTRY & OrphanRegistry()
Return the process-wide orphan thread registry owned by this backend.
virtual git_repository * GetRepositoryForFile(const char *aFilename, wxString *aError, wxString *aTrustCommand)=0
aError receives why git refused to open the repository, and aTrustCommand the command that trusts it ...
virtual bool RemoveVCS(git_repository *&aRepo, const wxString &aProjectPath, bool aRemoveGitDir, wxString *aErrors)=0
static bool WriteBlobToFile(git_repository *aRepo, const wxString &aRef, const wxString &aPath, const wxString &aDestPath)
Write a file as it exists at a git ref to a path on disk, so a committed version can be diffed.
static bool PathUsesGitLfs(git_repository *aRepo, const wxString &aPath, const git_oid *aCommit=nullptr)
Whether git routes aPath through the Git LFS filter.
static git_oid GetCapturedHeadOid(git_repository *aRepo)
Return HEAD for commit-based text queries.
static wxString GetCurrentHash(const wxString &aProjectFile, bool aShort)
Return the current HEAD commit hash for the repository containing aProjectFile.
static git_repository * GetRepositoryForFile(const char *aFilename, wxString *aError=nullptr, wxString *aTrustCommand=nullptr)
Discover and open the repository that contains the given file.
static bool IsTransientGitFile(const wxFileName &aFile)
Match a project-relative path against the shared transient patterns.
static const std::vector< wxString > & TransientGitIgnorePatterns()
Shared patterns for new .gitignore files and the untracked-file filter.
static void SafeFreeRepository(std::shared_ptr< KIGIT_COMMON > aCommon, const std::string &aLabel)
Free the session's repository handle without pulling it out from under a running operation.
static wxString ComputeSymlinkPreservingWorkDir(const wxString &aUserProjectPath, const wxString &aCanonicalWorkDir)
Compute a working directory path that preserves symlinks from the user's project path.
static bool WriteTreeToDir(git_repository *aRepo, const wxString &aRef, const wxString &aDestDir)
Check out a whole tree into a directory, leaving the repository's working copy and index alone.
static bool ProjectUsesGitLfs(const KIGIT_COMMON &aCommon, wxString *aExample=nullptr, const git_oid *aCommit=nullptr)
Whether any committed file in the project's folder goes through Git LFS.
static bool IsWithinProjectPath(const wxString &aAbsPath, const wxString &aProjectPath)
Test whether an absolute file path lives inside the current project directory.
static bool RemoveVCS(git_repository *&aRepo, const wxString &aProjectPath=wxEmptyString, bool aRemoveGitDir=false, wxString *aErrors=nullptr)
Remove version control from a directory by freeing the repository and optionally removing the ....
static wxString LfsRefusal(KIGIT_COMMON *aCommon)
Return a refusal message for LFS files or an unknown project folder, otherwise empty.
static int CreateBranch(git_repository *aRepo, const wxString &aBranchName)
Create a new branch based on HEAD.
static std::optional< wxString > ProjectPrefix(const wxString &aWorkDir, const wxString &aProjectPath)
The project's folder inside the repository, ending in a slash.
The project's git session.
std::optional< wxString > GetProjectPrefix() const
Project folder relative to the repository, with a trailing slash.
git_repository * GetRepo() const
static wxString GetLastGitError()
libgit2's last message, without credentials from any URL it quotes, so it is safe to show or trace.
bool Register(const std::string &aLabel, F &&aWork)
Spawn a tracked orphan thread running aWork.
A text evaluation frame with one frozen clock and memoized external queries.
static ENVIRONMENT * Current()
#define _(s)
GIT_BACKEND * GetGitBackend()
const wxChar *const traceGit
Flag to enable Git debugging output.
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
static bool pathIsUnder(const wxString &aAbsPath, const wxString &aProjectPath)
wxString BranchNameError(const wxString &aName)
Empty when aName is a branch name git would accept, otherwise why it would not, in the words to show ...
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
git_tree * ResolveRefToTree(git_repository *aRepo, const wxString &aRef)
Resolve a string ref (branch name, short OID, full OID, tag) to its tree.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
static wxString canonicalDir(const wxString &aPath)
std::unique_ptr< git_tree_entry, decltype([](git_tree_entry *aEntry) { git_tree_entry_free(aEntry); })> GitTreeEntryPtr
A unique pointer for git_tree_entry objects with automatic cleanup.
static std::optional< wxString > prefixUnder(const wxString &aWorkDir, const wxString &aProjectPath)
std::unique_ptr< git_object, decltype([](git_object *aObject) { git_object_free(aObject); })> GitObjectPtr
A unique pointer for git_object objects with automatic cleanup.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
std::string path
wxLogTrace helper definitions.