@branchwright/cli

中文文档
1.0.1发布于 2025/11/24MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.2万

Git branch name linting and interactive branch creation tool

原始使用文档
文档目录35 节
  1. Installation
  2. Quick Start
  3. Interactive Branch Creation
  4. Validate Branch Names
  5. Configuration
  6. Configuration Options
  7. Template System
  8. Interactive Questions
  9. Customizing Prompts
  10. Rules System
  11. Built-in Rules
  12. Rule Configuration
  13. Custom Rules
  14. Rule Plugins
  15. Rule Presets
  16. CLI Reference
  17. brw create
  18. brw lint [branches...]
  19. brw init
  20. brw config
  21. Programmatic API
  22. Advanced: Rule Evaluation
  23. Integration
  24. Git Hooks (Husky)
  25. CI/CD (GitHub Actions)
  26. NPM Scripts
  27. Common Patterns
  28. Conventional Commits Style
  29. Ticket-Required Workflow
  30. Team with Optional Extras
  31. Troubleshooting
  32. "Not in a git repository"
  33. Branch name validation fails
  34. Custom rules not loading
  35. License
Branchwright

Branchwright

Git branch name linting and interactive branch creation - the commitizen/commitlint for branches

npm npm downloads Release TypeScript

Branchwright helps teams maintain consistent Git branch naming conventions with a streamlined, interactive workflow:

  • ✨ Frictionless branch creation - Smart defaults, minimal prompts
  • 🔍 Branch name validation - Lint existing branches against your rules
  • 🎯 Extensible rules system - Built-in rules plus custom rule plugins
  • ⚙️ Template-based naming - {{type}}/{{ticket}}-{{desc}} or any format you need
  • 🚀 CLI & API - Use standalone or integrate programmatically
  • 📦 TypeScript-first - Full type definitions included

Installation

# Global installation (recommended)
npm install -g @branchwright/cli

# Project-local installation
npm install --save-dev @branchwright/cli

# Or use without installing
npx @branchwright/cli create

Quick Start

Interactive Branch Creation

# If installed globally or locally
brw create

# Without installing (using npx)
npx @branchwright/cli create

That's it! Branchwright will guide you through:

  1. Selecting a branch type (feat, fix, chore, etc.)
  2. Optionally entering a ticket ID
  3. Describing your branch
  4. Creating and switching to the new branch

The tool uses smart defaults to minimize friction:

  • Base branch: current branch
  • Auto-checkout: yes
  • Push to remote: no (optional)

Validate Branch Names

# Validate current branch
brw lint

# Validate specific branch
brw lint feature/user-auth

# Validate all branches
brw lint --all

Configuration

Create branchwright.config.ts in your project root:

import { defineConfig } from '@branchwright/cli';

export default defineConfig({
  // Define your branch types
  branchTypes: [
    { name: 'feat', label: 'Feature' },
    { name: 'fix', label: 'Bug Fix' },
    { name: 'chore', label: 'Chore' },
  ],
  
  // Customize branch name template
  template: '{{type}}/{{ticket}}-{{desc}}',
  
  // Configure rules
  rules: {
    ticketId: ['optional', { prefix: 'PROJ-' }],
  },
  
  // Control optional questions (all default to false)
  extraQuestions: {
    baseBranch: false,    // Ask which branch to base from
    checkout: false,      // Ask whether to switch to new branch
    pushToRemote: false,  // Ask whether to push to remote
  },
  
  // Customize prompts (optional)
  questions: {
    branchType: 'What type of branch?',
    description: 'Describe your change:',
    pushToRemote: 'Push to origin?',
  },
});

Configuration Options

OptionTypeDefaultDescription
branchTypesBranchTypeOption[][feat, fix, chore]Available branch types for selection
templatestring'{{type}}/{{desc}}'Branch name template with placeholders
maxDescriptionLengthnumber24Maximum description length
descriptionStylestring'kebab-case'Enforce case style: kebab-case, snake_case, PascalCase, camelCase
ignoredBranchesstring[]['main', 'master', 'next', 'dev']Branches to skip during validation
rulesRules{}Rule configuration (see Rules section)
extraQuestionsInteractiveQuestionsAll falseEnable optional prompts
questionsQuestionConfig{}Customize prompt text

Template System

Use placeholders in your branch name template:

  • {{type}} - Branch type (feat, fix, etc.)
  • {{ticket}} - Ticket ID (if provided)
  • {{desc}} - Description

Examples:

// Simple format
template: '{{type}}/{{desc}}'
// Output: feat/user-authentication

// With ticket
template: '{{type}}/{{ticket}}-{{desc}}'
// Output: feat/PROJ-123-user-authentication

// Custom format
template: '{{ticket}}/{{type}}-{{desc}}'
// Output: PROJ-123/feat-user-authentication

