Skip to content

Commit

Permalink
cleanup cpplibostree
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 26, 2024
1 parent 6cc9daa commit c3ef8e6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 73 deletions.
14 changes: 7 additions & 7 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 @@ -211,7 +211,7 @@ void OSTreeTUI::RefreshCommitListComponent() {
}

bool OSTreeTUI::RefreshOSTreeRepository() {
ostreeRepo.updateData();
ostreeRepo.UpdateData();
RefreshCommitListComponent();
return true;
}
Expand Down Expand Up @@ -284,16 +284,16 @@ 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;
});
}

Expand Down
16 changes: 8 additions & 8 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
: commitPosition(position),
hash(std::move(commit)),
ostreetui(ostreetui),
commit(ostreetui.GetOstreeRepo().getCommitList().at(hash)),
commit(ostreetui.GetOstreeRepo().GetCommitList().at(hash)),
newVersion(this->commit.version),
drag_initial_y(position * COMMIT_WINDOW_HEIGHT),
drag_initial_x(1) {
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,7 +183,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
}
} else if (ostreetui.GetViewMode() == ViewMode::COMMIT_DROP &&
ostreetui.GetModeHash() == hash) {
const auto& commitList = ostreetui.GetOstreeRepo().getCommitList();
const auto& commitList = ostreetui.GetOstreeRepo().GetCommitList();
auto commit = commitList.at(hash);
startDeletionWindow(ostreetui.GetOstreeRepo().IsMostRecentCommitOnBranch(hash));
}
Expand Down Expand Up @@ -269,7 +269,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 @@ -423,7 +423,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 Down Expand Up @@ -475,7 +475,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
2 changes: 1 addition & 1 deletion src/core/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ BranchBoxManager::BranchBoxManager(OSTreeTUI& ostreetui,
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
39 changes: 8 additions & 31 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 @@ -358,31 +358,8 @@ 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) {
Expand Down
52 changes: 26 additions & 26 deletions src/util/cpplibostree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class OSTreeRepo {
OstreeRepo* _c();

/// Getter
const std::string& getRepoPath() const;
const std::string& GetRepoPath() const;
/// Getter
const CommitList& getCommitList() const;
const CommitList& GetCommitList() const;
/// Getter
const std::vector<std::string>& getBranches() const;
const std::vector<std::string>& GetBranches() const;

// Methods

Expand All @@ -105,7 +105,7 @@ class OSTreeRepo {
* @return true if data was changed during the reload
* @return false if nothing changed
*/
bool updateData();
bool UpdateData();

/**
* @brief Check if a certain commit is signed. This simply accesses the
Expand All @@ -115,24 +115,7 @@ class OSTreeRepo {
* @return true if the commit is signed
* @return false if the commit is not signed
*/
static bool isCommitSigned(const Commit& commit);

/**
* @brief Parse commits from a ostree log output to a commitList, mapping
* the hashes to commits.
*
* @param branch
* @return std::unordered_map<std::string,Commit>
*/
CommitList parseCommitsOfBranch(const std::string& branch);

/**
* @brief Performs parseCommitsOfBranch() on all available branches and
* merges all commit lists into one.
*
* @return std::unordered_map<std::string,Commit>
*/
CommitList parseCommitsAllBranches();
static bool IsCommitSigned(const Commit& commit);

// read & write access to OSTree repo:

Expand Down Expand Up @@ -164,18 +147,18 @@ class OSTreeRepo {
* If the commit is the most recent on its branch -> reset head of branch.
* If not, then remove this commit and all predecessors (that would otherwise be unreachable).
*
* @param commit Commit to remove (must match an element in the `getCommitList()`).
* @param commit Commit to remove (must match an element in the `GetCommitList()`).
* @return True on success.
*/
bool RemoveCommitFromBranchAndPrune(const Commit& commit);

/**
* @brief Drops commit, if it is the last commit on this branch.
* @brief Resets the specified branch head by one commit, similar to `git reset HEAD~`
*
* @param commit Commit to drop (must match an element in the `getCommitList()`).
* @param branch Branch to reset.
* @return True on success.
*/
bool ResetBranchHeadAndPrune(const Commit& commit);
bool ResetBranchHeadAndPrune(const std::string& branch);

/**
* @brief Checks if commit is the most recent commit on its branch
Expand All @@ -202,6 +185,23 @@ class OSTreeRepo {
bool IsMostRecentCommitOnBranch(const std::string& hash) const;

private:
/**
* @brief Parse commits from a ostree log output to a commitList, mapping
* the hashes to commits.
*
* @param branch
* @return std::unordered_map<std::string,Commit>
*/
CommitList parseCommitsOfBranch(const std::string& branch);

/**
* @brief Performs parseCommitsOfBranch() on all available branches and
* merges all commit lists into one.
*
* @return std::unordered_map<std::string,Commit>
*/
CommitList parseCommitsAllBranches();

/**
* @brief Execute a command on the CLI.
*
Expand Down

0 comments on commit c3ef8e6

Please sign in to comment.