opencode-git-trailers 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +161 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +30 -0
- package/dist/git-commit.d.ts +14 -0
- package/dist/git-commit.js +44 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +63 -0
- package/dist/interpolate.d.ts +10 -0
- package/dist/interpolate.js +13 -0
- package/dist/modify-command.d.ts +15 -0
- package/dist/modify-command.js +72 -0
- package/dist/trailers.d.ts +16 -0
- package/dist/trailers.js +33 -0
- package/dist/types.d.ts +32 -0
- package/dist/types.js +4 -0
- package/dist/variables.d.ts +18 -0
- package/dist/variables.js +41 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anthony Lannutti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# opencode-git-trailers
|
|
2
|
+
|
|
3
|
+
OpenCode plugin that automatically adds git trailers to commits made through OpenCode.
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Some projects require commits created with AI assistance to include metadata denoting this fact. Git trailers provide a standardized way to add such metadata to commit messages. This plugin ensures that trailers are deterministically added to all commits made through OpenCode.
|
|
8
|
+
|
|
9
|
+
## What It Does
|
|
10
|
+
|
|
11
|
+
This plugin intercepts `git commit` commands invoked by OpenCode and automatically appends configured trailers to the commit message. Trailers are added to the end of the commit message following the [git trailer format](https://git-scm.com/docs/git-interpret-trailers).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install opencode-git-trailers
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Basic Setup
|
|
22
|
+
|
|
23
|
+
Add the plugin to your OpenCode configuration:
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
{
|
|
27
|
+
"plugins": ["opencode-git-trailers"]
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Configuration
|
|
32
|
+
|
|
33
|
+
Configure trailers using git config with the `opencode.git-trailers` prefix.
|
|
34
|
+
|
|
35
|
+
#### Global Configuration
|
|
36
|
+
|
|
37
|
+
Configure trailers globally to apply to all repositories:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# Add the model used for the commit
|
|
41
|
+
git config --global opencode.git-trailers.model '{{model}}'
|
|
42
|
+
|
|
43
|
+
# Add yourself as a co-author with the AI
|
|
44
|
+
git config --global opencode.git-trailers.co-authored-by 'AI Assistant <ai@opencode.ai>'
|
|
45
|
+
|
|
46
|
+
# Add a signed-off-by trailer
|
|
47
|
+
git config --global opencode.git-trailers.signed-off-by '{{user.name}} <{{user.email}}>'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### Per-Repository Configuration
|
|
51
|
+
|
|
52
|
+
Configure trailers for a specific repository by running commands within the repository directory:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Navigate to your repository
|
|
56
|
+
cd /path/to/your/repo
|
|
57
|
+
|
|
58
|
+
# Add the model used for the commit (repository-specific)
|
|
59
|
+
git config opencode.git-trailers.model '{{model}}'
|
|
60
|
+
|
|
61
|
+
# Add a signed-off-by trailer (repository-specific)
|
|
62
|
+
git config opencode.git-trailers.signed-off-by '{{user.name}} <{{user.email}}>'
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Per-repository configuration overrides global configuration for the same trailer key.
|
|
66
|
+
|
|
67
|
+
#### Result
|
|
68
|
+
|
|
69
|
+
These configuration options result in the following trailers being added to commits:
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
Model: claude-sonnet-4-5@20250929
|
|
73
|
+
Co-authored-by: AI Assistant <ai@opencode.ai>
|
|
74
|
+
Signed-off-by: John Doe <john@example.com>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Variable Interpolation
|
|
78
|
+
|
|
79
|
+
Trailers support variable interpolation to include contextual information such as the model used:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# Configure a trailer with model interpolation
|
|
83
|
+
git config opencode.git-trailers.model '{{model}}'
|
|
84
|
+
|
|
85
|
+
# Use git user information
|
|
86
|
+
git config opencode.git-trailers.signed-off-by '{{user.name}} <{{user.email}}>'
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Available Variables
|
|
90
|
+
|
|
91
|
+
- `{{model}}` - The model identifier used for the commit
|
|
92
|
+
- `{{provider}}` - The provider name (e.g., "anthropic", "openai")
|
|
93
|
+
- `{{timestamp}}` - ISO 8601 timestamp of the commit
|
|
94
|
+
- `{{session}}` - OpenCode session ID
|
|
95
|
+
- `{{user.name}}` - Git user name from git config
|
|
96
|
+
- `{{user.email}}` - Git user email from git config
|
|
97
|
+
|
|
98
|
+
### Example Commit Message
|
|
99
|
+
|
|
100
|
+
Before this plugin:
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
Add user authentication
|
|
104
|
+
|
|
105
|
+
This commit implements JWT-based authentication
|
|
106
|
+
for the API endpoints.
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
After this plugin (with configured trailers):
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
Add user authentication
|
|
113
|
+
|
|
114
|
+
This commit implements JWT-based authentication
|
|
115
|
+
for the API endpoints.
|
|
116
|
+
|
|
117
|
+
Model: claude-sonnet-4-5@20250929
|
|
118
|
+
Co-authored-by: AI Assistant <ai@opencode.ai>
|
|
119
|
+
Signed-off-by: John Doe <john@example.com>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
### Prerequisites
|
|
125
|
+
|
|
126
|
+
- [Bun](https://bun.sh) 1.x or later
|
|
127
|
+
|
|
128
|
+
### Setup
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
bun install
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Build
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
bun run build
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Test
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Run tests once
|
|
144
|
+
bun run test
|
|
145
|
+
|
|
146
|
+
# Run tests in watch mode
|
|
147
|
+
bun run test:watch
|
|
148
|
+
|
|
149
|
+
# Run tests with coverage
|
|
150
|
+
bun run test:coverage
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Lint
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
bun run lint
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
MIT
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ShellAPI } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reads git trailer configuration from git config.
|
|
4
|
+
* Looks for config keys starting with "opencode.git-trailers."
|
|
5
|
+
* @param $shell - OpenCode's shell API
|
|
6
|
+
* @param cwd - Current working directory
|
|
7
|
+
* @returns Record of trailer configuration key-value pairs
|
|
8
|
+
*/
|
|
9
|
+
export declare function readGitTrailers($shell: ShellAPI, cwd: string): Promise<Record<string, string>>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const TRAILER_PREFIX = "opencode.git-trailers.";
|
|
2
|
+
/**
|
|
3
|
+
* Reads git trailer configuration from git config.
|
|
4
|
+
* Looks for config keys starting with "opencode.git-trailers."
|
|
5
|
+
* @param $shell - OpenCode's shell API
|
|
6
|
+
* @param cwd - Current working directory
|
|
7
|
+
* @returns Record of trailer configuration key-value pairs
|
|
8
|
+
*/
|
|
9
|
+
export async function readGitTrailers($shell, cwd) {
|
|
10
|
+
const configOutput = await $shell `git config --get-regexp '^opencode\.git-trailers\.'`
|
|
11
|
+
.cwd(cwd)
|
|
12
|
+
.nothrow()
|
|
13
|
+
.quiet()
|
|
14
|
+
.text();
|
|
15
|
+
if (!configOutput.trim()) {
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
const trailers = {};
|
|
19
|
+
const lines = configOutput.trim().split("\n");
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
const spaceIndex = line.indexOf(" ");
|
|
22
|
+
if (spaceIndex === -1)
|
|
23
|
+
continue;
|
|
24
|
+
const fullKey = line.substring(0, spaceIndex);
|
|
25
|
+
const value = line.substring(spaceIndex + 1);
|
|
26
|
+
const trailerName = fullKey.substring(TRAILER_PREFIX.length);
|
|
27
|
+
trailers[trailerName] = value;
|
|
28
|
+
}
|
|
29
|
+
return trailers;
|
|
30
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a command is a git commit command.
|
|
3
|
+
* Handles both standalone and chained commands (using && or ;).
|
|
4
|
+
* @param command - The command string to check
|
|
5
|
+
* @returns True if the command contains "git commit"
|
|
6
|
+
*/
|
|
7
|
+
export declare function isGitCommitCommand(command: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Extracts the commit message from a git commit command.
|
|
10
|
+
* Supports double-quoted, single-quoted, and unquoted message formats.
|
|
11
|
+
* @param command - The git commit command
|
|
12
|
+
* @returns The extracted commit message, or null if not found
|
|
13
|
+
*/
|
|
14
|
+
export declare function extractCommitMessage(command: string): string | null;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a command is a git commit command.
|
|
3
|
+
* Handles both standalone and chained commands (using && or ;).
|
|
4
|
+
* @param command - The command string to check
|
|
5
|
+
* @returns True if the command contains "git commit"
|
|
6
|
+
*/
|
|
7
|
+
export function isGitCommitCommand(command) {
|
|
8
|
+
const trimmed = command.trim();
|
|
9
|
+
// Check if it starts with "git commit"
|
|
10
|
+
if (trimmed.startsWith("git commit")) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
// Check for git commit in chained commands
|
|
14
|
+
// Match: && git commit or ; git commit (with word boundaries)
|
|
15
|
+
return /[;&]\s*git\s+commit\b/.test(command);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the commit message from a git commit command.
|
|
19
|
+
* Supports double-quoted, single-quoted, and unquoted message formats.
|
|
20
|
+
* @param command - The git commit command
|
|
21
|
+
* @returns The extracted commit message, or null if not found
|
|
22
|
+
*/
|
|
23
|
+
export function extractCommitMessage(command) {
|
|
24
|
+
if (!isGitCommitCommand(command)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
// Match double-quoted strings, handling escaped quotes (\")
|
|
28
|
+
const doubleQuoteMatch = command.match(/-m\s+"((?:\\"|[^"])*)"/);
|
|
29
|
+
if (doubleQuoteMatch) {
|
|
30
|
+
// Unescape the matched content
|
|
31
|
+
return doubleQuoteMatch[1]
|
|
32
|
+
.replace(/\\"/g, '"')
|
|
33
|
+
.replace(/\\\\/g, '\\');
|
|
34
|
+
}
|
|
35
|
+
const singleQuoteMatch = command.match(/-m\s+'([^']*)'/);
|
|
36
|
+
if (singleQuoteMatch) {
|
|
37
|
+
return singleQuoteMatch[1];
|
|
38
|
+
}
|
|
39
|
+
const unquotedMatch = command.match(/-m\s+(\S+)/);
|
|
40
|
+
if (unquotedMatch) {
|
|
41
|
+
return unquotedMatch[1];
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { readGitTrailers } from "./config.js";
|
|
2
|
+
import { isGitCommitCommand } from "./git-commit.js";
|
|
3
|
+
import { modifyGitCommitCommand } from "./modify-command.js";
|
|
4
|
+
import { buildTrailers } from "./trailers.js";
|
|
5
|
+
import { getUserVariables, buildContextVariables } from "./variables.js";
|
|
6
|
+
const plugin = async (input) => {
|
|
7
|
+
// Store model/provider in closure to access across hooks
|
|
8
|
+
let currentModel;
|
|
9
|
+
let currentProvider;
|
|
10
|
+
return {
|
|
11
|
+
"chat.params": async (hookInput) => {
|
|
12
|
+
// Capture model and provider from chat parameters
|
|
13
|
+
try {
|
|
14
|
+
if (hookInput.model?.id) {
|
|
15
|
+
currentModel = hookInput.model.id;
|
|
16
|
+
}
|
|
17
|
+
if (hookInput.provider?.info?.name) {
|
|
18
|
+
currentProvider = hookInput.provider.info.name;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// Silently ignore errors in capturing model/provider
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"tool.execute.before": async (hookInput, output) => {
|
|
26
|
+
try {
|
|
27
|
+
// Only process bash tool
|
|
28
|
+
if (hookInput.tool !== "bash") {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const command = output.args?.command;
|
|
32
|
+
if (!command || !isGitCommitCommand(command)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Read git trailer configuration
|
|
36
|
+
const workdir = output.args?.workdir;
|
|
37
|
+
const cwd = (typeof workdir === 'string' ? workdir : undefined) || input.directory;
|
|
38
|
+
const trailerConfig = await readGitTrailers(input.$, cwd);
|
|
39
|
+
if (Object.keys(trailerConfig).length === 0) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
// Collect all variables
|
|
43
|
+
const userVars = await getUserVariables(input.$, cwd);
|
|
44
|
+
const contextVars = buildContextVariables({
|
|
45
|
+
session: hookInput.sessionID,
|
|
46
|
+
model: currentModel,
|
|
47
|
+
provider: currentProvider,
|
|
48
|
+
});
|
|
49
|
+
const allVariables = { ...userVars, ...contextVars };
|
|
50
|
+
// Build and apply trailers
|
|
51
|
+
const trailers = buildTrailers(trailerConfig, allVariables);
|
|
52
|
+
const modifiedCommand = modifyGitCommitCommand(command, trailers);
|
|
53
|
+
output.args.command = modifiedCommand;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
// Gracefully handle errors - don't break the commit
|
|
57
|
+
// Log error for debugging but allow commit to proceed unchanged
|
|
58
|
+
console.error("opencode-git-trailers: Error processing trailers:", error);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
export default plugin;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Variables } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Interpolates variables into a template string.
|
|
4
|
+
* Variables are referenced using {{variableName}} or {{nested.path}} syntax.
|
|
5
|
+
* Undefined variables are left as-is in the template.
|
|
6
|
+
* @param template - Template string with variable placeholders
|
|
7
|
+
* @param variables - Record of variable values
|
|
8
|
+
* @returns Template with variables interpolated
|
|
9
|
+
*/
|
|
10
|
+
export declare function interpolateVariables(template: string, variables: Variables): string;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interpolates variables into a template string.
|
|
3
|
+
* Variables are referenced using {{variableName}} or {{nested.path}} syntax.
|
|
4
|
+
* Undefined variables are left as-is in the template.
|
|
5
|
+
* @param template - Template string with variable placeholders
|
|
6
|
+
* @param variables - Record of variable values
|
|
7
|
+
* @returns Template with variables interpolated
|
|
8
|
+
*/
|
|
9
|
+
export function interpolateVariables(template, variables) {
|
|
10
|
+
return template.replace(/\{\{(\w+(?:\.\w+)*)\}\}/g, (_, key) => {
|
|
11
|
+
return variables[key] ?? `{{${key}}}`;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a string for use within $'...' ANSI-C quoting.
|
|
3
|
+
* @param str - String to escape
|
|
4
|
+
* @returns Escaped string safe for $'...' context
|
|
5
|
+
*/
|
|
6
|
+
export declare function escapeForAnsiCQuotes(str: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Modifies a git commit command to include trailers in the commit message.
|
|
9
|
+
* Supports double-quoted, single-quoted, and unquoted message formats.
|
|
10
|
+
* Filters out trailers that already exist in the message.
|
|
11
|
+
* @param command - The git commit command to modify
|
|
12
|
+
* @param trailers - Record of trailers to append to the message
|
|
13
|
+
* @returns Modified command with trailers appended to the commit message
|
|
14
|
+
*/
|
|
15
|
+
export declare function modifyGitCommitCommand(command: string, trailers: Record<string, string>): string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isGitCommitCommand, extractCommitMessage } from "./git-commit.js";
|
|
2
|
+
import { formatTrailers } from "./trailers.js";
|
|
3
|
+
/**
|
|
4
|
+
* Escapes a string for use within $'...' ANSI-C quoting.
|
|
5
|
+
* @param str - String to escape
|
|
6
|
+
* @returns Escaped string safe for $'...' context
|
|
7
|
+
*/
|
|
8
|
+
export function escapeForAnsiCQuotes(str) {
|
|
9
|
+
return str
|
|
10
|
+
.replace(/\\/g, "\\\\") // Escape backslashes
|
|
11
|
+
.replace(/'/g, "\\'") // Escape single quotes
|
|
12
|
+
.replace(/\n/g, "\\n") // Convert newlines to \n escape sequence
|
|
13
|
+
.replace(/\r/g, "\\r") // Convert carriage returns to \r escape sequence
|
|
14
|
+
.replace(/\t/g, "\\t") // Convert tabs to \t escape sequence
|
|
15
|
+
// eslint-disable-next-line no-control-regex
|
|
16
|
+
.replace(/\x00/g, "\\x00"); // Convert null bytes to \x00 escape sequence
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Filters out trailers that already exist in the commit message (case-insensitive).
|
|
20
|
+
* @param message - The commit message to check
|
|
21
|
+
* @param trailers - Record of trailers to filter
|
|
22
|
+
* @returns New trailers record with duplicates removed
|
|
23
|
+
*/
|
|
24
|
+
function filterDuplicateTrailers(message, trailers) {
|
|
25
|
+
const filtered = {};
|
|
26
|
+
for (const [key, value] of Object.entries(trailers)) {
|
|
27
|
+
// Create a case-insensitive regex to match existing trailer
|
|
28
|
+
// Match "Key: value" format with the key capitalized
|
|
29
|
+
const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);
|
|
30
|
+
const pattern = new RegExp(`^${capitalizedKey}: .+$`, "mi");
|
|
31
|
+
// Only add the trailer if it doesn't already exist in the message
|
|
32
|
+
if (!pattern.test(message)) {
|
|
33
|
+
filtered[key] = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return filtered;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Modifies a git commit command to include trailers in the commit message.
|
|
40
|
+
* Supports double-quoted, single-quoted, and unquoted message formats.
|
|
41
|
+
* Filters out trailers that already exist in the message.
|
|
42
|
+
* @param command - The git commit command to modify
|
|
43
|
+
* @param trailers - Record of trailers to append to the message
|
|
44
|
+
* @returns Modified command with trailers appended to the commit message
|
|
45
|
+
*/
|
|
46
|
+
export function modifyGitCommitCommand(command, trailers) {
|
|
47
|
+
if (!isGitCommitCommand(command)) {
|
|
48
|
+
return command;
|
|
49
|
+
}
|
|
50
|
+
if (Object.keys(trailers).length === 0) {
|
|
51
|
+
return command;
|
|
52
|
+
}
|
|
53
|
+
const message = extractCommitMessage(command);
|
|
54
|
+
if (!message) {
|
|
55
|
+
return command;
|
|
56
|
+
}
|
|
57
|
+
// Filter out trailers that already exist in the message
|
|
58
|
+
const newTrailers = filterDuplicateTrailers(message, trailers);
|
|
59
|
+
if (Object.keys(newTrailers).length === 0) {
|
|
60
|
+
return command;
|
|
61
|
+
}
|
|
62
|
+
const formattedTrailers = formatTrailers(newTrailers);
|
|
63
|
+
// Build the new message with trailers using ANSI-C quoting ($'...')
|
|
64
|
+
// This allows \n to be interpreted as actual newlines by the shell
|
|
65
|
+
const escapedMessage = escapeForAnsiCQuotes(message);
|
|
66
|
+
const escapedTrailers = escapeForAnsiCQuotes(formattedTrailers);
|
|
67
|
+
const newMessage = `${escapedMessage}\\n\\n${escapedTrailers}`;
|
|
68
|
+
// Replace the -m flag with $'...' syntax which interprets escape sequences
|
|
69
|
+
// Match any of: -m "..." or -m '...' or -m word
|
|
70
|
+
// Note: $$ in replacement string becomes a literal $ in the result
|
|
71
|
+
return command.replace(/-m\s+(?:"[^"]*"|'[^']*'|\S+)/, "-m $$'" + newMessage + "'");
|
|
72
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Variables } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Formats trailers into git trailer format.
|
|
4
|
+
* Each trailer is formatted as "Key: value" with the key capitalized.
|
|
5
|
+
* @param trailers - Record of trailer key-value pairs
|
|
6
|
+
* @returns Formatted trailers joined by newlines
|
|
7
|
+
*/
|
|
8
|
+
export declare function formatTrailers(trailers: Record<string, string>): string;
|
|
9
|
+
/**
|
|
10
|
+
* Builds trailers by interpolating variables into trailer templates.
|
|
11
|
+
* Filters out empty values and uninterpolated variable placeholders.
|
|
12
|
+
* @param config - Record of trailer templates with variable placeholders
|
|
13
|
+
* @param variables - Record of variable values for interpolation
|
|
14
|
+
* @returns Record of trailers with interpolated values
|
|
15
|
+
*/
|
|
16
|
+
export declare function buildTrailers(config: Record<string, string>, variables: Variables): Record<string, string>;
|
package/dist/trailers.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { interpolateVariables } from "./interpolate.js";
|
|
2
|
+
/**
|
|
3
|
+
* Formats trailers into git trailer format.
|
|
4
|
+
* Each trailer is formatted as "Key: value" with the key capitalized.
|
|
5
|
+
* @param trailers - Record of trailer key-value pairs
|
|
6
|
+
* @returns Formatted trailers joined by newlines
|
|
7
|
+
*/
|
|
8
|
+
export function formatTrailers(trailers) {
|
|
9
|
+
const entries = [];
|
|
10
|
+
for (const [key, value] of Object.entries(trailers)) {
|
|
11
|
+
const formattedKey = key.charAt(0).toUpperCase() + key.slice(1);
|
|
12
|
+
entries.push(`${formattedKey}: ${value}`);
|
|
13
|
+
}
|
|
14
|
+
return entries.join("\n");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Builds trailers by interpolating variables into trailer templates.
|
|
18
|
+
* Filters out empty values and uninterpolated variable placeholders.
|
|
19
|
+
* @param config - Record of trailer templates with variable placeholders
|
|
20
|
+
* @param variables - Record of variable values for interpolation
|
|
21
|
+
* @returns Record of trailers with interpolated values
|
|
22
|
+
*/
|
|
23
|
+
export function buildTrailers(config, variables) {
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const [key, template] of Object.entries(config)) {
|
|
26
|
+
const value = interpolateVariables(template, variables);
|
|
27
|
+
const trimmedValue = value.trim();
|
|
28
|
+
if (trimmedValue && !trimmedValue.match(/^\{\{.*\}\}$/)) {
|
|
29
|
+
result[key] = value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared type definitions for the opencode-git-trailers plugin.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Shell expression types accepted by Bun's shell API.
|
|
6
|
+
*/
|
|
7
|
+
export type ShellExpression = {
|
|
8
|
+
toString(): string;
|
|
9
|
+
} | Array<ShellExpression> | string | {
|
|
10
|
+
raw: string;
|
|
11
|
+
} | ReadableStream;
|
|
12
|
+
/**
|
|
13
|
+
* Chainable shell command interface returned by ShellAPI.
|
|
14
|
+
*/
|
|
15
|
+
export interface ShellChain {
|
|
16
|
+
cwd(dir: string): ShellChain;
|
|
17
|
+
nothrow(): ShellChain;
|
|
18
|
+
quiet(): ShellChain;
|
|
19
|
+
text(): Promise<string>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* OpenCode's shell API for executing shell commands.
|
|
23
|
+
* The values parameter accepts the same types as Bun's shell API.
|
|
24
|
+
*/
|
|
25
|
+
export type ShellAPI = {
|
|
26
|
+
(strings: TemplateStringsArray, ...values: ShellExpression[]): ShellChain;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Variables used for template interpolation.
|
|
30
|
+
* Keys can be simple names (e.g., "model") or dot-separated paths (e.g., "user.name").
|
|
31
|
+
*/
|
|
32
|
+
export type Variables = Record<string, string | undefined>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ShellAPI, Variables } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves user variables from git config.
|
|
4
|
+
* @param $shell - OpenCode's shell API
|
|
5
|
+
* @param cwd - Current working directory
|
|
6
|
+
* @returns Variables object with user.name and user.email
|
|
7
|
+
*/
|
|
8
|
+
export declare function getUserVariables($shell: ShellAPI, cwd: string): Promise<Variables>;
|
|
9
|
+
/**
|
|
10
|
+
* Builds context variables from the current session and model information.
|
|
11
|
+
* @param context - Context object with optional model, provider, and session
|
|
12
|
+
* @returns Variables object with context information and current timestamp
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildContextVariables(context: {
|
|
15
|
+
model?: string;
|
|
16
|
+
provider?: string;
|
|
17
|
+
session?: string;
|
|
18
|
+
}): Variables;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieves user variables from git config.
|
|
3
|
+
* @param $shell - OpenCode's shell API
|
|
4
|
+
* @param cwd - Current working directory
|
|
5
|
+
* @returns Variables object with user.name and user.email
|
|
6
|
+
*/
|
|
7
|
+
export async function getUserVariables($shell, cwd) {
|
|
8
|
+
const name = await $shell `git config user.name`
|
|
9
|
+
.cwd(cwd)
|
|
10
|
+
.nothrow()
|
|
11
|
+
.quiet()
|
|
12
|
+
.text();
|
|
13
|
+
const email = await $shell `git config user.email`
|
|
14
|
+
.cwd(cwd)
|
|
15
|
+
.nothrow()
|
|
16
|
+
.quiet()
|
|
17
|
+
.text();
|
|
18
|
+
return {
|
|
19
|
+
"user.name": name.trim(),
|
|
20
|
+
"user.email": email.trim(),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Builds context variables from the current session and model information.
|
|
25
|
+
* @param context - Context object with optional model, provider, and session
|
|
26
|
+
* @returns Variables object with context information and current timestamp
|
|
27
|
+
*/
|
|
28
|
+
export function buildContextVariables(context) {
|
|
29
|
+
const variables = {};
|
|
30
|
+
if (context.model) {
|
|
31
|
+
variables.model = context.model;
|
|
32
|
+
}
|
|
33
|
+
if (context.provider) {
|
|
34
|
+
variables.provider = context.provider;
|
|
35
|
+
}
|
|
36
|
+
if (context.session) {
|
|
37
|
+
variables.session = context.session;
|
|
38
|
+
}
|
|
39
|
+
variables.timestamp = new Date().toISOString();
|
|
40
|
+
return variables;
|
|
41
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-git-trailers",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "OpenCode plugin for managing git trailers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"lint": "eslint src/ tests/",
|
|
17
|
+
"prepublishOnly": "bun run build",
|
|
18
|
+
"test": "vitest run",
|
|
19
|
+
"test:coverage": "vitest run --coverage",
|
|
20
|
+
"test:watch": "vitest"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@eslint/js": "^9.0.0",
|
|
24
|
+
"@opencode-ai/plugin": "^1.2.1",
|
|
25
|
+
"@types/bun": "^1.3.14",
|
|
26
|
+
"@types/node": "^25.2.3",
|
|
27
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
28
|
+
"eslint": "^9.0.0",
|
|
29
|
+
"typescript": "^5.7.0",
|
|
30
|
+
"typescript-eslint": "^8.55.0",
|
|
31
|
+
"vitest": "^4.0.18"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/lannuttia/opencode-git-trailers.git"
|
|
39
|
+
},
|
|
40
|
+
"author": "Anthony Lannutti <lannuttia@gmail.com>",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"homepage": "https://github.com/lannuttia/opencode-git-trailers#readme",
|
|
43
|
+
"bugs": "https://github.com/lannuttia/opencode-git-trailers/issues",
|
|
44
|
+
"keywords": [
|
|
45
|
+
"opencode",
|
|
46
|
+
"opencode-plugin",
|
|
47
|
+
"git",
|
|
48
|
+
"git-trailers",
|
|
49
|
+
"ai",
|
|
50
|
+
"coding-assistant",
|
|
51
|
+
"plugin"
|
|
52
|
+
],
|
|
53
|
+
"dependencies": {}
|
|
54
|
+
}
|