Skip to content

Commit 8a9e6f4

Browse files
authored
refactor: dont sign if --sign not passed into cli arguments (#84)
2 parents 37af3da + dcd7428 commit 8a9e6f4

File tree

5 files changed

+159
-2
lines changed

5 files changed

+159
-2
lines changed

src/config/schema.js

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export const ForkConfigSchema = z.object({
252252
sign: z.boolean().describe("If true, git will sign the commit with the systems GPG key."),
253253
/**
254254
* If true, git will run user defined git hooks before committing.
255+
* @see {@link https://git-scm.com/docs/githooks Git - Git Hooks}
255256
* @default false
256257
*/
257258
verify: z.boolean().describe("If true, git will run user defined git hooks before committing."),

src/process/commit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function commitChanges(
4040
}
4141

4242
const shouldVerify = config.verify ? undefined : "--no-verify";
43-
const shouldSign = config.sign ? "--gpg-sign" : undefined;
43+
const shouldSign = config.sign ? "--gpg-sign" : "--no-gpg-sign";
4444

4545
await git.commit(
4646
shouldVerify,

src/process/tag.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ export async function tagChanges(
1919

2020
logger.log(`Creating tag: ${tag}`);
2121

22+
const shouldSign = config.sign ? "--sign" : "--no-sign";
23+
2224
await git.tag(
23-
config.sign ? "--sign" : "--annotate",
25+
shouldSign,
26+
"--annotate",
2427
tag,
2528
"--message",
2629
formatCommitMessage(config.changelogPresetConfig?.releaseCommitMessageFormat, nextVersion),

src/utils/git.ts

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export class Git {
3333
});
3434
}
3535

36+
/**
37+
* - [git-add Documentation](https://git-scm.com/docs/git-add)
38+
*/
3639
public async add(...args: (string | undefined)[]): Promise<string> {
3740
if (this.config.dryRun) {
3841
return "";
@@ -41,6 +44,9 @@ export class Git {
4144
return this.execGit("add", args.filter(Boolean) as string[]);
4245
}
4346

47+
/**
48+
* - [git-commit Documentation](https://git-scm.com/docs/git-commit)
49+
*/
4450
public async commit(...args: (string | undefined)[]): Promise<string> {
4551
if (this.config.dryRun) {
4652
return "";
@@ -49,6 +55,9 @@ export class Git {
4955
return this.execGit("commit", args.filter(Boolean) as string[]);
5056
}
5157

58+
/**
59+
* - [git-tag Documentation](https://git-scm.com/docs/git-tag)
60+
*/
5261
public async tag(...args: (string | undefined)[]): Promise<string> {
5362
if (this.config.dryRun) {
5463
return "";
@@ -57,6 +66,9 @@ export class Git {
5766
return this.execGit("tag", args.filter(Boolean) as string[]);
5867
}
5968

69+
/**
70+
* - [git-check-ignore Documentation](https://git-scm.com/docs/git-check-ignore)
71+
*/
6072
public async isIgnored(file: string): Promise<boolean> {
6173
try {
6274
await this.execGit("check-ignore", ["--no-index", file]);

tests/test-gpg-key.ps1

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env pwsh
2+
3+
param (
4+
[switch] $SkipConfirm = $false,
5+
[string] $KeyId
6+
)
7+
8+
$CurrentPath = Get-Location
9+
$SourceRootPath = Resolve-Path (Join-Path -Path $PSScriptRoot -ChildPath "..")
10+
$TestPath = Resolve-Path (Join-Path -Path $PSScriptRoot -ChildPath "..\..\fork-version.tests\gpg")
11+
$TempKeyContentPath = (Join-Path -Path $PSScriptRoot -ChildPath "TEST_GPG_KEY_CONTENT.tmp")
12+
13+
$KeyContent = @"
14+
Key-Type: DSA
15+
Key-Length: 1024
16+
Subkey-Type: ECDSA
17+
Subkey-Curve: nistp256
18+
Expire-Date: 0
19+
Name-Real: Fork Version
20+
Name-Email: [email protected]
21+
Passphrase: FORK_VERSION_PASSPHRASE
22+
"@.Trim()
23+
24+
# Functions
25+
#
26+
27+
function ConfirmAction {
28+
param (
29+
[string] $Title
30+
)
31+
32+
if ($SkipConfirm) {
33+
return $true
34+
}
35+
36+
$Choices = '&Yes', '&No'
37+
$Decision = $Host.UI.PromptForChoice($Title, "", $Choices, 1)
38+
return $Decision -eq 0
39+
}
40+
41+
function CreateTestFolder {
42+
# Create test directory if it doesn't exist
43+
if (!(Test-Path $TestPath)) {
44+
New-Item -Path $TestPath -ItemType Directory -Force | Out-Null
45+
}
46+
47+
if ((Get-ChildItem -Path $TestPath -Force).Count -ne 0) {
48+
# Confirm if the test directory should be cleared
49+
if (ConfirmAction -Title "Clear test directory?") {
50+
Remove-Item -Path $TestPath\* -Recurse -Force | Out-Null
51+
}
52+
else {
53+
Write-Host "Aborting test"
54+
exit 1
55+
}
56+
}
57+
}
58+
59+
function GenerateGPGKey {
60+
Write-Host "Generating GPG key"
61+
62+
# Create temporary file with key content
63+
New-Item -Path $TempKeyContentPath -ItemType File -Value $KeyContent -Force | Out-Null
64+
65+
gpg --batch --generate-key $TempKeyContentPath
66+
67+
if ($LastExitCode -ne 0) {
68+
Write-Error "Failed to generate GPG key"
69+
exit 1
70+
}
71+
else {
72+
Remove-Item -Path $TempKeyContentPath -Force | Out-Null
73+
}
74+
75+
# Search for the fingerprint
76+
$SearchPattern = "fpr:+(.*?):"
77+
78+
$Fingerprints = gpg --list-secret-keys --with-colons |
79+
Select-String -Pattern "fpr" |
80+
ForEach-Object {
81+
[regex]::Match($_.Line, $SearchPattern).Groups[1].Value
82+
}
83+
84+
return $Fingerprints[0]
85+
}
86+
87+
# Main
88+
#
89+
90+
function Main() {
91+
CreateTestFolder
92+
93+
Set-Location -Path $TestPath
94+
95+
$GPGKey = $KeyId
96+
if (([string]::IsNullOrEmpty($GPGKey)) -and (ConfirmAction -Title "Generate a new GPG key?")) {
97+
$GPGKey = GenerateGPGKey
98+
}
99+
100+
if ([string]::IsNullOrEmpty($GPGKey)) {
101+
Write-Error "GPG key is not set"
102+
exit 1
103+
}
104+
Write-Host "GPG Key: $GPGKey"
105+
106+
# Initialize a new git repository with GPG signing + create initial test file and commit
107+
git init --initial-branch=main
108+
git config user.signingKey $GPGKey
109+
git config commit.gpgSign true
110+
git config tag.gpgSign true
111+
112+
New-Item -Path "package.json" -ItemType File -Value @"
113+
{
114+
"version": "1.2.3"
115+
}
116+
"@.Trim() | Out-Null
117+
118+
git add .
119+
git commit -S -m "chore: init commit"
120+
121+
Set-Location -Path $SourceRootPath
122+
123+
# Run fork-version script with GPG signing
124+
pnpm run fork-version --path $TestPath --sign
125+
pnpm run fork-version --path $TestPath
126+
pnpm run fork-version --path $TestPath --sign
127+
128+
Set-Location -Path $TestPath
129+
130+
# Manually verify the commits and tags
131+
git --no-pager log --show-signature
132+
133+
git tag --list | ForEach-Object {
134+
Write-Host "Verifying tag: $_"
135+
git verify-tag $_
136+
}
137+
}
138+
139+
Main
140+
141+
Set-Location -Path $CurrentPath

0 commit comments

Comments
 (0)