Added Git data models.

Small base Model clean up.
This commit is contained in:
Martín Lucas Golini
2024-01-13 12:56:00 -03:00
parent d5322b7615
commit 697fc29514
16 changed files with 96 additions and 42 deletions

View File

@@ -23,8 +23,8 @@ class Git {
std::size_t line{ 0 };
};
enum class FileStatus {
Unknown,
enum class FileStatus : char {
Unknown = ' ',
Modified = 'M',
Added = 'A',
Renamed = 'R',
@@ -34,6 +34,7 @@ class Git {
Untracked = '?',
ModifiedSubmodule = 'm',
};
struct DiffFile {
std::string file;
int inserts{ 0 };
@@ -45,10 +46,12 @@ class Git {
}
};
using FilesStatus = std::map<std::string, DiffFile>;
struct Status {
int totalInserts{ 0 };
int totalDeletions{ 0 };
std::map<std::string, DiffFile> files;
FilesStatus files;
bool operator==( const Status& other ) const {
return totalInserts == other.totalInserts && totalDeletions == other.totalDeletions &&
@@ -72,6 +75,23 @@ class Git {
All = 0x7,
};
static constexpr const char* HEAD = "head";
static constexpr const char* REMOTE = "remote";
static constexpr const char* TAG = "tag";
static constexpr const char* ALL = "all";
static const char* refTypeToString( RefType type ) {
switch ( type ) {
case Head:
return HEAD;
case Remote:
return REMOTE;
case Tag:
return TAG;
case All:
return ALL;
}
}
struct Branch {
/** Branch name */
std::string name;
@@ -81,6 +101,8 @@ class Git {
RefType type = All;
/** last commit on this branch, may be empty **/
std::string lastCommit;
const char* typeStr() const { return refTypeToString( type ); }
};
Git( const std::string& projectDir = "", const std::string& gitPath = "" );

View File

@@ -4,9 +4,13 @@
#include "../plugin.hpp"
#include "../pluginmanager.hpp"
#include "git.hpp"
#include <eepp/ui/models/model.hpp>
#include <eepp/ui/uilinearlayout.hpp>
#include <optional>
using namespace EE::UI::Models;
using namespace EE::UI;
namespace ecode {
class Git;
@@ -97,6 +101,72 @@ class GitPlugin : public PluginBase {
void updateUINow( bool force = false );
};
class GitBranchModel : public Model {
public:
enum Column { Name, Remote, Type, LastCommit };
GitBranchModel( std::vector<Git::Branch>&& branches ) : mBranches( std::move( branches ) ) {}
size_t rowCount( const ModelIndex& ) const { return mBranches.size(); }
size_t columnCount( const ModelIndex& ) const { return 4; }
Variant data( const ModelIndex& index, ModelRole role ) const {
if ( role == ModelRole::Display && index.row() < mBranches.size() ) {
const Git::Branch& branch = mBranches[index.row()];
switch ( index.column() ) {
case Column::Name:
return branch.name.c_str();
case Column::Remote:
return branch.remote.c_str();
case Column::Type:
return branch.typeStr();
case Column::LastCommit:
return branch.lastCommit.c_str();
}
}
return {};
}
protected:
std::vector<Git::Branch> mBranches;
};
class GitStatusModel : public Model {
public:
enum Column { File, Inserted, Removed, FileStatus };
GitStatusModel( Git::FilesStatus&& status ) {
mStatus.reserve( status.size() );
for ( auto& s : status )
mStatus.emplace_back( std::move( s.second ) );
}
size_t rowCount( const ModelIndex& ) const { return mStatus.size(); }
size_t columnCount( const ModelIndex& ) const { return 4; }
Variant data( const ModelIndex& index, ModelRole role ) const {
if ( role == ModelRole::Display && index.row() < mStatus.size() ) {
const Git::DiffFile& s = mStatus[index.row()];
switch ( index.column() ) {
case Column::File:
return s.file.c_str();
case Column::Inserted:
return s.inserts;
case Column::Removed:
return s.deletes;
case Column::FileStatus:
return Variant( std::string( static_cast<char>( s.status ), 1 ) );
}
}
return {};
}
protected:
std::vector<Git::DiffFile> mStatus;
};
} // namespace ecode
#endif // ECODE_GITPLUGIN_HPP