Skip to content

Commit

Permalink
feat(branch): #71 better branch order (#78)
Browse files Browse the repository at this point in the history
Added ability to change better branch order. Added version field (default disabled).

Closes: #71
  • Loading branch information
Everduin94 authored Jan 26, 2024
1 parent f78e5b2 commit 73fc9ab
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 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.

42 changes: 35 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
"confirm_ticket": true,
"add_to_title": true,
"append_hashtag": false,
"title_position": "start",
"surround": ""
"surround": "",
"title_position": "start"
},
"commit_title": {
"max_size": 70
Expand Down Expand Up @@ -231,15 +231,28 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
"enable": true,
"separator": "/"
},
"branch_version": {
"enable": false,
"required": false,
"separator": "/"
},
"branch_ticket": {
"enable": true,
"required": false,
"separator": "-"
},
"branch_description": {
"max_length": 70
"max_length": 70,
"separator": ""
},
"branch_action_default": "branch",
"branch_order": [
"user",
"version",
"type",
"ticket",
"description"
],
"enable_worktrees": true,
"overrides": {
"shell": "/bin/sh"
Expand Down Expand Up @@ -295,18 +308,33 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
| `breaking_change.add_exclamation_to_title` | If true adds exclamation mark to title for breaking changes |
| `confirm_commit` | If true manually confirm commit at end |
| `print_commit_output` | If true pretty print commit preview |
| `overrides.shell` | Override default shell, useful for windows users |

Branch configuration (same config file, split for readability)

| Property | Description |
| -------- | ----------- |
| `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 |
| `branch_user.separator` | Branch delimeter - "/" (default), "-", "_" |
| `branch_type.enable` | If enabled include type |
| `branch_type.separator` | Branch delimeter - "/" (default), "-", "_" |
| `branch_ticket.enable` | If enabled include ticket |
| `branch_ticket.required` | If enabled require ticket |
| `branch_ticket.separator` | Branch delimeter - "/", "-" (default), "_" |
| `branch_description.max_length` | Max length branch name |
| `branch_action_default` | 'branch' or 'worktree' |
| `branch_description.separator` | Branch delimeter - "" (default), "/", "-", "_" |
| `branch_version.enable` | If enabled include version |
| `branch_version.required` | If enabled require version |
| `branch_version.separator` | Branch delimeter - "", "/" (default), "-", "_" |
| `branch_order` | Order of branch name values (doesn't effect prompt order) |
| `branch_action_default` | "branch" or "worktree" |
| `enable_worktrees` | If false, always default to branch action |
| `overrides.shell` | Override default shell, useful for windows users |


</details>

### 🔎 Inference
Expand Down
30 changes: 25 additions & 5 deletions src/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
load_setup,
OPTIONAL_PROMPT,
Z_BRANCH_ACTIONS,
Z_BRANCH_CONFIG_FIELDS,
Z_BRANCH_FIELDS,
} from "./utils";
import { BranchState } from "./zod-state";
import * as p from "@clack/prompts";
Expand Down Expand Up @@ -77,6 +79,21 @@ async function main(config: z.infer<typeof Config>) {
branch_state.ticket = ticket;
}

if (config.branch_version.enable) {
const version_required = config.branch_version.required;
const version = await p.text({
message: `Type version number ${
version_required ? "" : OPTIONAL_PROMPT
}`.trim(),
placeholder: "",
validate: (val) => {
if (version_required && !val) return "Please enter a version";
},
});
if (p.isCancel(version)) process.exit(0);
branch_state.version = version;
}

const description_max_length = config.branch_description.max_length;
const description = await p.text({
message: "Type a short description",
Expand Down Expand Up @@ -165,11 +182,14 @@ function build_branch(
config: z.infer<typeof Config>
) {
let res = "";
if (branch.user) res += branch.user + config.branch_user.separator;
if (branch.type) res += branch.type + config.branch_type.separator;
if (branch.ticket) res += branch.ticket + config.branch_ticket.separator;
if (branch.description) res += branch.description;
return res;
config.branch_order.forEach((b: z.infer<typeof Z_BRANCH_FIELDS>) => {
const config_key: z.infer<typeof Z_BRANCH_CONFIG_FIELDS> = `branch_${b}`
if (branch[b]) res += branch[b] + config[config_key].separator
})
if (res.endsWith('-') || res.endsWith('/') || res.endsWith('_')) {
return res.slice(0, -1).trim();
}
return res.trim();
}

function get_user_from_cache(): string {
Expand Down
18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ export const OPTIONAL_PROMPT = `${color.dim("(optional)")}`;
export const CACHE_PROMPT = `${color.dim("(value will be saved)")}`;
export const REGEX_SLASH_TAG = new RegExp(/\/(\w+-\d+)/);
export const REGEX_START_TAG = new RegExp(/^(\w+-\d+)/);
export const REGEX_SLASH_NUM = new RegExp(/\/(\d+)/);
export const REGEX_START_NUM = new RegExp(/^(\d+)/);
export const REGEX_START_UND = new RegExp(/^([A-Z]+-[\[a-zA-Z\]\d]+)_/);
export const REGEX_SLASH_UND = new RegExp(/\/([A-Z]+-[\[a-zA-Z\]\d]+)_/);

// TODO: This might conflict with version from better-branch
// - Maybe negative lookup against .
// - Maybe check the order
// - Maybe use order to split and check values
export const REGEX_SLASH_NUM = new RegExp(/\/(\d+)/);
export const REGEX_START_NUM = new RegExp(/^(\d+)/);

export const DEFAULT_TYPE_OPTIONS = [
{ value: "feat", label: "feat", hint: "A new feature", emoji: "✨", trailer: "Changelog: feature"},
{ value: "fix", label: "fix", hint: "A bug fix", emoji: "🐛", trailer: "Changelog: fix"},
Expand Down Expand Up @@ -110,6 +115,15 @@ export const Z_FOOTER_OPTIONS = z.enum([
"deprecated",
"custom",
]);
export const Z_BRANCH_FIELDS = z.enum(["user", "version", "type", "ticket", "description"]);
export const Z_BRANCH_CONFIG_FIELDS = z.enum([
"branch_user",
"branch_version",
"branch_type",
"branch_ticket",
"branch_description"
]);
export const BRANCH_ORDER_DEFAULTS: z.infer<typeof Z_BRANCH_FIELDS>[] = ["user", "version", "type", "ticket", "description"]
export const Z_BRANCH_ACTIONS = z.enum(["branch", "worktree"]);
export const FOOTER_OPTION_VALUES: z.infer<typeof Z_FOOTER_OPTIONS>[] = [
"closes",
Expand Down
12 changes: 12 additions & 0 deletions src/zod-state.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { z } from "zod";
import {
BRANCH_ORDER_DEFAULTS,
CUSTOM_SCOPE_KEY,
DEFAULT_SCOPE_OPTIONS,
DEFAULT_TYPE_OPTIONS,
FOOTER_OPTION_VALUES,
Z_BRANCH_ACTIONS,
Z_BRANCH_FIELDS,
Z_FOOTER_OPTIONS,
} from "./utils";

Expand Down Expand Up @@ -141,6 +143,13 @@ export const Config = z
separator: z.enum(["/", "-", "_"]).default("/"),
})
.default({}),
branch_version: z
.object({
enable: z.boolean().default(false),
required: z.boolean().default(false),
separator: z.enum(["/", "-", "_"]).default("/"),
})
.default({}),
branch_ticket: z
.object({
enable: z.boolean().default(true),
Expand All @@ -151,9 +160,11 @@ export const Config = z
branch_description: z
.object({
max_length: z.number().positive().default(70),
separator: z.enum(["", "/", "-", "_"]).default(""),
})
.default({}),
branch_action_default: Z_BRANCH_ACTIONS.default("branch"),
branch_order: z.array(Z_BRANCH_FIELDS).default(BRANCH_ORDER_DEFAULTS),
enable_worktrees: z.boolean().default(true),
overrides: z.object({ shell: z.string().optional() }).default({}),
})
Expand Down Expand Up @@ -183,5 +194,6 @@ export const BranchState = z
type: z.string().default(""),
ticket: z.string().default(""),
description: z.string().default(""),
version: z.string().default("")
})
.default({});

0 comments on commit 73fc9ab

Please sign in to comment.