31#include <wx/tokenzr.h>
32#include <wx/translation.h>
39wxString escapePattern(
const wxString& aText )
43 for(
const wxUniChar& ch : aText )
45 if( ch ==
'*' || ch ==
'?' || ch ==
'[' || ch ==
'\\' )
55bool readIgnoreFile(
const wxFileName& aIgnoreFile, wxString& aContents )
58 if( !aIgnoreFile.FileExists() )
61 wxFFile in( aIgnoreFile.GetFullPath(), wxT(
"r" ) );
63 return in.IsOpened() && in.ReadAll( &aContents );
67bool fileError( wxString& aError,
const wxString& aMessage,
const wxFileName& aFile )
69 aError = wxString::Format( aMessage, aFile.GetFullPath() );
74std::vector<wxString> splitIgnoreRules(
const wxString& aContents )
76 std::vector<wxString> rules;
77 wxStringTokenizer tokenizer( aContents, wxS(
"\n" ) );
79 while( tokenizer.HasMoreTokens() )
81 wxString line = tokenizer.GetNextToken();
82 line.Trim().Trim(
false );
84 if( !line.IsEmpty() && !line.StartsWith( wxS(
"#" ) ) )
85 rules.push_back( line );
96 return wxFileName( aProjectPath, wxS(
".gitignore" ) );
102 if( aProjectRelPath.IsEmpty() )
103 return wxEmptyString;
106 wxString rule = wxS(
"/" ) + escapePattern( aProjectRelPath );
117 const wxString ext = wxFileName( aProjectRelPath, wxPATH_UNIX ).GetExt();
120 return wxEmptyString;
122 return wxS(
"*." ) + escapePattern( ext );
130 if( !readIgnoreFile( aIgnoreFile, contents ) )
133 return splitIgnoreRules( contents );
137bool KIGIT::AddIgnoreRules(
const wxFileName& aIgnoreFile,
const std::vector<wxString>& aRules,
const wxString& aHeader,
140 const bool exists = aIgnoreFile.FileExists();
143 if( exists && !readIgnoreFile( aIgnoreFile, contents ) )
144 return fileError( aError,
_(
"Could not read '%s'." ), aIgnoreFile );
146 std::vector<wxString> present = splitIgnoreRules( contents );
149 for(
const wxString& rule : aRules )
155 present.push_back( rule );
156 missing += rule + wxS(
"\n" );
159 if( missing.IsEmpty() )
164 if( !aHeader.IsEmpty() )
165 missing.Prepend( wxS(
"# " ) + aHeader + wxS(
"\n" ) );
167 else if( !contents.IsEmpty() && !contents.EndsWith( wxS(
"\n" ) ) )
170 missing.Prepend( wxS(
"\n" ) );
173 wxFFile out( aIgnoreFile.GetFullPath(), wxT(
"a" ) );
175 if( !out.IsOpened() || !out.Write( missing ) )
176 return fileError( aError,
_(
"Could not write to '%s'." ), aIgnoreFile );
186 if( !aIgnoreFile.FileExists() )
189 if( !readIgnoreFile( aIgnoreFile, contents ) )
190 return fileError( aError,
_(
"Could not read '%s'." ), aIgnoreFile );
193 bool removed =
false;
195 wxStringTokenizer tokenizer( contents, wxS(
"\n" ), wxTOKEN_RET_EMPTY );
197 while( tokenizer.HasMoreTokens() )
199 wxString line = tokenizer.GetNextToken();
200 wxString rule = line;
201 rule.Trim().Trim(
false );
209 kept += line + wxS(
"\n" );
216 wxTempFile out( aIgnoreFile.GetFullPath() );
218 if( !out.IsOpened() || !out.Write( kept ) || !out.Commit() )
219 return fileError( aError,
_(
"Could not write to '%s'." ), aIgnoreFile );
231 if( !repo || !prefix || aProjectRelPath.IsEmpty() )
234 const wxString repoRelative = *prefix + aProjectRelPath;
235 const std::string repoRelPath = repoRelative.utf8_string();
239 const std::string ignorePath = aIsDirectory ? repoRelPath +
"/" : repoRelPath;
241 if( git_ignore_path_is_ignored( &ignored, repo, ignorePath.c_str() ) == GIT_OK )
244 git_index*
index =
nullptr;
246 if( git_repository_index( &
index, repo ) != GIT_OK )
254 const std::string indexPrefix = repoRelPath +
"/";
257 state.
tracked = git_index_find_prefix( &position,
index, indexPrefix.c_str() ) == GIT_OK;
262 state.
tracked = git_index_get_bypath(
index, repoRelPath.c_str(), 0 ) !=
nullptr
The project's git session.
std::optional< wxString > GetProjectPrefix() const
Project folder relative to the repository, with a trailing slash.
git_repository * GetRepo() const
bool IsPathConflicted(const wxString &aPath) const
True while aPath is one of the files a merge or rebase stopped on.
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.
APIEXPORT bool RemoveIgnoreRules(const wxFileName &aIgnoreFile, const std::vector< wxString > &aRules, wxString &aError)
Remove rules from an ignore file.
APIEXPORT wxString ExtensionIgnoreRule(const wxString &aProjectRelPath)
The rule that ignores every file sharing a path's extension, or nothing when it has none.
APIEXPORT IGNORE_STATE IgnoreState(KIGIT_COMMON &aCommon, const wxString &aProjectRelPath, bool aIsDirectory)
What git's rules and its index say about a project path, so the user is offered only what would take ...
APIEXPORT wxString PathIgnoreRule(const wxString &aProjectRelPath, bool aIsDirectory)
The rule that ignores one path and nothing else.
APIEXPORT std::vector< wxString > ReadIgnoreRules(const wxFileName &aIgnoreFile)
The rules an ignore file holds, in order, without its comments and blank lines.
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex); })> GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
What an ignore rule for one path would achieve.
bool tracked
The path, or a file inside it, is in the index, so a new rule would do nothing.
bool ignored
Git's rules already cover the path.