Git plugin fixes.

This commit is contained in:
Martín Lucas Golini
2024-01-30 00:01:07 -03:00
parent 9a0057c890
commit f2324d8f40
6 changed files with 61 additions and 32 deletions

View File

@@ -75,6 +75,7 @@ int Git::git( const std::string& args, const std::string& projectDir, std::strin
Log::instance()->writef( mLogLevel, "GitPlugin cmd in %s (%d): %s %s",
clock.getElapsedTime().toString(), retCode, mGitPath, args );
}
Log::debug( "%s", buf );
return retCode;
}
@@ -491,8 +492,10 @@ bool Git::hasSubmodules( const std::string& projectDir ) {
( !mProjectPath.empty() && FileSystem::fileExists( mProjectPath + ".gitmodules" ) );
}
std::string Git::repoName( const std::string& file, bool allowExactMatch,
const std::string& projectDir ) {
std::string Git::repoName( std::string file, bool allowExactMatch, const std::string& projectDir ) {
if ( String::startsWith( file, !projectDir.empty() ? projectDir : mProjectPath ) )
FileSystem::filePathRemoveBasePath( !projectDir.empty() ? projectDir : mProjectPath, file );
Lock l( mSubModulesMutex );
for ( const auto& subRepo : mSubModules ) {
if ( String::startsWith( file, subRepo ) &&
( allowExactMatch || file.size() != subRepo.size() ) )
@@ -502,6 +505,7 @@ std::string Git::repoName( const std::string& file, bool allowExactMatch,
}
std::string Git::repoPath( const std::string& file ) {
Lock l( mSubModulesMutex );
for ( const auto& subRepo : mSubModules ) {
if ( String::startsWith( file, subRepo ) && file.size() != subRepo.size() )
return mProjectPath + subRepo;
@@ -531,7 +535,20 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )
LuaPattern subModulePattern( "^Entering '(.*)'" );
bool modifiedSubmodule = false;
auto parseStatus = [&s, &buf, &modifiedSubmodule, &projectDir, this, &subModulePattern]() {
std::vector<std::string> curSubModules;
{
Lock l( mSubModulesMutex );
curSubModules = mSubModules;
}
const auto isSubmodule = [&curSubModules]( const std::string_view& file ) -> bool {
return std::any_of( curSubModules.begin(), curSubModules.end(),
[&file]( const auto& submodule ) { return submodule == file; } );
};
auto parseStatus = [&s, &buf, &modifiedSubmodule, &projectDir, this, &subModulePattern,
submodules, &isSubmodule]() {
std::string subModulePath = "";
LuaPattern pattern( "^([mMARTUD?%s][mMARTUD?%s])%s(.*)" );
size_t changesCount = countLines( buf );
@@ -561,6 +578,9 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )
return;
}
if ( submodules && !mSubModules.empty() && isSubmodule( file ) )
modifiedSubmodule = true;
bool isStagedAndModified =
status.type == GitStatusType::Staged && statusStr[1] != ' ';
@@ -578,7 +598,7 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )
}
}
if ( !found ) {
s.files[repo].push_back( { std::move( filePath ), 0, 0, status } );
s.files[repo].push_back( { filePath, 0, 0, status } );
if ( isStagedAndModified ) {
status.type = GitStatusType::Changed;

View File

@@ -318,7 +318,7 @@ class Git {
std::vector<std::string> getSubModules( const std::string& projectDir = "" );
std::string repoName( const std::string& file, bool allowExactMatch = false,
std::string repoName( std::string file, bool allowExactMatch = false,
const std::string& projectDir = "" );
std::string repoPath( const std::string& file );

View File

@@ -520,6 +520,8 @@ void GitPlugin::blame( UICodeEditor* editor ) {
} );
}
// Branch operations
void GitPlugin::checkout( Git::Branch branch ) {
if ( !mGit )
return;
@@ -563,7 +565,6 @@ void GitPlugin::checkout( Git::Branch branch ) {
checkOutFn( false );
}
// Branch operations
void GitPlugin::branchRename( Git::Branch branch ) {
UIMessageBox* msgBox = UIMessageBox::New(
UIMessageBox::INPUT,
@@ -698,7 +699,7 @@ void GitPlugin::commit( const std::string& repoPath ) {
auto branchName = mGitBranches[repoPath];
if ( !branchName.empty() ) {
if ( repoPath != repoSelected() || !mBranchesTree->getModel() ) {
auto branch = mGit->getAllBranchesAndTags( Git::RefType::All,
auto branch = mGit->getAllBranchesAndTags( Git::RefType::Head,
"refs/heads/" + branchName, repoPath );
if ( !branch.empty() )
chkAmmend->setEnabled( branch.front().ahead > 0 );
@@ -731,7 +732,7 @@ void GitPlugin::commit( const std::string& repoPath ) {
mLastCommitMsg = msgBox->getTextEdit()->getText();
return res;
},
true, true, true, true );
true, true, true, true, true );
} );
msgBox->on( Event::OnCancel, [this, msgBox]( const Event* ) {
@@ -759,6 +760,7 @@ void GitPlugin::fastForwardMerge( Git::Branch branch ) {
},
false, true );
}
// Branch operations
// File operations
@@ -777,7 +779,7 @@ std::string GitPlugin::fixFilePath( const std::string& file ) {
if ( !isPath( file ) ) {
path = ( mProjectPath + file );
}
return file;
return path;
}
std::vector<std::string> GitPlugin::fixFilePaths( const std::vector<std::string>& files ) {
@@ -820,8 +822,9 @@ void GitPlugin::discard( const std::string& file ) {
file ) );
msgBox->on( Event::OnConfirm, [this, file]( auto ) {
runAsync( [this, file]() { return mGit->restore( file, mGit->repoPath( file ) ); }, true,
false );
runAsync(
[this, file]() { return mGit->restore( fixFilePath( file ), mGit->repoPath( file ) ); },
true, false );
} );
msgBox->setCloseShortcut( { KEY_ESCAPE, KEYMOD_NONE } );
msgBox->setTitle( i18n( "git_confirm", "Confirm" ) );
@@ -1528,26 +1531,29 @@ void GitPlugin::openFileStatusMenu( const Git::DiffFile& file ) {
}
void GitPlugin::runAsync( std::function<Git::Result()> fn, bool _updateStatus, bool _updateBranches,
bool displaySuccessMsg, bool updateBranchesOnError ) {
bool displaySuccessMsg, bool updateBranchesOnError,
bool updateStatusOnError ) {
if ( !mGit )
return;
mLoader->setVisible( true );
mThreadPool->run(
[this, fn, _updateStatus, _updateBranches, displaySuccessMsg, updateBranchesOnError] {
auto res = fn();
mLoader->runOnMainThread( [this] { mLoader->setVisible( false ); } );
if ( res.fail() || displaySuccessMsg ) {
showMessage( LSPMessageType::Warning, res.result );
if ( _updateBranches && updateBranchesOnError )
updateBranches();
return;
}
if ( _updateBranches )
mThreadPool->run( [this, fn, _updateStatus, _updateBranches, displaySuccessMsg,
updateBranchesOnError, updateStatusOnError] {
auto res = fn();
mLoader->runOnMainThread( [this] { mLoader->setVisible( false ); } );
if ( res.fail() || displaySuccessMsg ) {
showMessage( LSPMessageType::Warning, res.result );
if ( _updateBranches && updateBranchesOnError )
updateBranches();
if ( _updateStatus )
if ( _updateStatus && updateStatusOnError )
updateStatus( true );
} );
return;
}
if ( _updateBranches )
updateBranches();
if ( _updateStatus )
updateStatus( true );
} );
}
void GitPlugin::addMenuItem( UIMenu* menu, const std::string& txtKey, const std::string& txtVal,

View File

@@ -200,7 +200,8 @@ class GitPlugin : public PluginBase {
void stashDrop( const Git::Branch& branch );
void runAsync( std::function<Git::Result()> fn, bool updateStatus, bool updateBranches,
bool displaySuccessMsg = false, bool updateBranchesOnError = false );
bool displaySuccessMsg = false, bool updateBranchesOnError = false,
bool updateStatusOnError = false );
void addMenuItem( UIMenu* menu, const std::string& txtKey, const std::string& txtVal,
const std::string& icon = "",

View File

@@ -178,7 +178,9 @@ void PluginBase::onUnregister( UICodeEditor* editor ) {
for ( auto editorIt : mEditorDocs )
if ( editorIt.second == doc )
return;
onUnregisterDocument( doc );
mDocs.erase( doc );
}