Skip to content

Commit

Permalink
add git hooks docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 4, 2024
1 parent 182b72a commit 70b8b05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"tldraw-org.tldraw-vscode",
"bierner.emojisense",
"github.vscode-pull-request-github",
"ms-toolsai.prompty"
"ms-toolsai.prompty",
"unifiedjs.vscode-mdx"
]
}
44 changes: 37 additions & 7 deletions docs/src/content/docs/guides/auto-git-commit-message.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ensuring they are meaningful and save you time.
The script acts as a regular node.js automation script and uses [runPrompt](/genaiscript/reference/scripts/inner-prompts)
to issue calls to the LLM and ask the user to confirm the generated text.

## 🔍 **Explaining the Script**
## Explaining the Script

First, we check if there are any staged changes in the Git repository:

Expand All @@ -24,7 +24,7 @@ let { stdout } = await host.exec("git", ["diff", "--cached"])
If no changes are staged, we ask the user if they want to stage all changes. If the user confirms, we stage all changes. Otherwise, we bail out.

```ts
const stage = await host.confirm("No staged changes. Stage all changes?", {
const stage = await host.confirm("No staged changes. Stage all changes?", {
default: true,
})
if (stage) {
Expand Down Expand Up @@ -70,8 +70,7 @@ Options are given to edit or regenerate the message. If the user chooses to edit

```ts
if (choice === "edit") {
message = await host.input("Edit commit message",
{ required: true })
message = await host.input("Edit commit message", { required: true })
choice = "commit"
}
```
Expand All @@ -84,7 +83,7 @@ if (choice === "commit" && message) {
}
```

## 🚀 **Running the Script**
## Running the Script

You can run this script using the [CLI](/genaiscript/reference/cli).

Expand All @@ -96,8 +95,11 @@ You can wrap this command in a `gcm.sh` file or in your package `script` section

```json '"gcm": "genaiscript run gcm"'
{
"devDependencies": {
"genaiscript": "*"
},
"scripts": {
"gcm": "npx --yes genaiscript run gcm"
"gcm": "genaiscript run gcm"
}
}
```
Expand All @@ -108,7 +110,35 @@ Then you can run the script using:
npm run gcm
```

## Using git hooks

You can also attach to the [commit-msg](https://git-scm.com/docs/githooks#_commit_msg) git hook to run the message generation on demand.
Using the [huksy](https://typicode.github.io/husky/) framework, we can register the execution
of genaiscript in the `.husky/commit-msg` file.

Check failure on line 117 in docs/src/content/docs/guides/auto-git-commit-message.mdx

View workflow job for this annotation

GitHub Actions / build

Incorrect tool name 'huksy'; the correct name is 'Husky'.

The `commit-msg` hook receives a file location where the message is stored. We pass this parameter to the script
so that it gets populated in the `env.files` variable.

```bash title=".husky/commit-msg"
npx --yes genaiscript run commit-msg "$1"

Check failure on line 123 in docs/src/content/docs/guides/auto-git-commit-message.mdx

View workflow job for this annotation

GitHub Actions / build

Incorrect usage of 'npx --yes'; it should be removed as it is not necessary when running local npm scripts or dependencies.
```

In the script, we check if the content of the file already has a user message, otherwize generate a new message.

```js title="commit-msg.genai.mts"
const msg = env.files[0] // file created by git to hold the message
const msgContent = msg.content // check if the user added any message
?.split(/\n/g)
.filter((l) => l && !/^#/.test(l)) // filter out comments
.join("\n")
if (msgContent) cancel("commit message already exists")

...

await host.writeText(msg.filename, message)

Check failure on line 138 in docs/src/content/docs/guides/auto-git-commit-message.mdx

View workflow job for this annotation

GitHub Actions / build

Incorrect code comment 'otherwize'; the correct spelling is 'otherwise'.

Check failure on line 138 in docs/src/content/docs/guides/auto-git-commit-message.mdx

View workflow job for this annotation

GitHub Actions / build

The code comment 'cancel("commit message already exists")' suggests using a function 'cancel' which is not defined or explained in the provided code snippet.
```
## Acknowledgements
This script was inspired from Karpathy's
This script was inspired from Karpathy's
[commit message generator](https://gist.github.com/karpathy/1dd0294ef9567971c1e4348a90d69285).

0 comments on commit 70b8b05

Please sign in to comment.