Skip to content

Commit

Permalink
More git plugin refactor and some fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Jan 25, 2024
1 parent 3893d13 commit 71089a5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 81 deletions.
1 change: 1 addition & 0 deletions src/tools/ecode/notificationcenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void NotificationCenter::addNotification( const String& text, const Time& delay
{ Actions::FadeIn::New( Seconds( 0.125 ) ), Actions::Delay::New( delay ),
Actions::FadeOut::New( Seconds( 0.125 ) ), Actions::Close::New() } );
tv->runAction( sequence );
Log::info( "Displayed notification:\n%s", text.toUtf8() );
};

if ( Engine::isRunninMainThread() )
Expand Down
46 changes: 26 additions & 20 deletions src/tools/ecode/plugins/git/git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ bool Git::isGitRepo( const std::string& projectDir ) {

std::string Git::branch( const std::string& projectDir ) {
std::string buf;

if ( EXIT_SUCCESS == git( "symbolic-ref --short HEAD", projectDir, buf ) )
return String::rTrim( buf, '\n' );

if ( EXIT_SUCCESS == git( "rev-parse --abbrev-ref HEAD", projectDir, buf ) )
return String::rTrim( buf, '\n' );

return "HEAD";
}

std::unordered_map<std::string, std::string>
Git::branches( const std::vector<std::string>& repos ) {
std::unordered_map<std::string, std::string> ret;
for ( const auto& repo : repos )
ret[repo] = branch( repo );
return ret;
}

bool Git::setProjectPath( const std::string& projectPath ) {
auto lastProjectPath = mProjectPath;
mProjectPath = "";
Expand Down Expand Up @@ -495,6 +508,8 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )
auto filePath = subModulePath + file;
auto repo = repoName( filePath, projectDir );
auto repoIt = s.files.find( repo );
GitStatusReport status = { GitStatus::NotSet, GitStatusType::Untracked,
GitStatusChar::Untracked };
if ( repoIt != s.files.end() ) {
bool found = false;
for ( auto& fileIt : repoIt->second ) {
Expand All @@ -506,15 +521,12 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )
}
}
if ( !found ) {
s.files[repo].push_back( { std::move( filePath ), inserts, deletes,
GitStatus::NotSet, GitStatusType::Untracked,
GitStatusChar::Untracked } );
s.files[repo].push_back(
{ std::move( filePath ), inserts, deletes, status } );
}
} else {
s.files.insert(
{ repo,
{ { std::move( filePath ), inserts, deletes, GitStatus::NotSet,
GitStatusType::Untracked, GitStatusChar::Untracked } } } );
{ repo, { { std::move( filePath ), inserts, deletes, status } } } );
}
s.totalInserts += inserts;
s.totalDeletions += deletes;
Expand Down Expand Up @@ -559,10 +571,10 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )

auto status = statusFromShortStatusStr( statusStr );

if ( status.gitStatus != GitStatus::NotSet )
if ( status.status == GitStatus::NotSet )
return;

if ( status.gitStatusChar == GitStatusChar::ModifiedSubmodule ) {
if ( status.symbol == GitStatusChar::ModifiedSubmodule ) {
modifiedSubmodule = true;
return;
}
Expand All @@ -574,21 +586,15 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )
bool found = false;
for ( auto& fileIt : repoIt->second ) {
if ( fileIt.file == filePath ) {
fileIt.statusChar = status.gitStatusChar;
fileIt.status = status.gitStatus;
fileIt.statusType = status.gitStatusType;
fileIt.report = status;
found = true;
break;
}
}
if ( !found ) {
s.files[repo].push_back( { std::move( filePath ), 0, 0, status.gitStatus,
status.gitStatusType, status.gitStatusChar } );
}
if ( !found )
s.files[repo].push_back( { std::move( filePath ), 0, 0, status } );
} else {
s.files.insert( { repo,
{ { std::move( filePath ), 0, 0, status.gitStatus,
status.gitStatusType, status.gitStatusChar } } } );
s.files.insert( { repo, { { std::move( filePath ), 0, 0, status } } } );
}
}
} );
Expand All @@ -604,7 +610,7 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir )

for ( auto& [_, repo] : s.files ) {
for ( auto& val : repo ) {
if ( val.statusChar == GitStatusChar::Added && val.inserts == 0 ) {
if ( val.report.symbol == GitStatusChar::Added && val.inserts == 0 ) {
std::string fileText;
FileSystem::fileGet( ( projectDir.empty() ? mProjectPath : projectDir ) + val.file,
fileText );
Expand Down Expand Up @@ -813,7 +819,7 @@ Git::GitStatusReport Git::statusFromShortStatusStr( const std::string_view& stat
}
}

return { gitStatus, gitStatusChar, gitStatusType };
return { gitStatus, gitStatusType, gitStatusChar };
}

} // namespace ecode
28 changes: 16 additions & 12 deletions src/tools/ecode/plugins/git/git.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,25 @@ class Git {
SMM = git_xy( 'm', ANY ), // staged modified submodule
};

struct GitStatusReport {
GitStatus status = GitStatus::NotSet;
GitStatusType type = GitStatusType::Untracked;
GitStatusChar symbol = GitStatusChar::Unknown;

bool operator==( const GitStatusReport& other ) const {
return status == other.status && symbol == other.symbol && type == other.type;
}
};

struct DiffFile {
std::string file;
int inserts{ 0 };
int deletes{ 0 };
GitStatus status;
GitStatusType statusType;
GitStatusChar statusChar{ GitStatusChar::Unknown };
GitStatusReport report;

bool operator==( const DiffFile& other ) const {
return file == other.file && inserts == other.inserts && deletes == other.deletes &&
status == other.status && statusType == other.statusType &&
statusChar == other.statusChar;
report == other.report;
}
};

Expand Down Expand Up @@ -199,6 +206,8 @@ class Git {

std::string branch( const std::string& projectDir = "" );

std::unordered_map<std::string, std::string> branches( const std::vector<std::string>& repos );

Status status( bool recurseSubmodules, const std::string& projectDir = "" );

Result add( std::vector<std::string> files, const std::string& projectDir = "" );
Expand Down Expand Up @@ -279,19 +288,14 @@ class Git {

Result gitSimple( const std::string& cmd, const std::string& projectDir );

GitStatusReport statusFromShortStatusStr( const std::string_view& statusStr );

protected:
std::string mGitPath;
std::string mProjectPath;
std::string mGitFolder;
std::vector<std::string> mSubModules;
bool mSubModulesUpdated{ false };

struct GitStatusReport {
GitStatus gitStatus = GitStatus::NotSet;
GitStatusChar gitStatusChar = GitStatusChar::Unknown;
GitStatusType gitStatusType = GitStatusType::Untracked;
};
GitStatusReport statusFromShortStatusStr( const std::string_view& statusStr );
};

} // namespace ecode
Loading

0 comments on commit 71089a5

Please sign in to comment.