Skip to content

Commit

Permalink
GPG対応の改良(wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jan 15, 2024
1 parent 438f135 commit b252834
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
7 changes: 4 additions & 3 deletions src/CommitPropertyDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ 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
// 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();
Expand Down
34 changes: 32 additions & 2 deletions src/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ bool Git::chdirexec(std::function<bool()> const &fn)
return ok;
}

bool Git::git(QString const &arg, bool chdir, bool errout, AbstractPtyProcess *pty)
bool Git::git(QString const &arg, bool chdir, bool errout, AbstractPtyProcess *pty, QString const &prefix)
{
QElapsedTimer e;
e.start();
Expand Down Expand Up @@ -265,7 +265,13 @@ bool Git::git(QString const &arg, bool chdir, bool errout, AbstractPtyProcess *p
}

auto DoIt = [&](){
QString cmd = QString("\"%1\" --no-pager ").arg(gitCommand());
QString cmd;
#ifdef _WIN32
cmd = prefix;
#else

#endif
cmd += QString("\"%1\" --no-pager ").arg(gitCommand());
cmd += arg;

if (m->info.fn_log_writer_callback) {
Expand Down Expand Up @@ -896,6 +902,30 @@ Git::CommitItem Git::parseCommit(QByteArray const &ba)
return out;
}

/**
* @brief Git::log_show_signature
* @param id コミットID
* @return
*
* コミットに署名が付いている場合は署名情報を返す
*/
std::optional<Git::Signature> Git::log_show_signature(CommitID const &id)
{
Git::Signature sig;
QString cmd = "log --show-signature -1 %1";
cmd = cmd.arg(id.toQString());
if (!git(cmd, true)) return std::nullopt;
QString text = resultText();
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';
}
}
return sig;
}

std::optional<Git::CommitItem> Git::queryCommit(CommitID const &id)
{
Git::CommitItem ret;
Expand Down
9 changes: 8 additions & 1 deletion src/Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ class Git : QObject {
};
using CommitItemList = std::vector<CommitItem>;

struct Signature {
QString text;
QString author;
QDateTime date;
};

class Hunk {
public:
std::string at;
Expand Down Expand Up @@ -361,7 +367,7 @@ class Git : QObject {
void clearResult();
QString resultText() const;
bool chdirexec(std::function<bool ()> const &fn);
bool git(QString const &arg, bool chdir, bool errout = false, AbstractPtyProcess *pty = nullptr);
bool git(QString const &arg, bool chdir, bool errout = false, AbstractPtyProcess *pty = nullptr, const QString &prefix = {});
bool git(QString const &arg)
{
return git(arg, true);
Expand All @@ -379,6 +385,7 @@ class Git : QObject {
QStringList getUntrackedFiles();
CommitItemList log_all(QString const &id, int maxcount);
CommitItemList log(int maxcount);
std::optional<Git::Signature> log_show_signature(CommitID const &id);
std::optional<CommitItem> queryCommit(const CommitID &id);

struct CloneData {
Expand Down
73 changes: 53 additions & 20 deletions src/win32/Win32Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#include <QDateTime>
#include <QMutex>

QString GetErrorMessage(DWORD e)
{
QString msg;
wchar_t *p = nullptr;
size_t size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, e, 0, (wchar_t *)&p, 0, nullptr);
msg = QString::fromUtf16((ushort const *)p);
LocalFree(p);
return msg;
}

class OutputReaderThread : public QThread {
private:
Expand Down Expand Up @@ -130,8 +139,27 @@ class Win32ProcessThread : public QThread {
wchar_t *tmp = (wchar_t *)alloca(sizeof(wchar_t) * (len + 1));
memcpy(tmp, command.utf16(), sizeof(wchar_t) * len);
tmp[len] = 0;
if (!CreateProcessW(nullptr, tmp, nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi)) {
throw std::string("Failed to CreateProcess");
std::vector<wchar_t> env;
{
#if 1
wchar_t *p = GetEnvironmentStringsW();
if (p) {
int i = 0;
while (p[i] || p[i + 1]) {
i++;
}
env.insert(env.end(), p, p + i + 1);
FreeEnvironmentStringsW(p);
}
#endif
wchar_t const *e = L"LANG=en_US.UTF8";
env.insert(env.end(), e, e + wcslen(e) + 1);
env.push_back(0);
}
if (!CreateProcessW(nullptr, tmp, nullptr, nullptr, TRUE, CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT, (void *)env.data(), nullptr, &si, &pi)) {
DWORD e = GetLastError();
qDebug() << e << GetErrorMessage(e);
throw std::string("Failed to CreateProcess: ");
}

// 不要なハンドルを閉じる
Expand All @@ -148,26 +176,31 @@ class Win32ProcessThread : public QThread {
t1.start();
t2.start();

while (WaitForSingleObject(pi.hProcess, 1) != WAIT_OBJECT_0) {
QMutexLocker lock(mutex);
int n = inq.size();
if (n > 0) {
while (n > 0) {
char tmp[1024];
int l = n;
if (l > sizeof(tmp)) {
l = sizeof(tmp);
}
std::copy(inq.begin(), inq.begin() + l, tmp);
inq.erase(inq.begin(), inq.begin() + l);
if (hInputWrite != INVALID_HANDLE_VALUE) {
DWORD written;
WriteFile(hInputWrite, tmp, l, &written, nullptr);
while (1) {
auto r = WaitForSingleObject(pi.hProcess, 1);
if (r == WAIT_OBJECT_0) break;
if (r == WAIT_FAILED) break;
{
QMutexLocker lock(mutex);
int n = inq.size();
if (n > 0) {
while (n > 0) {
char tmp[1024];
int l = n;
if (l > sizeof(tmp)) {
l = sizeof(tmp);
}
std::copy(inq.begin(), inq.begin() + l, tmp);
inq.erase(inq.begin(), inq.begin() + l);
if (hInputWrite != INVALID_HANDLE_VALUE) {
DWORD written;
WriteFile(hInputWrite, tmp, l, &written, nullptr);
}
n -= l;
}
n -= l;
} else if (close_input_later) {
closeInput();
}
} else if (close_input_later) {
closeInput();
}
}

Expand Down

0 comments on commit b252834

Please sign in to comment.