Skip to content

Commit

Permalink
fix(branch): worktree commands (#58)
Browse files Browse the repository at this point in the history
Separate branch and worktree pre/post commands. Some operations that make sense for branches (like git stash), will throw an error in the root of a bare repository (like when working with worktrees). Increasing the granularity of the configuration tracks with previous decisions and helps avoid issues like this.
  • Loading branch information
Everduin94 authored Dec 15, 2023
1 parent 6712ccd commit 972c3f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
"print_commit_output": true,
"branch_pre_commands": [],
"branch_post_commands": [],
"worktree_pre_commands": [],
"worktree_post_commands": [],
"branch_user": {
"enable": true,
"required": false,
Expand Down Expand Up @@ -276,6 +278,8 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
| `print_commit_output` | If true pretty print commit preview |
| `branch_pre_commands` | Array of shell commands to run before branching |
| `branch_post_commands` | Array of shell commands to run after branching |
| `worktree_pre_commands` | Array of shell commands to run before creating worktree |
| `worktree_post_commands` | Array of shell commands to run after creating worktree |
| `branch_user.enable` | If enabled include user name |
| `branch_user.required` | If enabled require user name |
| `branch_user.separator` | Branch delimeter |
Expand Down Expand Up @@ -330,7 +334,7 @@ Optionally configure pre and post checkout commands, for example:
- run `npm install` before branching
- run `npm run dev` after branching

See *branch_pre_commands* and *branch_post_commands* in default config.
See *branch_pre_commands* and *branch_post_commands* in default config. (or *worktree_pre_commands* and *worktree_post_commands* for creating worktrees)

## 🌌 Mildly Interesting

Expand Down
12 changes: 10 additions & 2 deletions src/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ async function main(config: z.infer<typeof Config>) {
branch_state.description =
description?.replace(/\s+/g, "-")?.toLowerCase() ?? "";

config.branch_pre_commands.forEach((command) => {
const pre_commands =
checkout_type === "worktree"
? config.worktree_pre_commands
: config.branch_pre_commands;
pre_commands.forEach((command) => {
try {
execSync(command, { stdio: "inherit" });
} catch (err) {
Expand Down Expand Up @@ -142,7 +146,11 @@ async function main(config: z.infer<typeof Config>) {
}
}

config.branch_post_commands.forEach((command) => {
const post_commands =
checkout_type === "worktree"
? config.worktree_post_commands
: config.branch_post_commands;
post_commands.forEach((command) => {
try {
execSync(command, { stdio: "inherit" });
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions src/zod-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export const Config = z
print_commit_output: z.boolean().default(true),
branch_pre_commands: z.array(z.string()).default([]),
branch_post_commands: z.array(z.string()).default([]),
worktree_pre_commands: z.array(z.string()).default([]),
worktree_post_commands: z.array(z.string()).default([]),
branch_user: z
.object({
enable: z.boolean().default(true),
Expand Down

0 comments on commit 972c3f3

Please sign in to comment.