35#include <wx/filename.h>
37#include <wx/tokenzr.h>
46 if( ignoreFile.FileExists() )
52 wxS(
"KiCad-generated files and directories." ), error );
66 static const std::pair<wxString, wxString> kicadEntries[] = {
67 { wxS(
"*.kicad_pcb" ), wxS(
"merge=kicad-pcb" ) },
68 { wxS(
"*.kicad_sch" ), wxS(
"merge=kicad-sch" ) },
69 { wxS(
"*.kicad_sym" ), wxS(
"merge=kicad-sym-lib" ) },
70 { wxS(
"*.kicad_mod" ), wxS(
"merge=kicad-fp" ) },
73 wxFileName attrFile( aProjectPath, wxS(
".gitattributes" ) );
76 std::set<wxString> existingPatterns;
77 bool fileExists = attrFile.FileExists();
78 bool hasTrailingNewline =
true;
82 wxFFile in( attrFile.GetFullPath(), wxT(
"r" ) );
85 if( !in.IsOpened() || !in.ReadAll( &contents ) )
88 hasTrailingNewline = contents.empty() || contents.EndsWith( wxS(
"\n" ) );
90 wxStringTokenizer tok( contents, wxS(
"\n" ) );
92 while( tok.HasMoreTokens() )
94 wxString line = tok.GetNextToken();
95 line.Trim().Trim(
false );
97 if( line.empty() || line.StartsWith( wxS(
"#" ) ) )
101 wxString pattern = line.BeforeFirst(
' ' );
102 pattern = pattern.BeforeFirst(
'\t' );
104 if( !pattern.empty() )
105 existingPatterns.insert( pattern );
111 for(
const auto& [glob, attr] : kicadEntries )
113 if( existingPatterns.find( glob ) == existingPatterns.end() )
114 missing += glob + wxS(
" " ) + attr + wxS(
"\n" );
117 if( missing.empty() )
120 wxFFile out( attrFile.GetFullPath(), wxT(
"a" ) );
122 if( !out.IsOpened() )
126 out.Write( wxS(
"# KiCad design files use custom merge drivers.\n" ) );
127 else if( !hasTrailingNewline )
128 out.Write( wxS(
"\n" ) );
130 out.Write( missing );
148 git_config_entry* entry =
nullptr;
149 int result = git_config_get_entry( &entry, aConfig, aKey );
152 if(
result == 0 && entry )
154 wxLogTrace(
traceGit,
"Config key '%s' already set; leaving alone", aKey );
158 std::string val( aValue.ToUTF8() );
160 if( git_config_set_string( aConfig, aKey, val.c_str() ) != 0 )
162 wxLogTrace(
traceGit,
"git_config_set_string('%s') failed: %s",
180 if( cliPath.IsEmpty() )
183 "kicad-cli not found next to running binary; skipping merge driver "
184 "config (libgit2 in-process driver still works)." );
190 if( git_repository_open( &repo, aProjectPath.ToUTF8() ) != 0 )
192 wxLogTrace(
traceGit,
"git_repository_open failed: %s",
199 git_config*
config =
nullptr;
201 if( git_repository_config( &
config, repo ) != 0 )
203 wxLogTrace(
traceGit,
"git_repository_config failed: %s",
223 const char* driverKey;
224 const char* humanName;
225 const char* cliCommand;
228 static const DRIVER_ENTRY drivers[] = {
229 {
"merge.kicad-pcb.name",
"merge.kicad-pcb.driver",
"KiCad PCB merge driver",
230 "git-mergedriver --kind pcb \"%O\" \"%A\" \"%B\" --output \"%A\"" },
231 {
"merge.kicad-sch.name",
"merge.kicad-sch.driver",
"KiCad schematic merge driver",
232 "git-mergedriver --kind sch \"%O\" \"%A\" \"%B\" --output \"%A\"" },
233 {
"merge.kicad-sym-lib.name",
"merge.kicad-sym-lib.driver",
234 "KiCad symbol library merge driver",
235 "git-mergedriver --kind sym \"%O\" \"%A\" \"%B\" --output \"%A\"" },
236 {
"merge.kicad-fp.name",
"merge.kicad-fp.driver",
"KiCad footprint merge driver",
237 "git-mergedriver --kind fp \"%O\" \"%A\" \"%B\" --output \"%A\"" }
240 for(
const DRIVER_ENTRY& d : drivers )
244 wxString cmd = wxT(
"\"" ) + cliPath + wxT(
"\" " ) + wxString::FromUTF8( d.cliCommand );
251 wxString
mergetoolCmd = wxT(
"\"" ) + cliPath + wxT(
"\" mergetool "
252 "\"$BASE\" \"$LOCAL\" \"$REMOTE\" "
253 "--output \"$MERGED\"" );
virtual InitResult InitializeRepository(GIT_INIT_HANDLER *aHandler, const wxString &aPath)=0
virtual bool IsRepository(GIT_INIT_HANDLER *aHandler, const wxString &aPath)=0
virtual bool SetupRemote(GIT_INIT_HANDLER *aHandler, const RemoteConfig &aConfig)=0
bool IsRepository(const wxString &aPath)
Check if a directory is already a git repository.
virtual ~GIT_INIT_HANDLER()
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.
static const std::vector< wxString > & TransientGitIgnorePatterns()
Shared patterns for new .gitignore files and the untracked-file filter.
The project's git session.
static wxString GetLastGitError()
libgit2's last message, without credentials from any URL it quotes, so it is safe to show or trace.
KIGIT_REPO_MIXIN(KIGIT_COMMON *aCommon)
static wxString ResolveSiblingExecutable(const wxString &aBaseName)
Resolve a sibling executable alongside the running process.
GIT_BACKEND * GetGitBackend()
static void ensureProjectGitignore(const wxString &aProjectPath)
static wxString resolveKicadCliPath()
static void ensureProjectGitattributes(const wxString &aProjectPath)
void ApplyKicadGitConventions(const wxString &aProjectPath)
Apply KiCad's standard repo conventions to a project directory:
static void ensureMergeDriverConfig(const wxString &aProjectPath)
static bool setConfigIfMissing(git_config *aConfig, const char *aKey, const wxString &aValue)
APIEXPORT void ApplyKicadGitConventions(const wxString &aProjectPath)
Apply KiCad's standard repo conventions to a project directory:
const wxChar *const traceGit
Flag to enable Git debugging output.
static CLI::MERGETOOL_COMMAND mergetoolCmd
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
APIEXPORT bool AddIgnoreRules(const wxFileName &aIgnoreFile, const std::vector< wxString > &aRules, const wxString &aHeader, wxString &aError)
Append rules to an ignore file, creating it with aHeader as a comment when it is not there.
APIEXPORT wxFileName ProjectIgnoreFile(const wxString &aProjectPath)
The ignore file that covers a project.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
std::unique_ptr< git_config_entry, decltype([](git_config_entry *aEntry) { git_config_entry_free(aEntry); })> GitConfigEntryPtr
A unique pointer for git_config_entry objects with automatic cleanup.
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.