Interactive Questions

Control which optional questions are shown during brw create:

extraQuestions: {
  baseBranch: true,    // Ask "Base branch?" (default: uses current branch)
  checkout: true,      // Ask "Switch to new branch?" (default: yes)
  pushToRemote: true,  // Ask "Push to remote?" (default: no)
}

By default, all are false for a streamlined experience. The tool:

  • Uses your current branch as the base
  • Automatically switches to the new branch
  • Doesn't push to remote

Enable questions only when you need that flexibility.

Customizing Prompts

Override the default prompt text:

questions: {
  branchType: 'Select the type of work:',
  ticketId: 'Ticket/issue number (optional):',
  ticketIdRequired: 'Ticket/issue number:',
  description: 'Brief description:',
  descriptionWithTicket: 'Describe your changes:',
  baseBranch: 'Create from which branch?',
  checkout: 'Switch to this branch now?',
  proceed: 'Create this branch?',
  pushToRemote: 'Push to origin now?',
}

Rules System

Branchwright includes a powerful, extensible rules engine for validation.

Built-in Rules

ticketId - Validate ticket ID format and requirements

rules: {
  // Off: No ticket ID validation
  ticketId: 'off',
  
  // Optional: Allow but don't require ticket IDs
  ticketId: 'optional',
  
  // Required: Ticket ID must be present
  ticketId: 'required',
  
  // With prefix validation
  ticketId: ['optional', { prefix: 'PROJ-' }],
  ticketId: ['required', { prefix: 'TEAM-' }],
}

Rule Configuration

Rules accept three severity levels:

  • 'off' - Rule is disabled
  • 'optional' - Rule runs but doesn't block
  • 'required' - Rule must pass

Configuration formats:

rules: {
  myRule: true,                           // Boolean (true = required, false = off)
  myRule: 'optional',                     // String severity
  myRule: ['required', { option: 'val' }], // Tuple with options
}

Custom Rules

Define your own validation rules:

import { defineRule, coreRuleRegistry, createRegistry } from '@branchwright/cli';

const noWipRule = defineRule(
  {
    id: 'no-wip',
    meta: {
      title: 'Disallow WIP branches',
      description: 'Prevents work-in-progress markers from shipping.',
    },
    defaultSeverity: 'required',
  },
  (context) => {
    if (context.branchName.includes('wip')) {
      return { 
        message: 'Remove "wip" from the branch name.',
        suggestions: [context.branchName.replace('wip', '')],
      };
    }
    return null; // Valid
  },
);

// Use with evaluateRules
const registry = createRegistry(...coreRuleRegistry.entries(), noWipRule);

Rule Plugins

Load custom rules from external modules:

// branchwright.config.ts
export default defineConfig({
  plugins: [
    './config/rules/company-rules.ts',  // Local file
    '@acme/branchwright-rules',         // NPM package
  ],
  rules: {
    'no-wip': 'required',
    'acme/ticket-format': ['required', { pattern: /^[A-Z]+-\d+$/ }],
  },
});

Plugin format:

// company-rules.ts
import { defineRule } from '@branchwright/cli';

export default [
  defineRule(/* ... */),
  defineRule(/* ... */),
];

Rule Presets

Combine multiple rule configurations:

export default defineConfig({
  presets: [
    'recommended',              // Built-in preset
    '@acme/strict-preset',      // Package preset
  ],
  rules: {
    // Override preset defaults
    ticketId: 'required',
  },
});

Presets are merged in order, with explicit rules taking final precedence.

CLI Reference

brw create

Create a new branch interactively or with pre-specified values.

Flags:

  • -t, --type <type> - Pre-specify branch type (e.g., feat, fix, chore)
  • --ticket <id> - Pre-specify ticket ID (e.g., JIRA-123)
  • -d, --desc <description> - Pre-specify branch description
  • -y, --yes - Skip confirmation prompt
  • --push - Push branch to remote after creation
  • --base <branch> - Specify base branch (overrides config)
  • --dry-run - Preview without creating the branch

Examples:

# Standard interactive creation
brw create

# Fully non-interactive - provide all values
brw create -t feat --ticket PROJ-123 -d "add-user-auth" -y

# Partial - skip some prompts
brw create -t fix -d "resolve-login-bug"

# Quick feature branch
brw create -t feat -d "new-dashboard" --push

# Create from specific base
brw create --base main

# Preview what would be created
brw create --dry-run

Non-Interactive Usage

Use flags to skip interactive prompts entirely - perfect for scripting and CI/CD:

Quick Branch Creation:

# Minimal - just type and description
brw create -t feat -d "add-oauth"

# With ticket ID
brw create -t fix --ticket BUG-456 -d "memory-leak"

# Skip confirmation
brw create -t chore -d "update-deps" -y

Automation & Scripts:

