Skip to content

Commit 1a784bc

Browse files
authored
Merge pull request #260 from epage/sign
feat: gpgSign support
2 parents 570242c + 154c566 commit 1a784bc

File tree

9 files changed

+122
-27
lines changed

9 files changed

+122
-27
lines changed

Cargo.lock

Lines changed: 32 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ human-panic = "1"
4343
termtree = "0.4"
4444
indexmap = "1"
4545

46-
git2-ext = "0.2.0"
46+
git2-ext = "0.3.0"
4747
git-branch-stash = "0.9"
4848
humantime = "2"
4949
itertools = "0.10"

docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,4 @@ Configuration is read from the following (in precedence order):
201201
| stack.show-stacked | \- | bool | Show branches as stacked on top of each other, where possible |
202202
| stack.auto-fixup | --fixup | "ignore", "move", "squash" | Default fixup operation with `--rebase` |
203203
| stack.auto-repair | \- | bool | Perform branch repair with `--rebase` |
204+
| stack.gpgSign | \- | bool | Sign commits, falling back to `commit.gpgSign` |

src/bin/git-stack/amend.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ impl AmendArgs {
6565
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
6666
repo.set_push_remote(repo_config.push_remote());
6767
repo.set_pull_remote(repo_config.pull_remote());
68+
let config = repo
69+
.raw()
70+
.config()
71+
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
72+
repo.set_sign(
73+
config
74+
.get_bool("stack.gpgSign")
75+
.or_else(|_| config.get_bool("commit.gpgSign"))
76+
.unwrap_or_default(),
77+
)
78+
.with_code(proc_exit::Code::FAILURE)?;
6879

6980
let protected = git_stack::git::ProtectedBranches::new(
7081
repo_config.protected_branches().iter().map(|s| s.as_str()),

src/bin/git-stack/reword.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ impl RewordArgs {
4242
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
4343
repo.set_push_remote(repo_config.push_remote());
4444
repo.set_pull_remote(repo_config.pull_remote());
45+
let config = repo
46+
.raw()
47+
.config()
48+
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
49+
repo.set_sign(
50+
config
51+
.get_bool("stack.gpgSign")
52+
.or_else(|_| config.get_bool("commit.gpgSign"))
53+
.unwrap_or_default(),
54+
)
55+
.with_code(proc_exit::Code::FAILURE)?;
4556

4657
let protected = git_stack::git::ProtectedBranches::new(
4758
repo_config.protected_branches().iter().map(|s| s.as_str()),

src/bin/git-stack/stack.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ impl State {
9191

9292
repo.set_push_remote(repo_config.push_remote());
9393
repo.set_pull_remote(repo_config.pull_remote());
94+
let config = repo
95+
.raw()
96+
.config()
97+
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
98+
repo.set_sign(
99+
config
100+
.get_bool("stack.gpgSign")
101+
.or_else(|_| config.get_bool("commit.gpgSign"))
102+
.unwrap_or_default(),
103+
)
104+
.with_code(proc_exit::Code::FAILURE)?;
94105

95106
let mut branches = git_stack::legacy::git::Branches::new([]);
96107
let mut protected_branches = git_stack::legacy::git::Branches::new([]);

src/bin/git-stack/sync.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ impl SyncArgs {
3535
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
3636
repo.set_push_remote(repo_config.push_remote());
3737
repo.set_pull_remote(repo_config.pull_remote());
38+
let config = repo
39+
.raw()
40+
.config()
41+
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
42+
repo.set_sign(
43+
config
44+
.get_bool("stack.gpgSign")
45+
.or_else(|_| config.get_bool("commit.gpgSign"))
46+
.unwrap_or_default(),
47+
)
48+
.with_code(proc_exit::Code::FAILURE)?;
3849

3950
let protected = git_stack::git::ProtectedBranches::new(
4051
repo_config.protected_branches().iter().map(|s| s.as_str()),

src/git/repo.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ impl Commit {
116116

117117
pub struct GitRepo {
118118
repo: git2::Repository,
119+
sign: Option<git2_ext::ops::UserSign>,
119120
push_remote: Option<String>,
120121
pull_remote: Option<String>,
121122
commits: std::cell::RefCell<std::collections::HashMap<git2::Oid, std::rc::Rc<Commit>>>,
@@ -128,6 +129,7 @@ impl GitRepo {
128129
pub fn new(repo: git2::Repository) -> Self {
129130
Self {
130131
repo,
132+
sign: None,
131133
push_remote: None,
132134
pull_remote: None,
133135
commits: Default::default(),
@@ -137,6 +139,17 @@ impl GitRepo {
137139
}
138140
}
139141

142+
pub fn set_sign(&mut self, yes: bool) -> Result<(), git2::Error> {
143+
if yes {
144+
let config = self.repo.config()?;
145+
let sign = git2_ext::ops::UserSign::from_config(&self.repo, &config)?;
146+
self.sign = Some(sign);
147+
} else {
148+
self.sign = None;
149+
}
150+
Ok(())
151+
}
152+
140153
pub fn set_push_remote(&mut self, remote: &str) {
141154
self.push_remote = Some(remote.to_owned());
142155
}
@@ -401,11 +414,21 @@ impl GitRepo {
401414
}
402415

403416
pub fn reword(&mut self, head_oid: git2::Oid, msg: &str) -> Result<git2::Oid> {
404-
git2_ext::ops::reword(&self.repo, head_oid, msg, None)
417+
git2_ext::ops::reword(
418+
&self.repo,
419+
head_oid,
420+
msg,
421+
self.sign.as_ref().map(|s| s as &dyn git2_ext::ops::Sign),
422+
)
405423
}
406424

407425
pub fn squash(&mut self, head_id: git2::Oid, into_id: git2::Oid) -> Result<git2::Oid> {
408-
git2_ext::ops::squash(&self.repo, head_id, into_id, None)
426+
git2_ext::ops::squash(
427+
&self.repo,
428+
head_id,
429+
into_id,
430+
self.sign.as_ref().map(|s| s as &dyn git2_ext::ops::Sign),
431+
)
409432
}
410433

411434
pub fn stash_push(&mut self, message: Option<&str>) -> Result<git2::Oid> {

src/legacy/git/repo.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl Commit {
119119

120120
pub struct GitRepo {
121121
repo: git2::Repository,
122+
sign: Option<git2_ext::ops::UserSign>,
122123
push_remote: Option<String>,
123124
pull_remote: Option<String>,
124125
commits: std::cell::RefCell<std::collections::HashMap<git2::Oid, std::rc::Rc<Commit>>>,
@@ -131,6 +132,7 @@ impl GitRepo {
131132
pub fn new(repo: git2::Repository) -> Self {
132133
Self {
133134
repo,
135+
sign: None,
134136
push_remote: None,
135137
pull_remote: None,
136138
commits: Default::default(),
@@ -140,6 +142,17 @@ impl GitRepo {
140142
}
141143
}
142144

145+
pub fn set_sign(&mut self, yes: bool) -> Result<(), git2::Error> {
146+
if yes {
147+
let config = self.repo.config()?;
148+
let sign = git2_ext::ops::UserSign::from_config(&self.repo, &config)?;
149+
self.sign = Some(sign);
150+
} else {
151+
self.sign = None;
152+
}
153+
Ok(())
154+
}
155+
143156
pub fn set_push_remote(&mut self, remote: &str) {
144157
self.push_remote = Some(remote.to_owned());
145158
}
@@ -435,7 +448,12 @@ impl GitRepo {
435448
head_id: git2::Oid,
436449
into_id: git2::Oid,
437450
) -> Result<git2::Oid, git2::Error> {
438-
git2_ext::ops::squash(&self.repo, head_id, into_id, None)
451+
git2_ext::ops::squash(
452+
&self.repo,
453+
head_id,
454+
into_id,
455+
self.sign.as_ref().map(|s| s as &dyn git2_ext::ops::Sign),
456+
)
439457
}
440458

441459
pub fn stash_push(&mut self, message: Option<&str>) -> Result<git2::Oid, git2::Error> {

0 commit comments

Comments
 (0)