Skip to content

Commit

Permalink
code cleanup (mainly compiler & attributes)
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Dec 3, 2024
1 parent 10e376b commit 9b8a063
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 96 deletions.
19 changes: 10 additions & 9 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string>& st
using namespace ftxui;

// set all branches as visible and define a branch color
for (const auto& branch : ostreeRepo.getBranches()) {
for (const auto& branch : ostreeRepo.GetBranches()) {
// if startupBranches are defined, set all as non-visible
visibleBranches[branch] = startupBranches.size() == 0 ? true : false;
std::hash<std::string> nameHash{};
Expand Down Expand Up @@ -88,7 +88,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string>& st
return text(" no commit info available ") | color(Color::RedLight) | bold | center;
}
return CommitInfoManager::renderInfoView(
ostreeRepo.getCommitList().at(visibleCommitViewMap.at(selectedCommit)));
ostreeRepo.GetCommitList().at(visibleCommitViewMap.at(selectedCommit)));
});

// filter
Expand Down Expand Up @@ -121,7 +121,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string>& st
if (event == Event::AltD) {
std::string hashToDrop = visibleCommitViewMap.at(selectedCommit);
SetViewMode(ViewMode::COMMIT_DROP, hashToDrop);
SetModeBranch(GetOstreeRepo().getCommitList().at(hashToDrop).branch);
SetModeBranch(GetOstreeRepo().GetCommitList().at(hashToDrop).branch);
}
// copy commit id
if (event == Event::AltC) {
Expand Down Expand Up @@ -186,7 +186,7 @@ void OSTreeTUI::RefreshCommitComponents() {

commitComponents.clear();
commitComponents.push_back(TrashBin::TrashBinComponent(*this));
int i{0};
size_t i{0};
parseVisibleCommitMap();
for (auto& hash : visibleCommitViewMap) {
commitComponents.push_back(CommitRender::CommitComponent(i, hash, *this));
Expand All @@ -211,7 +211,7 @@ void OSTreeTUI::RefreshCommitListComponent() {
}

bool OSTreeTUI::RefreshOSTreeRepository() {
ostreeRepo.updateData();
ostreeRepo.UpdateData();
RefreshCommitListComponent();
return true;
}
Expand Down Expand Up @@ -284,23 +284,24 @@ bool OSTreeTUI::RemoveCommit(const cpplibostree::Commit& commit) {
void OSTreeTUI::parseVisibleCommitMap() {
// get filtered commits
visibleCommitViewMap = {};
for (const auto& commitPair : ostreeRepo.getCommitList()) {
for (const auto& commitPair : ostreeRepo.GetCommitList()) {
if (visibleBranches[commitPair.second.branch]) {
visibleCommitViewMap.push_back(commitPair.first);
}
}
// sort by date
std::sort(visibleCommitViewMap.begin(), visibleCommitViewMap.end(),
[&](const std::string& a, const std::string& b) {
return ostreeRepo.getCommitList().at(a).timestamp >
ostreeRepo.getCommitList().at(b).timestamp;
return ostreeRepo.GetCommitList().at(a).timestamp >
ostreeRepo.GetCommitList().at(b).timestamp;
});
}

void OSTreeTUI::adjustScrollToSelectedCommit() {
// try to scroll it to the middle
int windowHeight = screen.dimy() - 4;
int scollOffsetToFitCommitToTop = -selectedCommit * CommitRender::COMMIT_WINDOW_HEIGHT;
int scollOffsetToFitCommitToTop =
-static_cast<int>(selectedCommit) * CommitRender::COMMIT_WINDOW_HEIGHT;
int newScroll =
scollOffsetToFitCommitToTop + windowHeight / 2 - CommitRender::COMMIT_WINDOW_HEIGHT;
// adjust if on edges
Expand Down
32 changes: 15 additions & 17 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ Element DefaultRenderState(const WindowRenderState& state,
/// https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
class CommitComponentImpl : public ComponentBase, public WindowOptions {
public:
explicit CommitComponentImpl(int position, std::string commit, OSTreeTUI& ostreetui)
: commitPosition(position),
explicit CommitComponentImpl(size_t position, std::string commit, OSTreeTUI& ostreetui)
: drag_initial_x(1),
drag_initial_y(static_cast<int>(position) * COMMIT_WINDOW_HEIGHT),
commitPosition(position),
hash(std::move(commit)),
ostreetui(ostreetui),
commit(ostreetui.GetOstreeRepo().getCommitList().at(hash)),
newVersion(this->commit.version),
drag_initial_y(position * COMMIT_WINDOW_HEIGHT),
drag_initial_x(1) {
commit(ostreetui.GetOstreeRepo().GetCommitList().at(hash)),
newVersion(this->commit.version) {
inner = Renderer([&] {
return vbox({
text(ostreetui.GetOstreeRepo().getCommitList().at(hash).subject),
text(ostreetui.GetOstreeRepo().GetCommitList().at(hash).subject),
text(
std::format("{:%Y-%m-%d %T %Ez}",
std::chrono::time_point_cast<std::chrono::seconds>(
ostreetui.GetOstreeRepo().getCommitList().at(hash).timestamp))),
ostreetui.GetOstreeRepo().GetCommitList().at(hash).timestamp))),
});
});
simpleCommit = inner;
Expand Down Expand Up @@ -160,7 +160,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {

void executeDeletion() {
// delete on the ostree repo
ostreetui.RemoveCommit(ostreetui.GetOstreeRepo().getCommitList().at(hash));
ostreetui.RemoveCommit(ostreetui.GetOstreeRepo().GetCommitList().at(hash));
resetWindow();
}

Expand All @@ -183,12 +183,10 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
}
} else if (ostreetui.GetViewMode() == ViewMode::COMMIT_DROP &&
ostreetui.GetModeHash() == hash) {
const auto& commitList = ostreetui.GetOstreeRepo().getCommitList();
auto commit = commitList.at(hash);
startDeletionWindow(ostreetui.GetOstreeRepo().IsMostRecentCommitOnBranch(hash));
}

auto element = ComponentBase::Render();
ftxui::Element element = ComponentBase::Render();

const WindowRenderState state = {element, title(), Active(), drag_};

Expand Down Expand Up @@ -269,7 +267,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
if (event.mouse().y > ostreetui.GetScreen().dimy() - 8) {
ostreetui.SetViewMode(ViewMode::COMMIT_DROP, hash);
ostreetui.SetModeBranch(
ostreetui.GetOstreeRepo().getCommitList().at(hash).branch);
ostreetui.GetOstreeRepo().GetCommitList().at(hash).branch);
top() = drag_initial_y;
}
// check if position matches branch & do something if it does
Expand Down Expand Up @@ -371,7 +369,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
bool drag_ = false;

// ostree-tui specific members
int commitPosition;
size_t commitPosition;
std::string hash;
OSTreeTUI& ostreetui;

Expand Down Expand Up @@ -423,7 +421,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
// deletion view, if commit is not the most recent on its branch
Component deletionViewBody = Container::Vertical(
{Renderer([&] {
std::string parent = ostreetui.GetOstreeRepo().getCommitList().at(hash).parent;
std::string parent = ostreetui.GetOstreeRepo().GetCommitList().at(hash).parent;
return vbox({text(" Remove Commit (and preceding)...") | bold, text(""),
text("" + ostreetui.GetModeBranch()) | dim, text("") | dim,
hbox({
Expand All @@ -445,7 +443,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {

} // namespace

ftxui::Component CommitComponent(int position, const std::string& commit, OSTreeTUI& ostreetui) {
ftxui::Component CommitComponent(size_t position, const std::string& commit, OSTreeTUI& ostreetui) {
return ftxui::Make<CommitComponentImpl>(position, commit, ostreetui);
}

Expand Down Expand Up @@ -475,7 +473,7 @@ ftxui::Element commitRender(OSTreeTUI& ostreetui,
ostreetui.GetColumnToBranchMap().clear();
for (const auto& visibleCommitIndex : ostreetui.GetVisibleCommitViewMap()) {
const cpplibostree::Commit commit =
ostreetui.GetOstreeRepo().getCommitList().at(visibleCommitIndex);
ostreetui.GetOstreeRepo().GetCommitList().at(visibleCommitIndex);
// branch head if it is first branch usage
const std::string relevantBranch = commit.branch;
if (usedBranches.at(relevantBranch) == -1) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ constexpr int COMMIT_WINDOW_HEIGHT{4};
constexpr int COMMIT_WINDOW_WIDTH{32};
constexpr int PROMOTION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 11};
constexpr int PROMOTION_WINDOW_WIDTH{COMMIT_WINDOW_WIDTH + 8};
constexpr int DELETION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 9};
constexpr int DELETION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 8};
constexpr int DELETION_WINDOW_WIDTH{COMMIT_WINDOW_WIDTH + 8};
// render tree types
enum RenderTree : uint8_t {
Expand All @@ -51,7 +51,7 @@ enum RenderTree : uint8_t {
*
* @return UI Component
*/
[[nodiscard]] ftxui::Component CommitComponent(int position,
[[nodiscard]] ftxui::Component CommitComponent(size_t position,
const std::string& commit,
OSTreeTUI& ostreetui);

Expand Down
6 changes: 4 additions & 2 deletions src/core/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ BranchBoxManager::BranchBoxManager(OSTreeTUI& ostreetui,
std::unordered_map<std::string, bool>& visibleBranches) {
using namespace ftxui;

CheckboxOption cboption = {.on_change = [&] { ostreetui.RefreshCommitListComponent(); }};
CheckboxOption cboption = CheckboxOption::Simple();
cboption.on_change = [&] { ostreetui.RefreshCommitListComponent(); };
// CheckboxOption cboption = {.on_change = [&] { ostreetui.RefreshCommitListComponent(); }};

// branch visibility
for (const auto& branch : repo.getBranches()) {
for (const auto& branch : repo.GetBranches()) {
branchBoxes->Add(Checkbox(branch, &(visibleBranches.at(branch)), cboption));
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/trashBin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ class TrashBinComponentImpl : public ComponentBase, public WindowOptions {

Element Render() final {
// check if in deletion or stuff
if (ostreetui.GetViewMode() == ViewMode::COMMIT_DRAGGING &&
ostreetui.GetOstreeRepo().IsMostRecentCommitOnBranch(ostreetui.GetModeHash())) {
if (ostreetui.GetViewMode() == ViewMode::COMMIT_DRAGGING) {
showBin();
} else {
hideBin();
Expand All @@ -88,7 +87,7 @@ class TrashBinComponentImpl : public ComponentBase, public WindowOptions {
return element;
}

bool OnEvent(Event event) final { return false; }
bool OnEvent(Event /*event*/) final { return false; }

private:
OSTreeTUI& ostreetui;
Expand Down
45 changes: 12 additions & 33 deletions src/util/cpplibostree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
namespace cpplibostree {

OSTreeRepo::OSTreeRepo(std::string path) : repoPath(std::move(path)), commitList({}), branches({}) {
updateData();
UpdateData();
}

bool OSTreeRepo::updateData() {
bool OSTreeRepo::UpdateData() {
// parse branches
std::string branchString = getBranchesAsString();
std::stringstream bss(branchString);
Expand Down Expand Up @@ -51,19 +51,19 @@ OstreeRepo* OSTreeRepo::_c() {
return repo;
}

const std::string& OSTreeRepo::getRepoPath() const {
const std::string& OSTreeRepo::GetRepoPath() const {
return repoPath;
}

const CommitList& OSTreeRepo::getCommitList() const {
const CommitList& OSTreeRepo::GetCommitList() const {
return commitList;
}

const std::vector<std::string>& OSTreeRepo::getBranches() const {
const std::vector<std::string>& OSTreeRepo::GetBranches() const {
return branches;
}

bool OSTreeRepo::isCommitSigned(const Commit& commit) {
bool OSTreeRepo::IsCommitSigned(const Commit& commit) {
return commit.signatures.size() > 0;
}

Expand Down Expand Up @@ -347,7 +347,9 @@ bool OSTreeRepo::RemoveCommitFromBranchAndPrune(const Commit& commit) {
command += " " + commit.branch;
command += " " + commit.branch + "^";

return runCLICommand(command);
if (!runCLICommand(command)) {
return false;
};
}

// prune commit
Expand All @@ -358,35 +360,12 @@ bool OSTreeRepo::RemoveCommitFromBranchAndPrune(const Commit& commit) {
return runCLICommand(command2);
}

/// TODO This implementation should not rely on the ostree CLI -> change to libostree usage.
bool OSTreeRepo::ResetBranchHeadAndPrune(const Commit& commit) {
// check if it is last commit on branch
if (!IsMostRecentCommitOnBranch(commit)) {
return false;
}

// reset head
std::string command = "ostree reset";
command += " --repo=" + repoPath;
command += " " + commit.branch;
command += " " + commit.branch + "^";
if (!runCLICommand(command)) {
return false;
}

// remove orphaned commit
std::string command2 = "ostree prune";
command2 += " --repo=" + repoPath;
command2 += " --delete-commit=" + commit.hash;
if (!runCLICommand(command2)) {
return false;
}

return true;
bool OSTreeRepo::ResetBranchHeadAndPrune(const std::string& branch) {
return RemoveCommitFromBranchAndPrune(GetMostRecentCommitOfBranch(branch));
}

bool OSTreeRepo::runCLICommand(const std::string& command) {
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
std::unique_ptr<FILE, int (*)(FILE*)> pipe(popen(command.c_str(), "r"), &pclose);
if (!pipe) {
return false;
}
Expand Down
Loading

0 comments on commit 9b8a063

Please sign in to comment.