Skip to content

Commit 7e16c72

Browse files
committed
code cleanup (mainly compiler & attributes)
1 parent d473137 commit 7e16c72

File tree

7 files changed

+75
-96
lines changed

7 files changed

+75
-96
lines changed

src/core/OSTreeTUI.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string>& st
2525
using namespace ftxui;
2626

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

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

187187
commitComponents.clear();
188188
commitComponents.push_back(TrashBin::TrashBinComponent(*this));
189-
int i{0};
189+
size_t i{0};
190190
parseVisibleCommitMap();
191191
for (auto& hash : visibleCommitViewMap) {
192192
commitComponents.push_back(CommitRender::CommitComponent(i, hash, *this));
@@ -211,7 +211,7 @@ void OSTreeTUI::RefreshCommitListComponent() {
211211
}
212212

213213
bool OSTreeTUI::RefreshOSTreeRepository() {
214-
ostreeRepo.updateData();
214+
ostreeRepo.UpdateData();
215215
RefreshCommitListComponent();
216216
return true;
217217
}
@@ -284,23 +284,24 @@ bool OSTreeTUI::RemoveCommit(const cpplibostree::Commit& commit) {
284284
void OSTreeTUI::parseVisibleCommitMap() {
285285
// get filtered commits
286286
visibleCommitViewMap = {};
287-
for (const auto& commitPair : ostreeRepo.getCommitList()) {
287+
for (const auto& commitPair : ostreeRepo.GetCommitList()) {
288288
if (visibleBranches[commitPair.second.branch]) {
289289
visibleCommitViewMap.push_back(commitPair.first);
290290
}
291291
}
292292
// sort by date
293293
std::sort(visibleCommitViewMap.begin(), visibleCommitViewMap.end(),
294294
[&](const std::string& a, const std::string& b) {
295-
return ostreeRepo.getCommitList().at(a).timestamp >
296-
ostreeRepo.getCommitList().at(b).timestamp;
295+
return ostreeRepo.GetCommitList().at(a).timestamp >
296+
ostreeRepo.GetCommitList().at(b).timestamp;
297297
});
298298
}
299299

300300
void OSTreeTUI::adjustScrollToSelectedCommit() {
301301
// try to scroll it to the middle
302302
int windowHeight = screen.dimy() - 4;
303-
int scollOffsetToFitCommitToTop = -selectedCommit * CommitRender::COMMIT_WINDOW_HEIGHT;
303+
int scollOffsetToFitCommitToTop =
304+
-static_cast<int>(selectedCommit) * CommitRender::COMMIT_WINDOW_HEIGHT;
304305
int newScroll =
305306
scollOffsetToFitCommitToTop + windowHeight / 2 - CommitRender::COMMIT_WINDOW_HEIGHT;
306307
// adjust if on edges

src/core/commit.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ Element DefaultRenderState(const WindowRenderState& state,
7575
/// https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
7676
class CommitComponentImpl : public ComponentBase, public WindowOptions {
7777
public:
78-
explicit CommitComponentImpl(int position, std::string commit, OSTreeTUI& ostreetui)
79-
: commitPosition(position),
78+
explicit CommitComponentImpl(size_t position, std::string commit, OSTreeTUI& ostreetui)
79+
: drag_initial_x(1),
80+
drag_initial_y(static_cast<int>(position) * COMMIT_WINDOW_HEIGHT),
81+
commitPosition(position),
8082
hash(std::move(commit)),
8183
ostreetui(ostreetui),
82-
commit(ostreetui.GetOstreeRepo().getCommitList().at(hash)),
83-
newVersion(this->commit.version),
84-
drag_initial_y(position * COMMIT_WINDOW_HEIGHT),
85-
drag_initial_x(1) {
84+
commit(ostreetui.GetOstreeRepo().GetCommitList().at(hash)),
85+
newVersion(this->commit.version) {
8686
inner = Renderer([&] {
8787
return vbox({
88-
text(ostreetui.GetOstreeRepo().getCommitList().at(hash).subject),
88+
text(ostreetui.GetOstreeRepo().GetCommitList().at(hash).subject),
8989
text(
9090
std::format("{:%Y-%m-%d %T %Ez}",
9191
std::chrono::time_point_cast<std::chrono::seconds>(
92-
ostreetui.GetOstreeRepo().getCommitList().at(hash).timestamp))),
92+
ostreetui.GetOstreeRepo().GetCommitList().at(hash).timestamp))),
9393
});
9494
});
9595
simpleCommit = inner;
@@ -160,7 +160,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
160160

161161
void executeDeletion() {
162162
// delete on the ostree repo
163-
ostreetui.RemoveCommit(ostreetui.GetOstreeRepo().getCommitList().at(hash));
163+
ostreetui.RemoveCommit(ostreetui.GetOstreeRepo().GetCommitList().at(hash));
164164
resetWindow();
165165
}
166166

@@ -183,12 +183,10 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
183183
}
184184
} else if (ostreetui.GetViewMode() == ViewMode::COMMIT_DROP &&
185185
ostreetui.GetModeHash() == hash) {
186-
const auto& commitList = ostreetui.GetOstreeRepo().getCommitList();
187-
auto commit = commitList.at(hash);
188186
startDeletionWindow(ostreetui.GetOstreeRepo().IsMostRecentCommitOnBranch(hash));
189187
}
190188

191-
auto element = ComponentBase::Render();
189+
ftxui::Element element = ComponentBase::Render();
192190

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

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

373371
// ostree-tui specific members
374-
int commitPosition;
372+
size_t commitPosition;
375373
std::string hash;
376374
OSTreeTUI& ostreetui;
377375

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

446444
} // namespace
447445

448-
ftxui::Component CommitComponent(int position, const std::string& commit, OSTreeTUI& ostreetui) {
446+
ftxui::Component CommitComponent(size_t position, const std::string& commit, OSTreeTUI& ostreetui) {
449447
return ftxui::Make<CommitComponentImpl>(position, commit, ostreetui);
450448
}
451449

@@ -475,7 +473,7 @@ ftxui::Element commitRender(OSTreeTUI& ostreetui,
475473
ostreetui.GetColumnToBranchMap().clear();
476474
for (const auto& visibleCommitIndex : ostreetui.GetVisibleCommitViewMap()) {
477475
const cpplibostree::Commit commit =
478-
ostreetui.GetOstreeRepo().getCommitList().at(visibleCommitIndex);
476+
ostreetui.GetOstreeRepo().GetCommitList().at(visibleCommitIndex);
479477
// branch head if it is first branch usage
480478
const std::string relevantBranch = commit.branch;
481479
if (usedBranches.at(relevantBranch) == -1) {

src/core/commit.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ constexpr int COMMIT_WINDOW_HEIGHT{4};
3232
constexpr int COMMIT_WINDOW_WIDTH{32};
3333
constexpr int PROMOTION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 11};
3434
constexpr int PROMOTION_WINDOW_WIDTH{COMMIT_WINDOW_WIDTH + 8};
35-
constexpr int DELETION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 9};
35+
constexpr int DELETION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 8};
3636
constexpr int DELETION_WINDOW_WIDTH{COMMIT_WINDOW_WIDTH + 8};
3737
// render tree types
3838
enum RenderTree : uint8_t {
@@ -51,7 +51,7 @@ enum RenderTree : uint8_t {
5151
*
5252
* @return UI Component
5353
*/
54-
[[nodiscard]] ftxui::Component CommitComponent(int position,
54+
[[nodiscard]] ftxui::Component CommitComponent(size_t position,
5555
const std::string& commit,
5656
OSTreeTUI& ostreetui);
5757

src/core/manager.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ BranchBoxManager::BranchBoxManager(OSTreeTUI& ostreetui,
5353
std::unordered_map<std::string, bool>& visibleBranches) {
5454
using namespace ftxui;
5555

56-
CheckboxOption cboption = {.on_change = [&] { ostreetui.RefreshCommitListComponent(); }};
56+
CheckboxOption cboption = CheckboxOption::Simple();
57+
cboption.on_change = [&] { ostreetui.RefreshCommitListComponent(); };
58+
// CheckboxOption cboption = {.on_change = [&] { ostreetui.RefreshCommitListComponent(); }};
5759

5860
// branch visibility
59-
for (const auto& branch : repo.getBranches()) {
61+
for (const auto& branch : repo.GetBranches()) {
6062
branchBoxes->Add(Checkbox(branch, &(visibleBranches.at(branch)), cboption));
6163
}
6264
}

src/core/trashBin.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ class TrashBinComponentImpl : public ComponentBase, public WindowOptions {
7171

7272
Element Render() final {
7373
// check if in deletion or stuff
74-
if (ostreetui.GetViewMode() == ViewMode::COMMIT_DRAGGING &&
75-
ostreetui.GetOstreeRepo().IsMostRecentCommitOnBranch(ostreetui.GetModeHash())) {
74+
if (ostreetui.GetViewMode() == ViewMode::COMMIT_DRAGGING) {
7675
showBin();
7776
} else {
7877
hideBin();
@@ -88,7 +87,7 @@ class TrashBinComponentImpl : public ComponentBase, public WindowOptions {
8887
return element;
8988
}
9089

91-
bool OnEvent(Event event) final { return false; }
90+
bool OnEvent(Event /*event*/) final { return false; }
9291

9392
private:
9493
OSTreeTUI& ostreetui;

src/util/cpplibostree.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
namespace cpplibostree {
2020

2121
OSTreeRepo::OSTreeRepo(std::string path) : repoPath(std::move(path)), commitList({}), branches({}) {
22-
updateData();
22+
UpdateData();
2323
}
2424

25-
bool OSTreeRepo::updateData() {
25+
bool OSTreeRepo::UpdateData() {
2626
// parse branches
2727
std::string branchString = getBranchesAsString();
2828
std::stringstream bss(branchString);
@@ -51,19 +51,19 @@ OstreeRepo* OSTreeRepo::_c() {
5151
return repo;
5252
}
5353

54-
const std::string& OSTreeRepo::getRepoPath() const {
54+
const std::string& OSTreeRepo::GetRepoPath() const {
5555
return repoPath;
5656
}
5757

58-
const CommitList& OSTreeRepo::getCommitList() const {
58+
const CommitList& OSTreeRepo::GetCommitList() const {
5959
return commitList;
6060
}
6161

62-
const std::vector<std::string>& OSTreeRepo::getBranches() const {
62+
const std::vector<std::string>& OSTreeRepo::GetBranches() const {
6363
return branches;
6464
}
6565

66-
bool OSTreeRepo::isCommitSigned(const Commit& commit) {
66+
bool OSTreeRepo::IsCommitSigned(const Commit& commit) {
6767
return commit.signatures.size() > 0;
6868
}
6969

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

350-
return runCLICommand(command);
350+
if (!runCLICommand(command)) {
351+
return false;
352+
};
351353
}
352354

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

361-
/// TODO This implementation should not rely on the ostree CLI -> change to libostree usage.
362-
bool OSTreeRepo::ResetBranchHeadAndPrune(const Commit& commit) {
363-
// check if it is last commit on branch
364-
if (!IsMostRecentCommitOnBranch(commit)) {
365-
return false;
366-
}
367-
368-
// reset head
369-
std::string command = "ostree reset";
370-
command += " --repo=" + repoPath;
371-
command += " " + commit.branch;
372-
command += " " + commit.branch + "^";
373-
if (!runCLICommand(command)) {
374-
return false;
375-
}
376-
377-
// remove orphaned commit
378-
std::string command2 = "ostree prune";
379-
command2 += " --repo=" + repoPath;
380-
command2 += " --delete-commit=" + commit.hash;
381-
if (!runCLICommand(command2)) {
382-
return false;
383-
}
384-
385-
return true;
363+
bool OSTreeRepo::ResetBranchHeadAndPrune(const std::string& branch) {
364+
return RemoveCommitFromBranchAndPrune(GetMostRecentCommitOfBranch(branch));
386365
}
387366

388367
bool OSTreeRepo::runCLICommand(const std::string& command) {
389-
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
368+
std::unique_ptr<FILE, int (*)(FILE*)> pipe(popen(command.c_str(), "r"), &pclose);
390369
if (!pipe) {
391370
return false;
392371
}

0 commit comments

Comments
 (0)