Skip to content

Commit

Permalink
gpg test
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jan 16, 2024
1 parent 66f6822 commit be56ea1
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 139 deletions.
82 changes: 43 additions & 39 deletions src/CommitPropertyDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gpg::Data> 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<gpg::Data> 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("<Unknown>");
}
} else {
qDebug() << "Failed to get gpg keys";
}
if (key.id.isEmpty()) {
// gpgコマンドが登録されていないなど、keyidが取得できなかったとき
key.id = tr("<Unknown>");
}
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);
Expand All @@ -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()
Expand Down
151 changes: 56 additions & 95 deletions src/CommitPropertyDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>527</width>
<height>482</height>
<width>724</width>
<height>632</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -112,97 +112,60 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>2</number>
<widget class="QGroupBox" name="groupBox">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<item>
<widget class="QLabel" name="label_signature_icon">
<property name="minimumSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
<property name="pixmap">
<pixmap resource="resources/resources.qrc">:/image/signature-good.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>GPG Sign</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<layout class="QFormLayout" name="formLayout_2">
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="formAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="verticalSpacing">
<number>3</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>ID</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="ReadOnlyLineEdit" name="lineEdit_sign_id"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="ReadOnlyLineEdit" name="lineEdit_sign_name"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Mail</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="ReadOnlyLineEdit" name="lineEdit_sign_mail"/>
</item>
</layout>
</item>
<item>
<widget class="SimpleImageWidget" name="widget_sign_avatar" native="true">
<property name="minimumSize">
<size>
<width>80</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
<property name="title">
<string>GPG Sign</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTextEdit" name="textEdit_signature">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>ID</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="ReadOnlyLineEdit" name="lineEdit_sign_id"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="ReadOnlyLineEdit" name="lineEdit_sign_name"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Mail</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="ReadOnlyLineEdit" name="lineEdit_sign_mail"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -355,9 +318,7 @@
<tabstop>pushButton_checkout</tabstop>
<tabstop>pushButton_close</tabstop>
</tabstops>
<resources>
<include location="resources/resources.qrc"/>
</resources>
<resources/>
<connections>
<connection>
<sender>pushButton_close</sender>
Expand Down
31 changes: 29 additions & 2 deletions src/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
*/
std::optional<Git::Signature> 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());
Expand All @@ -915,10 +916,36 @@ std::optional<Git::Signature> 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;
}

Expand Down
1 change: 1 addition & 0 deletions src/Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Git : QObject {
QString text;
QString author;
QDateTime date;
std::vector<uint8_t> fingerprint;
};

class Hunk {
Expand Down
9 changes: 9 additions & 0 deletions src/common/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/common/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ {
Expand Down
10 changes: 7 additions & 3 deletions src/gpg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "MyProcess.h"

#include "common/misc.h"

void gpg::parse(char const *begin, char const *end, QList<gpg::Data> *keys)
{
char const *ptr = begin;
Expand Down Expand Up @@ -79,6 +81,8 @@ void gpg::parse(char const *begin, char const *end, QList<gpg::Data> *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);
}
Expand All @@ -87,11 +91,11 @@ void gpg::parse(char const *begin, char const *end, QList<gpg::Data> *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) {
Expand Down
2 changes: 2 additions & 0 deletions src/gpg.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class gpg {
QString name;
QString comment;
QString mail;
QString pub;
QString sub;
QByteArray fingerprint;
};

Expand Down

0 comments on commit be56ea1

Please sign in to comment.