Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (initial) commit-signoff and gpg-signing support #405

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/dialogs/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class GeneralPanel : public QWidget {
mStoreCredentials =
new QCheckBox(tr("Store credentials in secure storage"), this);

mAutoSignoffCommits =
new QCheckBox(tr("Automatically signoff on commits"), this);

mGpgSignCommits = new QCheckBox(tr("Sign all commits"), this);

mGpgSignPushes = new QCheckBox(tr("Sign all pushes"), this);

mGpgSignTags = new QCheckBox(tr("Sign all tags"), this);

QLabel *privacy = new QLabel(tr("<a href='view'>View privacy policy</a>"));
connect(privacy, &QLabel::linkActivated,
[] { AboutDialog::openSharedInstance(AboutDialog::Privacy); });
Expand All @@ -125,6 +134,10 @@ class GeneralPanel : public QWidget {
form->addRow(QString(), mAutoPrune);
form->addRow(tr("Language:"), mNoTranslation);
form->addRow(tr("Credentials:"), mStoreCredentials);
form->addRow(tr("Auto Signoff:"), mAutoSignoffCommits);
form->addRow(tr("Sign Commits:"), mGpgSignCommits);
form->addRow(tr("Sign Pushes:"), mGpgSignPushes);
form->addRow(tr("Sign Tags:"), mGpgSignTags);
form->addRow(QString(), privacy);

#if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
Expand Down Expand Up @@ -184,6 +197,26 @@ class GeneralPanel : public QWidget {
delete CredentialHelper::instance();
});

connect(mAutoSignoffCommits, &QCheckBox::toggled, [](bool checked) {
git::Config config = git::Config::global();
config.setValue("format.signOff", checked);
});

connect(mGpgSignCommits, &QCheckBox::toggled, [](bool checked) {
git::Config config = git::Config::global();
config.setValue("commit.gpgSign", checked);
});

connect(mGpgSignPushes, &QCheckBox::toggled, [](bool checked) {
git::Config config = git::Config::global();
config.setValue("push.gpgSign", checked);
});

connect(mGpgSignTags, &QCheckBox::toggled, [](bool checked) {
git::Config config = git::Config::global();
config.setValue("tag.gpgSign", checked);
});

connect(mSingleInstance, &QCheckBox::toggled, [](bool checked) {
Settings::instance()->setValue(Setting::Id::AllowSingleInstanceOnly,
checked);
Expand Down Expand Up @@ -213,6 +246,10 @@ class GeneralPanel : public QWidget {
settings->value(Setting::Id::DontTranslate).toBool());
mStoreCredentials->setChecked(
settings->value(Setting::Id::StoreCredentials).toBool());
mAutoSignoffCommits->setChecked(config.value<bool>("format.signOff"));
mGpgSignCommits->setChecked(config.value<bool>("commit.gpgSign"));
mGpgSignPushes->setChecked(config.value<bool>("push.gpgSign"));
mGpgSignTags->setChecked(config.value<bool>("tag.gpgSign"));

mSingleInstance->setChecked(
settings->value(Setting::Id::AllowSingleInstanceOnly).toBool());
Expand All @@ -229,6 +266,10 @@ class GeneralPanel : public QWidget {
QCheckBox *mAutoPrune;
QCheckBox *mNoTranslation;
QCheckBox *mStoreCredentials;
QCheckBox *mAutoSignoffCommits;
QCheckBox *mGpgSignCommits;
QCheckBox *mGpgSignPushes;
QCheckBox *mGpgSignTags;
QCheckBox *mSingleInstance;
};

Expand Down
39 changes: 34 additions & 5 deletions src/git/Repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "git2/branch.h"
#include "git2/checkout.h"
#include "git2/cherrypick.h"
#include "git2/commit.h"
#include "git2/filter.h"
#include "git2/global.h"
#include "git2/ignore.h"
Expand All @@ -52,6 +53,7 @@
#include <QStandardPaths>
#include <QTextCodec>
#include <QVector>
#include <QDebug>

#ifdef Q_OS_UNIX
#include <pwd.h>
Expand Down Expand Up @@ -602,11 +604,40 @@ Commit Repository::commit(const Signature &author, const Signature &committer,
if (mergeHead.isValid())
parents.append(mergeHead.commit());

// Create the commit.
// Create the unsigned commit.
git_buf content = GIT_BUF_INIT_CONST(nullptr, 0);
if (git_commit_create_buffer(&content, d->repo, author, committer, 0,
message.toUtf8(), tree, parents.size(),
parents.data())) {
git_buf_dispose(&content);
return Commit();
}

// GPG-Sign content if enabled.
const char *gpg_signature = nullptr; // TODO: Add wrapper and sign content

// Store the commit.
git_oid id;
if (git_commit_create(&id, d->repo, "HEAD", author, committer, 0,
message.toUtf8(), tree, parents.size(), parents.data()))
int error = git_commit_create_with_signature(&id, d->repo, content.ptr,
gpg_signature, "gpgsig");
git_buf_dispose(&content);
if (error) {
return Commit();
}
git_commit *commit = nullptr;
git_commit_lookup(&commit, d->repo, &id);

// Update HEAD.
git_reference *ref = NULL, *ref_new = NULL;
git_reference_resolve(&ref, head());
error = git_reference_create(&ref_new, d->repo, git_reference_name(ref), &id,
1, git_commit_summary(commit));
git_reference_free(ref);
git_reference_free(ref_new);
if (error) {
git_commit_free(commit);
return Commit();
}

// Cleanup merge state.
switch (state()) {
Expand All @@ -621,8 +652,6 @@ Commit Repository::commit(const Signature &author, const Signature &committer,
break;
}

git_commit *commit = nullptr;
git_commit_lookup(&commit, d->repo, &id);
emit d->notifier->referenceUpdated(head());
return Commit(commit);
}
Expand Down
4 changes: 3 additions & 1 deletion test/Setting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ private slots:

template <class T, typename TId> QStringList settingsKeys() {
QStringList settingsKeys;
foreach (const TId id, ids<TId>()) { settingsKeys.append(T::key(id)); }
foreach (const TId id, ids<TId>()) {
settingsKeys.append(T::key(id));
}
return settingsKeys;
}

Expand Down