From be56ea13adedd678ea2ff111f065d094d532dbf6 Mon Sep 17 00:00:00 2001 From: soramimi Date: Tue, 16 Jan 2024 22:26:31 +0900 Subject: [PATCH] gpg test --- src/CommitPropertyDialog.cpp | 82 ++++++++++--------- src/CommitPropertyDialog.ui | 151 +++++++++++++---------------------- src/Git.cpp | 31 ++++++- src/Git.h | 1 + src/common/misc.cpp | 9 +++ src/common/misc.h | 2 + src/gpg.cpp | 10 ++- src/gpg.h | 2 + 8 files changed, 149 insertions(+), 139 deletions(-) diff --git a/src/CommitPropertyDialog.cpp b/src/CommitPropertyDialog.cpp index a102a02c..6ef8ca6b 100644 --- a/src/CommitPropertyDialog.cpp +++ b/src/CommitPropertyDialog.cpp @@ -63,55 +63,59 @@ void CommitPropertyDialog::init(MainWindow *mw) ui->lineEdit_author->setText(m->commit.author); ui->lineEdit_mail->setText(m->commit.email); + // ui->label_signature_icon->setVisible(false); + QString text; for (Git::CommitID const &id : m->commit.parent_ids) { text += id.toQString() + '\n'; } 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 - auto sig = mainwindow()->git()->log_show_signature(m->commit.commit_id); - - gpg::Data key; - int n1 = m->commit.fingerprint.size(); - if (n1 > 0) { - QList keys; - if (gpg::listKeys(global->appsettings.gpg_command, &keys)) { - for (gpg::Data const &k : keys) { - int n2 = k.fingerprint.size(); - if (n2 > 0) { - int n = std::min(n1, n2); - char const *p1 = m->commit.fingerprint.data() + n1 - n; - char const *p2 = k.fingerprint.data() + n2 - n; - if (memcmp(p1, p2, n) == 0) { - key = k; - break; + gpg::Data gpg; + Git::Signature sig; + bool gpgsig = false; + auto s = mainwindow()->git()->log_show_signature(m->commit.commit_id); + if (s) { + sig = *s; + int n = sig.fingerprint.size(); + if (n > 0) { + QList keys; + if (gpg::listKeys(global->appsettings.gpg_command, &keys)) { + for (gpg::Data const &key : keys) { + if (n == key.fingerprint.size()) { + auto const *p1 = sig.fingerprint.data(); + auto const *p2 = key.fingerprint.data(); + if (memcmp(p1, p2, n) == 0) { + gpg = key; + break; + } } } + } else { + qDebug() << "Failed to get gpg keys"; + } + if (gpg.id.isEmpty()) { + // gpgコマンドが登録されていないなど、keyidが取得できなかったとき + gpg.id = tr(""); } - } else { - qDebug() << "Failed to get gpg keys"; - } - if (key.id.isEmpty()) { - // gpgコマンドが登録されていないなど、keyidが取得できなかったとき - key.id = tr(""); } + gpgsig = true; } - if (key.id.isEmpty()) { - ui->frame_sign->setVisible(false); + + if (gpgsig) { + ui->frame_sign->setVisible(true); + ui->textEdit_signature->setPlainText(sig.text); + // { + // int w = ui->label_signature_icon->width(); + // int h = ui->label_signature_icon->width(); + // QIcon icon = mainwindow()->verifiedIcon(m->commit.signature); + // ui->label_signature_icon->setPixmap(icon.pixmap(w, h)); + // } + ui->lineEdit_sign_id->setText(gpg.id); + ui->lineEdit_sign_name->setText(gpg.name); + ui->lineEdit_sign_mail->setText(gpg.mail); } else { - { - int w = ui->label_signature_icon->width(); - int h = ui->label_signature_icon->width(); - QIcon icon = mainwindow()->verifiedIcon(m->commit.signature); - ui->label_signature_icon->setPixmap(icon.pixmap(w, h)); - } - ui->lineEdit_sign_id->setText(key.id); - ui->lineEdit_sign_name->setText(key.name); - ui->lineEdit_sign_mail->setText(key.mail); + ui->frame_sign->setVisible(false); } global->avatar_loader.connectAvatarReady(this, &CommitPropertyDialog::avatarReady); @@ -131,8 +135,8 @@ void CommitPropertyDialog::updateAvatar(bool request) widget->setVisible(false); } }; - SetAvatar(ui->lineEdit_mail->text(), ui->widget_user_avatar); - SetAvatar(ui->lineEdit_sign_mail->text(), ui->widget_sign_avatar); + // SetAvatar(ui->lineEdit_mail->text(), ui->widget_user_avatar); + // SetAvatar(ui->lineEdit_sign_mail->text(), ui->widget_sign_avatar); } void CommitPropertyDialog::avatarReady() diff --git a/src/CommitPropertyDialog.ui b/src/CommitPropertyDialog.ui index 3bcceef8..6f6e6373 100644 --- a/src/CommitPropertyDialog.ui +++ b/src/CommitPropertyDialog.ui @@ -6,8 +6,8 @@ 0 0 - 527 - 482 + 724 + 632 @@ -112,97 +112,60 @@ - - - 2 + + + + 0 + 100 + - - - - - 20 - 20 - - - - - 20 - 20 - - - - :/image/signature-good.png - - - true - - - - - - - GPG Sign - - - - - - - - - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - 3 - - - - - ID - - - - - - - - - - Name - - - - - - - - - - Mail - - - - - - - - - - - - - 80 - 0 - - - - - + + GPG Sign + + + + + + true + + + + + + + + + ID + + + + + + + + + + Name + + + + + + + + + + Mail + + + + + + + + + + @@ -355,9 +318,7 @@ pushButton_checkout pushButton_close - - - + pushButton_close diff --git a/src/Git.cpp b/src/Git.cpp index 44628df3..420a1fcc 100644 --- a/src/Git.cpp +++ b/src/Git.cpp @@ -907,6 +907,7 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba) */ std::optional Git::log_show_signature(CommitID const &id) { + bool gpg = false; Git::Signature sig; QString cmd = "log --show-signature -1 %1"; cmd = cmd.arg(id.toQString()); @@ -915,10 +916,36 @@ std::optional Git::log_show_signature(CommitID const &id) QStringList lines = misc::splitLines(text); for (int i = 0; i < lines.size(); i++) { QString const &line = lines[i]; - if (line.startsWith("gpg:") || line.startsWith("Primary key fubgerorubt:")) { - sig.text += line.trimmed() + '\n'; + if (line.startsWith("gpg:")) { + gpg = true; + if (!sig.text.isEmpty()) { + sig.text += '\n'; + } + sig.text += line.trimmed(); + } else if (line.startsWith("Primary key fingerprint:")) { + std::string s = line.toStdString(); + unsigned char const *p = (unsigned char const *)strchr(s.c_str(), ':'); + if (p) { + p++; + while (1) { + if (isspace(*p)) { + p++; + } else if (isxdigit(p[0]) && isxdigit(p[1])) { + char tmp[3]; + tmp[0] = p[0]; + tmp[1] = p[1]; + tmp[2] = 0; + long c = (char)strtol(tmp, nullptr, 16); + sig.fingerprint.push_back((uint8_t)c); + p += 2; + } else { + break; + } + } + } } } + if (!gpg) return std::nullopt; return sig; } diff --git a/src/Git.h b/src/Git.h index 869c7990..d49d9175 100644 --- a/src/Git.h +++ b/src/Git.h @@ -116,6 +116,7 @@ class Git : QObject { QString text; QString author; QDateTime date; + std::vector fingerprint; }; class Hunk { diff --git a/src/common/misc.cpp b/src/common/misc.cpp index aa16bd1c..eb5dbcb0 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp @@ -553,3 +553,12 @@ bool misc::isValidMailAddress(const std::string &email) { return isValidMailAddress(QString::fromStdString(email)); } + +std::string_view misc::trimmed(const std::string_view &s) +{ + size_t i = 0; + size_t n = s.size(); + while (i < n && isspace((unsigned char)s[i])) i++; + while (i < n && isspace((unsigned char)s[n - 1])) n--; + return s.substr(i, n - i); +} diff --git a/src/common/misc.h b/src/common/misc.h index 6d23b571..4187826f 100644 --- a/src/common/misc.h +++ b/src/common/misc.h @@ -67,6 +67,8 @@ class misc { static QString collapseWhitespace(QString const &source); static bool isValidMailAddress(const QString &email); static bool isValidMailAddress(const std::string &email); + + static std::string_view trimmed(std::string_view const &s); }; class OverrideWaitCursor_ { diff --git a/src/gpg.cpp b/src/gpg.cpp index c224042f..fcd8810c 100644 --- a/src/gpg.cpp +++ b/src/gpg.cpp @@ -10,6 +10,8 @@ #include "MyProcess.h" +#include "common/misc.h" + void gpg::parse(char const *begin, char const *end, QList *keys) { char const *ptr = begin; @@ -79,6 +81,8 @@ void gpg::parse(char const *begin, char const *end, QList *keys) gpg::Data key; ParseKey(SkipHeader(pub.c_str()), &key); ParseUID(SkipHeader(uid.c_str()), &key); + key.pub = QString::fromStdString(pub); + key.sub = QString::fromStdString(sub); key.fingerprint = fingerprint; keys->push_back(key); } @@ -87,11 +91,11 @@ void gpg::parse(char const *begin, char const *end, QList *keys) } else if (line < ptr) { std::string s(line, ptr - line); if (strncmp(s.c_str(), "pub ", 4) == 0) { - pub = s; + pub = misc::trimmed(s.substr(4)); } else if (strncmp(s.c_str(), "uid ", 4) == 0) { - uid = s; + uid = misc::trimmed(s.substr(4)); } else if (strncmp(s.c_str(), "sub ", 4) == 0) { - sub = s; + sub = misc::trimmed(s.substr(4)); } else if (!pub.empty() && uid.empty()) { char const *p = strchr(s.c_str(), '='); if (p) { diff --git a/src/gpg.h b/src/gpg.h index 079e7ed0..2e072056 100644 --- a/src/gpg.h +++ b/src/gpg.h @@ -14,6 +14,8 @@ class gpg { QString name; QString comment; QString mail; + QString pub; + QString sub; QByteArray fingerprint; };