Skip to content

Commit

Permalink
高速化(ただしGPGの情報を取得しない)
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jan 15, 2024
1 parent d0257cc commit 438f135
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 43 deletions.
18 changes: 9 additions & 9 deletions src/BlameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ void BlameWindow::on_tableWidget_customContextMenuRequested(const QPoint &pos)
int row = ui->tableWidget->currentRow();
if (row < 0 || row >= m->list.size()) return;

Git::CommitItem commit;
BlameItem blame = m->list[row];
GitPtr g = mainwindow()->git();
if (!g->queryCommit(blame.commit_id, &commit)) return;
auto commit = g->queryCommit(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 @@ -226,12 +226,12 @@ void BlameWindow::on_tableWidget_currentItemChanged(QTableWidgetItem *current, Q
info = it->second;
} else {
GitPtr g = mainwindow()->git();
Git::CommitItem commit;
if (g->queryCommit(id, &commit)) {
info.datetime = misc::makeDateTimeString(commit.commit_date);
info.author = commit.author;
info.email = commit.email;
info.message = commit.message;
auto commit = g->queryCommit(id);
if (commit) {
info.datetime = misc::makeDateTimeString(commit->commit_date);
info.author = commit->author;
info.email = commit->email;
info.message = commit->message;
}
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/CommitPropertyDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ void CommitPropertyDialog::init(MainWindow *mw)
}
ui->plainTextEdit_parent_ids->setPlainText(text);


auto file = mainwindow()->catFile(m->commit.commit_id, mainwindow()->git());
Git::CommitItem c = Git::parseCommit(file.data);
(void)c; //@todo

gpg::Data key;
int n1 = m->commit.fingerprint.size();
if (n1 > 0) {
Expand Down
59 changes: 46 additions & 13 deletions src/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QThread>
#include <QTimer>
#include <set>
#include <optional>

#define DEBUGLOG 0

Expand Down Expand Up @@ -237,6 +238,10 @@ bool Git::chdirexec(std::function<bool()> const &fn)

bool Git::git(QString const &arg, bool chdir, bool errout, AbstractPtyProcess *pty)
{
QElapsedTimer e;
e.start();

qDebug() << "git: " << arg;
QFileInfo info(gitCommand());
if (!info.isExecutable()) {
qDebug() << "Invalid git command: " << gitCommand();
Expand Down Expand Up @@ -307,6 +312,7 @@ bool Git::git(QString const &arg, bool chdir, bool errout, AbstractPtyProcess *p
qDebug() << timer.elapsed() << "ms";
#endif

qDebug() << e.elapsed();
return ok;
}

Expand Down Expand Up @@ -732,7 +738,8 @@ Git::CommitItemList Git::log_all(QString const &id, int maxcount)
CommitItemList items;
QString text;

QString cmd = "log --pretty=format:\"commit:%H#gpg:%G?#key:%GK#parent:%P#author:%an#mail:%ae#date:%ci##%s\" --all -%1 %2";
// QString cmd = "log --pretty=format:\"commit:%H#gpg:%G?#key:%GK#parent:%P#author:%an#mail:%ae#date:%ci##%s\" --all -%1 %2";
QString cmd = "log --pretty=format:\"commit:%H#parent:%P#author:%an#mail:%ae#date:%ci##%s\" --all -%1 %2"; // TODO: gpg関連の情報を取得すると遅すぎるので取得しない
cmd = cmd.arg(maxcount).arg(id);
git(cmd);
if (getProcessExitCode() == 0) {
Expand Down Expand Up @@ -821,6 +828,9 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
lines.pop_back();
}

bool gpgsig = false;
bool message = false;

int i;
for (i = 0; i < lines.size(); i++) {
QString const &line = lines[i];
Expand All @@ -835,14 +845,33 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
}
break;
}
if (gpgsig) {
if (line[0] == ' ') {
QString s = line.mid(1);
out.gpgsig += s + '\n';
if (s == "-----END PGP SIGNATURE-----") {
gpgsig = false;
}
}
continue;
}
if (line.isEmpty()) {
message = true;
continue;
}
if (message) {
if (!out.message.isEmpty()) {
out.message += '\n';
}
out.message += line;
}
if (line.startsWith("parent ")) {
out.parent_ids.push_back(line.mid(7));
} else if (line.startsWith("author ")) {
QStringList arr = misc::splitWords(line);
int n = arr.size();
if (n > 4) {
n -= 2;
// out->commit_date = QDateTime::fromTime_t(atol(arr[n].toStdString().c_str()));
out.commit_date = QDateTime::fromSecsSinceEpoch(atol(arr[n].toStdString().c_str()));
n--;
out.email = arr[n];
Expand All @@ -857,23 +886,27 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
out.author += arr[i];
}
}
} else if (line.startsWith("commiter ")) {
// nop
} else if (line.startsWith("gpgsig -----BEGIN PGP SIGNATURE-----")) {
out.gpgsig.append(line.mid(7));
gpgsig = true;
}
}
return out;
};
}

