Skip to content

Commit

Permalink
refacotr: コミットIDをQStringからGit::CommitIDに変更するなど
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jun 23, 2024
1 parent 7f2161b commit 3ff9007
Show file tree
Hide file tree
Showing 21 changed files with 355 additions and 333 deletions.
32 changes: 17 additions & 15 deletions src/BlameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ QList<BlameItem> BlameWindow::parseBlame(char const *begin, char const *end)
return list;
}

QString BlameWindow::getCommitId(QTableWidgetItem *item) const
Git::CommitID BlameWindow::getCommitId(QTableWidgetItem *item) const
{
return item ? item->data(CommidIdRole).toString() : QString();
return item ? Git::CommitID(item->data(CommidIdRole).toString()) : Git::CommitID();
}

QString BlameWindow::currentCommitId() const
Git::CommitID BlameWindow::currentCommitId() const
{
QString id;
Git::CommitID id;
int row = ui->tableWidget->currentRow();
if (row >= 0 && row < m->list.size()) {
QTableWidgetItem *item = ui->tableWidget->item(row, 0);
Expand All @@ -181,9 +181,10 @@ QString BlameWindow::currentCommitId() const

void BlameWindow::on_tableWidget_itemDoubleClicked(QTableWidgetItem *)
{
QString id = currentCommitId();
if (Git::isValidID(id)) {
CommitPropertyDialog dlg(this, mainwindow(), id);
Git::CommitID commit_id = currentCommitId();
if (Git::isValidID(commit_id)) {
Git::CommitItem commit_item = mainwindow()->commitItem(nullptr, commit_id);
CommitPropertyDialog dlg(this, mainwindow(), commit_item);
dlg.showCheckoutButton(false);
dlg.showJumpButton(true);
if (dlg.exec() == QDialog::Accepted) {
Expand All @@ -200,15 +201,15 @@ void BlameWindow::on_tableWidget_customContextMenuRequested(const QPoint &pos)

BlameItem blame = m->list[row];
GitPtr g = mainwindow()->git();
auto commit = g->queryCommit(blame.commit_id);
std::optional<Git::CommitItem> commit = g->queryCommitItem(Git::CommitID(blame.commit_id));
if (!commit) return;

QMenu menu;
QAction *a_property = mainwindow()->addMenuActionProperty(&menu);
QAction *a = menu.exec(QCursor::pos() + QPoint(8, -8));
if (a) {
if (a == a_property) {
mainwindow()->execCommitPropertyDialog(this, &*commit);
mainwindow()->execCommitPropertyDialog(this, *commit);
return;
}
}
Expand All @@ -218,30 +219,31 @@ void BlameWindow::on_tableWidget_currentItemChanged(QTableWidgetItem *current, Q
{
(void)current;
(void)previous;
QString id = currentCommitId();
Git::CommitID commit_id = currentCommitId();
CommitInfo info;
if (Git::isValidID(id)) {
auto it = m->commit_cache.find(id);
if (Git::isValidID(commit_id)) {
auto it = m->commit_cache.find(commit_id.toQString());
if (it != m->commit_cache.end()) {
info = it->second;
} else {
GitPtr g = mainwindow()->git();
auto commit = g->queryCommit(id);
auto commit = g->queryCommitItem(commit_id);
if (commit) {
info.datetime = misc::makeDateTimeString(commit->commit_date);
info.author = commit->author;
info.email = commit->email;
info.message = commit->message;
m->commit_cache[commit_id.toQString()] = info;
}
}
} else {
id = QString();
commit_id = {};
}
QString author = info.author;
if (!info.email.isEmpty()) {
author = author + " <" + info.email + '>';
}
ui->lineEdit_commit_id->setText(id);
ui->lineEdit_commit_id->setText(commit_id.toQString());
ui->lineEdit_date->setText(info.datetime);
ui->lineEdit_author->setText(author);
ui->lineEdit_message->setText(info.message);
Expand Down
6 changes: 4 additions & 2 deletions src/BlameWindow.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef BLAMEWINDOW_H
#define BLAMEWINDOW_H

#include "Git.h"

#include <QDateTime>
#include <QDialog>

Expand Down Expand Up @@ -39,8 +41,8 @@ private slots:
MainWindow *mainwindow();

// QObject interface
QString getCommitId(QTableWidgetItem *item) const;
QString currentCommitId() const;
Git::CommitID getCommitId(QTableWidgetItem *item) const;
Git::CommitID currentCommitId() const;
};

#endif // BLAMEWINDOW_H
10 changes: 5 additions & 5 deletions src/CommitExploreWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CommitExploreWindow::CommitExploreWindow(QWidget *parent, MainWindow *mainwin, G
GitPtr g = git();
{
GitCommit c;
GitCommit::parseCommit(g, objcache, m->commit->commit_id.toQString(), &c);
GitCommit::parseCommit(g, objcache, m->commit->commit_id, &c);
m->root_tree_id = c.tree_id;
}

Expand Down Expand Up @@ -238,16 +238,16 @@ void CommitExploreWindow::on_listWidget_currentItemChanged(QListWidgetItem *curr

GitTreeItem::Type type = (GitTreeItem::Type)current->data(ItemTypeRole).toInt();
if (type == GitTreeItem::BLOB) {
QString id = current->data(ObjectIdRole).toString();
QString commit_id = current->data(ObjectIdRole).toString();
GitPtr g = git();
m->content_object = m->objcache->catFile(g, id);
m->content_object = m->objcache->catFile(g, Git::CommitID(commit_id));
QString path = current->data(FilePathRole).toString();
clearContent();
QString mimetype = mainwindow()->determinFileType(m->content_object.content);
if (misc::isImage(mimetype)) {
ui->widget_fileview->setImage(mimetype, m->content_object.content, id, path);
ui->widget_fileview->setImage(mimetype, m->content_object.content, commit_id, path);
} else {
ui->widget_fileview->setText(m->content_object.content, id, path);
ui->widget_fileview->setText(m->content_object.content, commit_id, path);
}
} else {
clearContent();
Expand Down
13 changes: 7 additions & 6 deletions src/CommitPropertyDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "ApplicationGlobal.h"
#include "AvatarLoader.h"
#include "MainWindow.h"
#include "UserEvent.h"
#include "common/misc.h"
#include "gpg.h"

Expand All @@ -12,16 +11,17 @@ struct CommitPropertyDialog::Private {
Git::CommitItem commit;
};

CommitPropertyDialog::CommitPropertyDialog(QWidget *parent, MainWindow *mw, Git::CommitItem const *commit)
CommitPropertyDialog::CommitPropertyDialog(QWidget *parent, MainWindow *mw, Git::CommitItem const &commit)
: QDialog(parent)
, ui(new Ui::CommitPropertyDialog)
, m(new Private)
{
ui->setupUi(this);
m->commit = *commit;
m->commit = commit;
init(mw);
}

#if 0
CommitPropertyDialog::CommitPropertyDialog(QWidget *parent, MainWindow *mw, QString const &commit_id) // used by BlameWindow
: QDialog(parent)
, ui(new Ui::CommitPropertyDialog)
Expand All @@ -33,10 +33,11 @@ CommitPropertyDialog::CommitPropertyDialog(QWidget *parent, MainWindow *mw, QStr
if (commit) {
m->commit = *commit;
} else {
m->commit.commit_id = commit_id;
m->commit.commit_id = Git::CommitID(commit_id);
}
init(mw);
}
#endif

CommitPropertyDialog::~CommitPropertyDialog()
{
Expand Down Expand Up @@ -82,7 +83,7 @@ void CommitPropertyDialog::init(MainWindow *mw)
}
ui->plainTextEdit_parent_ids->setPlainText(text);

auto sig = mainwindow()->git()->log_signature(m->commit.commit_id.toQString());
auto sig = mainwindow()->git()->log_signature(m->commit.commit_id);
if (sig) {
QString status;
QString sig_name;
Expand Down Expand Up @@ -187,7 +188,7 @@ void CommitPropertyDialog::showJumpButton(bool f)

void CommitPropertyDialog::on_pushButton_checkout_clicked()
{
mainwindow()->checkout(mainwindow()->frame(), this, &m->commit, [&](){ hide(); });
mainwindow()->checkout(mainwindow()->frame(), this, m->commit, [&](){ hide(); });
done(QDialog::Rejected);
}

Expand Down
4 changes: 2 additions & 2 deletions src/CommitPropertyDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class CommitPropertyDialog : public QDialog {
void setAvatar(const QImage &image, SimpleImageWidget *widget);
void updateAvatar(bool request);
public:
explicit CommitPropertyDialog(QWidget *parent, MainWindow *mw, Git::CommitItem const *commit);
explicit CommitPropertyDialog(QWidget *parent, MainWindow *mw, QString const &commit_id);
explicit CommitPropertyDialog(QWidget *parent, MainWindow *mw, Git::CommitItem const &commit);
// explicit CommitPropertyDialog(QWidget *parent, MainWindow *mw, QString const &commit_id);
~CommitPropertyDialog() override;

void showCheckoutButton(bool f);
Expand Down
2 changes: 1 addition & 1 deletion src/CommitViewWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CommitViewWindow::CommitViewWindow(MainWindow *parent, Git::CommitItem const *co
ui->lineEdit_message->setText(m->commit->message);
ui->lineEdit_id->setText(m->commit->commit_id.toQString());

mainwindow()->updateFilesList2(mainwindow()->frame(), m->commit->commit_id.toQString(), &m->diff_list, ui->listWidget_files);
mainwindow()->updateFilesList2(mainwindow()->frame(), m->commit->commit_id, &m->diff_list, ui->listWidget_files);

ui->listWidget_files->setCurrentRow(0);
}
Expand Down
6 changes: 3 additions & 3 deletions src/FileHistoryWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void FileHistoryWindow::prepare(GitPtr g, QString const &path)

{
OverrideWaitCursor;
m->commit_item_list = m->g->log_all(m->path, mainwindow()->limitLogCount());
m->commit_item_list = m->g->log_all(Git::CommitID(m->path), mainwindow()->limitLogCount());
}

collectFileHistory();
Expand Down Expand Up @@ -214,10 +214,10 @@ void FileHistoryWindow::on_tableWidget_log_currentItemChanged(QTableWidgetItem *
void FileHistoryWindow::on_tableWidget_log_customContextMenuRequested(const QPoint &pos)
{
(void)pos;
Git::CommitItem const *commit = nullptr;
Git::CommitItem commit;
int row = ui->tableWidget_log->currentRow();
if (row >= 0 && row < (int)m->commit_item_list.size()) {
commit = &m->commit_item_list[row];
commit = m->commit_item_list[row];
}
if (!commit) return;

Expand Down
4 changes: 2 additions & 2 deletions src/FilePropertyDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ FilePropertyDialog::~FilePropertyDialog()
delete ui;
}

void FilePropertyDialog::exec(QString const &path, QString const &id)
void FilePropertyDialog::exec(QString const &path, Git::CommitID const &id)
{
QFileInfo info(path);
if (!info.exists()) {
Expand All @@ -38,7 +38,7 @@ void FilePropertyDialog::exec(QString const &path, QString const &id)
ui->lineEdit_repo->setText(global->mainwindow->currentRepositoryName());
ui->lineEdit_path->setText(path);
ui->lineEdit_type->setText(mimetype);
ui->lineEdit_id->setText(id);
ui->lineEdit_id->setText(id.toQString());

QDialog::exec();
}
4 changes: 3 additions & 1 deletion src/FilePropertyDialog.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef FILEPROPERTYDIALOG_H
#define FILEPROPERTYDIALOG_H

#include "Git.h"

#include <QDialog>

namespace Ui {
Expand All @@ -17,7 +19,7 @@ class FilePropertyDialog : public QDialog {
explicit FilePropertyDialog(QWidget *parent = nullptr);
~FilePropertyDialog() override;

void exec(QString const &path, QString const &id);
void exec(QString const &path, const Git::CommitID &id);
private:
Ui::FilePropertyDialog *ui;
};
Expand Down
22 changes: 13 additions & 9 deletions src/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#include <QThread>
#include <QTimer>
#include <optional>
#include <set>

Git::CommitID::CommitID()
{

}

Git::CommitID::CommitID(const QString &qid)
{
Expand All @@ -30,7 +34,7 @@ void Git::CommitItem::setParents(const QStringList &list)
{
parent_ids.clear();
for (QString const &id : list) {
parent_ids.append(id);
parent_ids.append(Git::CommitID(id));
}
}

Expand Down Expand Up @@ -435,7 +439,7 @@ QList<Git::Tag> Git::tags2()
if (isValidID(l[0]) && l[1].startsWith("refs/tags/")) {
Tag t;
t.name = l[1].mid(10);
t.id = l[0];
t.id = Git::CommitID(l[0]);
list.push_back(t);
}
}
Expand Down Expand Up @@ -714,7 +718,7 @@ QList<Git::Branch> Git::branches()
} else {
int i = text.indexOf(' ');
if (i == GIT_ID_LENGTH) {
item.branch.id = text.mid(0, GIT_ID_LENGTH);
item.branch.id = Git::CommitID(text.mid(0, GIT_ID_LENGTH));
}
while (i < text.size() && QChar::isSpace(text.utf16()[i])) {
i++;
Expand Down Expand Up @@ -768,7 +772,7 @@ std::optional<Git::CommitItem> Git::parseCommitItem(QString const &line)
QString key = s.mid(0, j);
QString val = s.mid(j + 1);
if (key == "id") {
item.commit_id = val;
item.commit_id = Git::CommitID(val);
} else if (key == "gpg") { // %G? 署名検証結果
item.sign.verify = *val.utf16();
} else if (key == "key") { // %GF 署名フィンガープリント
Expand Down Expand Up @@ -899,7 +903,7 @@ std::optional<Git::CommitItem> Git::log_signature(CommitID const &id)

Git::CommitItemList Git::log(int maxcount)
{
return log_all(QString(), maxcount);
return log_all({}, maxcount);
}

Git::CommitItem Git::parseCommit(QByteArray const &ba)
Expand Down Expand Up @@ -950,7 +954,7 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
out.message += line;
}
if (line.startsWith("parent ")) {
out.parent_ids.push_back(line.mid(7));
out.parent_ids.push_back(Git::CommitID(line.mid(7)));
} else if (line.startsWith("author ")) {
QStringList arr = misc::splitWords(line);
int n = arr.size();
Expand Down Expand Up @@ -980,7 +984,7 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
return out;
}

std::optional<Git::CommitItem> Git::queryCommit(CommitID const &id)
std::optional<Git::CommitItem> Git::queryCommitItem(CommitID const &id)
{
Git::CommitItem ret;
if (objectType(id) != "commit") return std::nullopt;
Expand Down Expand Up @@ -1062,7 +1066,7 @@ QList<Git::SubmoduleItem> Git::submodules()
QStringList words = misc::splitWords(text);
if (words.size() >= 2) {
SubmoduleItem sm;
sm.id = words[0];
sm.id = Git::CommitID(words[0]);
sm.path = words[1];
if (isValidID(sm.id)) {
if (words.size() >= 3) {
Expand Down
Loading

0 comments on commit 3ff9007

Please sign in to comment.