# CI/CD workflow
brw create -t release -d "v2.0.0" -y --push

# Batch creation script
for feature in auth payments dashboard; do
  brw create -t feat -d "$feature" -y
done

# Integration with ticket systems
TICKET=$(gh issue view 123 --json number -q .number)
brw create -t feat --ticket "GH-$TICKET" -d "implement-feature" --push

Validation:

Flags are validated just like interactive input:

  • --type must match a configured branch type
  • --ticket must match the configured prefix (if rules.ticketId is set)
  • --desc must follow descriptionStyle and maxDescriptionLength rules

Disabling Tips:

To hide CLI tip messages globally (e.g., for experienced users):

export default defineConfig({
  showCliTips: false,  // Disable "you can use flags" tip
  // ... rest of config
});

brw lint [branches...]

Validate branch names against your rules.

Flags:

  • --all - Check all local branches

Examples:

# Validate current branch
brw lint

# Validate specific branches
brw lint feature/auth fix/login-bug

# Check all branches
brw lint --all

brw init

Generate a default branchwright.config.ts file.

brw config

Display your current configuration.

Programmatic API

Use Branchwright in your Node.js code:

import { Branchwright } from '@branchwright/cli';

const branchwright = new Branchwright();

// Validate a branch name
const result = await branchwright.validate('feat/user-auth');
console.log(result.valid ? '✓ Valid' : `✗ ${result.message}`);

// Create a branch interactively
const branchName = await branchwright.create();

// Access underlying components
const validator = branchwright.getValidator();
const creator = branchwright.getCreator();

Advanced: Rule Evaluation

import { evaluateRules, coreRuleRegistry } from '@branchwright/cli';

const config = { /* your config */ };
const violations = await evaluateRules('feat/my-branch', config, coreRuleRegistry);

violations.forEach(v => console.error(v.message));

Integration

Git Hooks (Husky)

Validate branch names before pushing:

npm install --save-dev husky
npx husky init

.husky/pre-push:

#!/bin/sh
npx brw lint || exit 1

CI/CD (GitHub Actions)

name: Branch Lint
on: [pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
      - run: npm install -g @branchwright/cli
      - run: brw lint ${{ github.head_ref }}

NPM Scripts

{
  "scripts": {
    "branch": "brw create",
    "branch:lint": "brw lint"
  }
}

Common Patterns

Conventional Commits Style

export default defineConfig({
  branchTypes: [
    { name: 'feat', label: 'feat: New feature' },
    { name: 'fix', label: 'fix: Bug fix' },
    { name: 'docs', label: 'docs: Documentation' },
    { name: 'refactor', label: 'refactor: Code refactoring' },
    { name: 'test', label: 'test: Testing' },
    { name: 'chore', label: 'chore: Maintenance' },
  ],
  template: '{{type}}/{{desc}}',
});

Ticket-Required Workflow

export default defineConfig({
  template: '{{type}}/{{ticket}}-{{desc}}',
  rules: {
    ticketId: ['required', { prefix: 'PROJ-' }],
  },
});

Team with Optional Extras

export default defineConfig({
  template: '{{type}}/{{ticket}}-{{desc}}',
  rules: {
    ticketId: ['optional', { prefix: 'TEAM-' }],
  },
  extraQuestions: {
    pushToRemote: true,  // Ask about pushing
  },
});

Troubleshooting

"Not in a git repository"

Ensure you're running commands inside a Git repository:

git init  # If needed

Branch name validation fails

Check your configuration:

brw config

Verify your branch name matches the template and rules.

Custom rules not loading

Ensure plugin paths are correct and exports match the expected format:

// my-rules.ts
export default [defineRule(/* ... */)];

License

MIT © Branchwright Contributors


Developed and maintained by Noldaru.

版本与发布记录

@branchwright/cli 共发布 4 个版本,首次发布于 2025/11/09;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.1latest2025/11/2411 个月前
1.0.02025/11/2411 个月前
1.0.0-next.1next2025/11/2311 个月前
0.0.0-development2025/11/0911 个月前

关于 @branchwright/cli 的常见问题

@branchwright/cli 是什么?

Git branch name linting and interactive branch creation tool

如何安装 @branchwright/cli?

在项目目录运行 npm install @branchwright/cli,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 brw、branchwright,可以用 npx brw 直接运行。

@branchwright/cli 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@branchwright/cli 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@branchwright/cli 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@branchwright/cli 可以商用吗?

@branchwright/cli 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@branchwright/cli 的下载量有多少?

根据 npm 官方下载统计,@branchwright/cli 在 2026/10/09 – 2026/11/07 这 30 天内被下载 12,062 次,日均约 402 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@branchwright/cli 还在维护吗?

最新版本 1.0.1 发布于 2025/11/24(11 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2025/11/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。