bool Git::queryCommit(CommitID const &id, CommitItem *out)
std::optional<Git::CommitItem> Git::queryCommit(CommitID const &id)
{
*out = {};
if (objectType(id) == "commit") {
out->commit_id = id;
QByteArray ba;
if (cat_file(id, &ba)) {
*out = parseCommit(ba);
}
return true;
Git::CommitItem ret;
if (objectType(id) != "commit") return std::nullopt;

ret.commit_id = id;
QByteArray ba;
if (cat_file(id, &ba)) {
ret = parseCommit(ba);
}
return false;
return ret;
}

Git::CloneData Git::preclone(QString const &url, QString const &path)
Expand Down
3 changes: 2 additions & 1 deletion src/Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class Git : QObject {
QString message;
QDateTime commit_date;
std::vector<TreeLine> parent_lines;
QString gpgsig;
QByteArray fingerprint;
char signature = 0; // git log format:%G?
bool has_child = false;
Expand Down Expand Up @@ -378,7 +379,7 @@ class Git : QObject {
QStringList getUntrackedFiles();
CommitItemList log_all(QString const &id, int maxcount);
CommitItemList log(int maxcount);
bool queryCommit(const CommitID &id, CommitItem *out);
std::optional<CommitItem> queryCommit(const CommitID &id);

struct CloneData {
QString url;
Expand Down
10 changes: 8 additions & 2 deletions src/GitDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,17 @@ bool GitDiff::diff(Git::CommitID const &id, const QList<Git::SubmoduleItem> &sub
if (id.startsWith('*')) {
out->item = mods;
out->item.id = g->rev_parse("HEAD");
g->queryCommit(out->item.id, &out->commit);
auto commit = g->queryCommit(out->item.id);
if (commit) {
out->commit = *commit;
}
} else if (Git::isValidID(id)) {
out->item = mods;
out->item.id = id;
g->queryCommit(out->item.id, &out->commit);
auto commit = g->queryCommit(out->item.id);
if (commit) {
out->commit = *commit;
}
} else {
*out = {};
}
Expand Down
37 changes: 23 additions & 14 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,8 @@ Git::SubmoduleItem const *MainWindow::querySubmoduleByPath(const QString &path,
if (submod.path == path) {
if (commit) {
GitPtr g = git(submod);
g->queryCommit(submod.id, commit);
auto c = g->queryCommit(submod.id);
if (c) *commit = *c;
}
return &submod;
}
Expand Down Expand Up @@ -3524,7 +3525,10 @@ void MainWindow::updateFilesList(RepositoryWrapperFrame *frame, QString const &i
data.submod = diff->b_submodule.item; // TODO:
if (data.submod) {
GitPtr g = git(data.submod);
g->queryCommit(data.submod.id, &data.submod_commit);
auto sc = g->queryCommit(data.submod.id);
if (sc) {
data.submod_commit = *sc;
}
}
}
AddItem(data);
Expand Down Expand Up @@ -3733,7 +3737,7 @@ void MainWindow::openRepository_(RepositoryWrapperFrame *frame, GitPtr g, bool k
QList<Git::Tag> tags = g->tags();
for (Git::Tag const &tag : tags) {
Git::Tag t = tag;
t.id = idFromTag(frame, tag.id.toQString());
// t.id = idFromTag(frame, tag.id.toQString()); //@TODO: これいらないのでは?
(*ptrCommitToTagMap(frame))[t.id].push_back(t);
}

Expand Down Expand Up @@ -3994,19 +3998,16 @@ void MainWindow::cherrypick(Git::CommitItem const *commit)
if (n == 1) {
g->cherrypick(commit->commit_id.toQString());
} else if (n > 1) {
Git::CommitItem head;
Git::CommitItem pick;
g->queryCommit(g->rev_parse("HEAD"), &head);
g->queryCommit(commit->commit_id.toQString(), &pick);
auto head = g->queryCommit(g->rev_parse("HEAD"));
auto pick = g->queryCommit(commit->commit_id.toQString());
QList<Git::CommitItem> parents;
for (int i = 0; i < n; i++) {
QString id = commit->commit_id.toQString() + QString("^%1").arg(i + 1);
Git::CommitID id2 = g->rev_parse(id);
Git::CommitItem item;
g->queryCommit(id2, &item);
parents.push_back(item);
auto item = g->queryCommit(id2);
parents.push_back(*item);
}
CherryPickDialog dlg(this, head, pick, parents);
CherryPickDialog dlg(this, *head, *pick, parents);
if (dlg.exec() == QDialog::Accepted) {
QString cmd = "-m %1 ";
cmd = cmd.arg(dlg.number());
Expand Down Expand Up @@ -4937,9 +4938,14 @@ void MainWindow::autoOpenRepository(QString dir, QString const &commit_id)

bool MainWindow::queryCommit(const QString &id, Git::CommitItem *out)
{
*out = Git::CommitItem();
GitPtr g = git();
return g->queryCommit(id, out);
auto commit = g->queryCommit(id);
if (commit) {
*out = *commit;
return true;
}
*out = {};
return false;
}

void MainWindow::checkout(RepositoryWrapperFrame *frame, QWidget *parent, const Git::CommitItem *commit, std::function<void ()> accepted_callback)
Expand Down Expand Up @@ -6498,7 +6504,10 @@ void MainWindow::on_action_submodules_triggered()
mods2[i].submodule = mod;

GitPtr g2 = git(g->workingDir(), mod.path, g->sshKey());
g2->queryCommit(mod.id, &mods2[i].head);
auto commit = g2->queryCommit(mod.id);
if (commit) {
mods2[i].head = *commit;
}
}

SubmodulesDialog dlg(this, mods2);
Expand Down
2 changes: 1 addition & 1 deletion src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ class MainWindow : public QMainWindow {
return t == type;
}
};
GitFile catFile(const Git::CommitID &id, GitPtr g);
protected:
void closeEvent(QCloseEvent *event) override;
void customEvent(QEvent *) override;
Expand Down Expand Up @@ -453,6 +452,7 @@ class MainWindow : public QMainWindow {
GitPtr git(const QString &dir, const QString &submodpath, const QString &sshkey) const;
GitPtr git();
GitPtr git(Git::SubmoduleItem const &submod);
GitFile catFile(const Git::CommitID &id, GitPtr g);
void autoOpenRepository(QString dir, const QString &commit_id = {});
bool queryCommit(const QString &id, Git::CommitItem *out);
void checkout(RepositoryWrapperFrame *frame, QWidget *parent, const Git::CommitItem *commit, std::function<void ()> accepted_callback = {});
Expand Down
6 changes: 3 additions & 3 deletions src/ObjectBrowserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ void ObjectBrowserDialog::on_pushButton_inspect_clicked()
QString id = item->data(IdRole).toString();
QString ty = item->data(TypeRole).toString();
if (Git::isValidID(id) && ty == "commit") {
Git::CommitItem commit;
if (g->queryCommit(id, &commit)) {
mainwindow()->execCommitPropertyDialog(this, &commit);
auto commit = g->queryCommit(id);
if (commit) {
mainwindow()->execCommitPropertyDialog(this, &*commit);
}
} else {
QMessageBox::information(this, tr("Object Inspection"), id + "\n\n" + ty);
Expand Down

0 comments on commit 438f135

Please sign in to comment.