Skip to content

Commit

Permalink
Refactor commit log access to use direct member access
Browse files Browse the repository at this point in the history
Modifies the way commit logs are accessed and stored in the application.
Instead of using getter and setter methods, the code now directly accesses the `commit_log` member of the `RepositoryWrapperFrame` class.
This change improves encapsulation and thread safety by consistently using mutex locks when accessing the commit log.
The `getLogs()` method now returns a copy of the commit log to prevent external modifications.
  • Loading branch information
soramimi committed Jun 24, 2024
1 parent 3678dc2 commit 9c4764f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
16 changes: 8 additions & 8 deletions src/LogTableWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void LogTableWidget::paintEvent(QPaintEvent *e)
pr.setRenderHint(QPainter::Antialiasing);
pr.setBrush(QBrush(QColor(255, 255, 255)));

Git::CommitItemList const *list = &frame()->getLogs();
Git::CommitItemList const list = frame()->getLogs();

int indent_span = 16;

Expand Down Expand Up @@ -282,15 +282,15 @@ void LogTableWidget::paintEvent(QPaintEvent *e)

auto DrawLine = [&](size_t index, int itemrow){
QRect rc1;
if (index < list->size()) {
Git::CommitItem const &item1 = list->at(index);
if (index < list.size()) {
Git::CommitItem const &item1 = list.at(index);
rc1 = ItemRect(itemrow);
QPointF pt1 = ItemPoint(item1.marker_depth, rc1);
double halfheight = rc1.height() / 2.0;
for (TreeLine const &line : item1.parent_lines) {
if (line.depth >= 0) {
QPainterPath *path = nullptr;
Git::CommitItem const &item2 = list->at(line.index);
Git::CommitItem const &item2 = list.at(line.index);
QRect rc2 = ItemRect(line.index);
if (index + 1 == (size_t)line.index || line.depth == item1.marker_depth || line.depth == item2.marker_depth) {
QPointF pt2 = ItemPoint(line.depth, rc2);
Expand Down Expand Up @@ -322,8 +322,8 @@ void LogTableWidget::paintEvent(QPaintEvent *e)
auto DrawMark = [&](size_t index, int row){
double x, y;
y = 0;
if (index < list->size()) {
Git::CommitItem const &item = list->at(index);
if (index < list.size()) {
Git::CommitItem const &item = list.at(index);
QRect rc = ItemRect(row);
QPointF pt = ItemPoint(item.marker_depth, rc);
double r = 4;
Expand Down Expand Up @@ -356,7 +356,7 @@ void LogTableWidget::paintEvent(QPaintEvent *e)
pr.setOpacity(opacity * 0.5);
pr.setBrush(Qt::NoBrush);

for (int i = 0; i < (int)list->size(); i++) {
for (int i = 0; i < (int)list.size(); i++) {
double y = DrawLine(i, i);
if (y >= height()) break;
}
Expand All @@ -366,7 +366,7 @@ void LogTableWidget::paintEvent(QPaintEvent *e)
pr.setOpacity(opacity * 1);
pr.setBrush(frame()->color(0));

for (int i = 0; i < (int)list->size(); i++) {
for (int i = 0; i < (int)list.size(); i++) {
double y = DrawMark(i, i);
if (y >= height()) break;
}
Expand Down
75 changes: 43 additions & 32 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2006,13 +2006,13 @@ void MainWindow::commit(RepositoryWrapperFrame *frame, bool amend)
std::lock_guard lock(frame->commit_log_mutex);

if (amend) {
message = getCommitLog(frame)[0].message;
message = frame->commit_log[0].message;
} else {
QString id = g->getCherryPicking();
if (Git::isValidID(id)) {
message = g->getMessage(id);
} else {
for (Git::CommitItem const &item : getCommitLog(frame).list) {
for (Git::CommitItem const &item : frame->commit_log.list) {
if (item.commit_id.isValid()) {
previousMessage = item.message;
break;
Expand Down Expand Up @@ -2848,7 +2848,7 @@ int MainWindow::rowFromCommitId(RepositoryWrapperFrame *frame, Git::CommitID con
{
std::lock_guard lock(frame->commit_log_mutex);

auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
for (size_t i = 0; i < logs.size(); i++) {
Git::CommitItem const &item = logs[i];
if (item.commit_id == id) {
Expand Down Expand Up @@ -2934,7 +2934,7 @@ void MainWindow::updateCommitGraph(RepositoryWrapperFrame *frame)
{
std::lock_guard lock(frame->commit_log_mutex);

auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
auto *logsp = getCommitLogPtr(frame);

const int LogCount = (int)logs.size();
Expand Down Expand Up @@ -3220,20 +3220,20 @@ Git::CommitItemList *MainWindow::getCommitLogPtr(RepositoryWrapperFrame *frame)
return &frame->commit_log;
}

const Git::CommitItemList &MainWindow::getCommitLog(RepositoryWrapperFrame const *frame) const
{
return frame->commit_log;
}
// const Git::CommitItemList &MainWindow::getCommitLog(RepositoryWrapperFrame const *frame) const
// {
// return frame->commit_log;
// }

void MainWindow::setCommitLog(RepositoryWrapperFrame *frame, const Git::CommitItemList &logs)
{
frame->commit_log = logs;
}
// void MainWindow::setCommitLog(RepositoryWrapperFrame *frame, const Git::CommitItemList &logs)
// {
// frame->commit_log = logs;
// }

void MainWindow::clearCommitLog(RepositoryWrapperFrame *frame)
{
frame->commit_log.clear();
}
// void MainWindow::clearCommitLog(RepositoryWrapperFrame *frame)
// {
// frame->commit_log.clear();
// }

PtyProcess *MainWindow::getPtyProcess()
{
Expand Down Expand Up @@ -3652,7 +3652,7 @@ QString MainWindow::makeCommitInfoText(RepositoryWrapperFrame *frame, int row, Q

QString message_ex;

Git::CommitItem commit = getCommitLog(frame)[row];
Git::CommitItem commit = frame->commit_log[row];

{ // branch
if (label_list) {
Expand Down Expand Up @@ -4246,7 +4246,7 @@ void MainWindow::updateCurrentFilesList(RepositoryWrapperFrame *frame)
int index = item->data(IndexRole).toInt();
{
std::lock_guard lock(frame->commit_log_mutex);
auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
int count = (int)logs.size();
Q_ASSERT(index >= 0 && index < count);
commit = logs[index];
Expand Down Expand Up @@ -4468,8 +4468,13 @@ void MainWindow::queryCommitLog(RepositoryWrapperFrame *frame, GitPtr g)

Git::CommitItemList commit_log = retrieveCommitLog(g); // コミットログを取得
QList<Git::Branch> branches = g->branches(); // ブランチを取得

setCommitLog(frame, commit_log);

{
std::lock_guard lock(frame->commit_log_mutex);

frame->commit_log = commit_log;
}
// setCommitLog(frame, commit_log);

// Uncommited changes がある場合、その親を取得するためにブランチ情報が必要
for (Git::Branch const &b : branches) {
Expand Down Expand Up @@ -4536,7 +4541,8 @@ void MainWindow::openRepositoryMain(RepositoryWrapperFrame *frame, GitPtr g, boo
getObjCache(frame)->setup(g);

if (clear_log) { // ログをクリア
clearCommitLog(frame);
std::lock_guard lock(frame->commit_log_mutex);
frame->commit_log.clear();
}

// リポジトリ情報をクリア
Expand Down Expand Up @@ -4695,7 +4701,7 @@ void MainWindow::updateStatusBarText(RepositoryWrapperFrame *frame)
QTableWidgetItem *item = frame->logtablewidget()->item(selectedLogIndex(frame, false), 0);
if (item) {
std::lock_guard lock(frame->commit_log_mutex);
auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
int row = item->data(IndexRole).toInt();
if (row < (int)logs.size()) {
Git::CommitItem const &commit = logs[row];
Expand Down Expand Up @@ -5920,10 +5926,15 @@ Git::CommitItem MainWindow::commitItem(RepositoryWrapperFrame const *frame, Git:
if (!frame) { // TODO:
frame = this->frame();
}

std::lock_guard lock(frame->commit_log_mutex);

return *frame->commit_log.find(id);

Git::CommitItem ret;

{
std::lock_guard lock(frame->commit_log_mutex);
ret = *frame->commit_log.find(id);
}

return ret;
}

QImage MainWindow::committerIcon(RepositoryWrapperFrame *frame, int row, QSize size) const
Expand All @@ -5933,7 +5944,7 @@ QImage MainWindow::committerIcon(RepositoryWrapperFrame *frame, int row, QSize s
Git::CommitItem commit;
{
std::lock_guard lock(frame->commit_log_mutex);
auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
if (row >= 0 && row < (int)logs.size()) {
commit = logs[row];
}
Expand Down Expand Up @@ -6017,7 +6028,7 @@ void MainWindow::findNext(RepositoryWrapperFrame *frame)

std::lock_guard lock(frame->commit_log_mutex);

auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
for (int pass = 0; pass < 2; pass++) {
int row = 0;
if (pass == 0) {
Expand Down Expand Up @@ -6048,7 +6059,7 @@ bool MainWindow::locateCommitID(RepositoryWrapperFrame *frame, QString const &co
{
std::lock_guard lock(frame->commit_log_mutex);

auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
int row = 0;
while (row < (int)logs.size()) {
Git::CommitItem const commit = logs[row];
Expand Down Expand Up @@ -6233,7 +6244,7 @@ int MainWindow::selectedLogIndex(RepositoryWrapperFrame *frame, bool lock) const
return selectedLogIndex(frame, false);
}

auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
int i = frame->logtablewidget()->currentRow();
if (i >= 0 && i < (int)logs.size()) {
return i;
Expand All @@ -6260,7 +6271,7 @@ void MainWindow::updateDiffView(RepositoryWrapperFrame *frame, QListWidgetItem *
auto it = getDiffCacheMap(frame)->find(key);
if (it != getDiffCacheMap(frame)->end()) {
std::lock_guard lock(frame->commit_log_mutex);
auto const &logs = getCommitLog(frame);
auto const &logs = frame->commit_log;
int row = frame->logtablewidget()->currentRow();
bool uncommited = (row >= 0 && row < (int)logs.size() && Git::isUncommited(logs[row]));
frame->filediffwidget()->updateDiffView(it->second, uncommited);
Expand Down Expand Up @@ -7228,7 +7239,7 @@ void MainWindow::on_action_find_triggered()
auto *f = frame();
std::lock_guard lock(f->commit_log_mutex);

if (getCommitLog(f).empty()) {
if (f->commit_log.empty()) {
return;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ class MainWindow : public QMainWindow {
void stopPtyProcess();
void abortPtyProcess();
Git::CommitItemList *getCommitLogPtr(RepositoryWrapperFrame *frame);
const Git::CommitItemList &getCommitLog(RepositoryWrapperFrame const *frame) const;
void setCommitLog(RepositoryWrapperFrame *frame, const Git::CommitItemList &logs);
void clearCommitLog(RepositoryWrapperFrame *frame);
// const Git::CommitItemList &getCommitLog(RepositoryWrapperFrame const *frame) const;
// void setCommitLog(RepositoryWrapperFrame *frame, const Git::CommitItemList &logs);
// void clearCommitLog(RepositoryWrapperFrame *frame);
PtyProcess *getPtyProcess();
bool getPtyProcessOk() const;
PtyCondition getPtyCondition();
Expand Down
5 changes: 3 additions & 2 deletions src/RepositoryWrapperFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ QString RepositoryWrapperFrame::currentBranchName() const
return mainwindow()->currentBranchName();
}

const Git::CommitItemList &RepositoryWrapperFrame::getLogs() const
Git::CommitItemList RepositoryWrapperFrame::getLogs() const
{
return mainwindow()->getCommitLog(this);
std::lock_guard lock(commit_log_mutex);
return commit_log;
}

bool RepositoryWrapperFrame::isAncestorCommit(const QString &id)
Expand Down
2 changes: 1 addition & 1 deletion src/RepositoryWrapperFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RepositoryWrapperFrame : public QFrame {
QImage committerIcon(int row, QSize size) const;
const QList<BranchLabel> *label(int row) const;
QString currentBranchName() const;
const Git::CommitItemList &getLogs() const;
Git::CommitItemList getLogs() const;
bool isAncestorCommit(const QString &id);
QColor color(unsigned int i);
void updateAncestorCommitMap();
Expand Down

0 comments on commit 9c4764f

Please sign in to comment.