help.lua

  1-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2--
  3-- SPDX-License-Identifier: GPL-3.0-or-later
  4
  5local exit = require("wt.exit")
  6local EXIT_SUCCESS = exit.EXIT_SUCCESS
  7
  8---@class wt.help
  9local M = {}
 10
 11---Print usage information
 12function M.print_usage()
 13	print("wt - git worktree manager")
 14	print("")
 15	print("Usage: wt <command> [options]")
 16	print("")
 17	print("Commands:")
 18	print("  c <url> [--remote name]... [--own]   Clone into bare worktree structure")
 19	print("  n <project-name> [--remote name]...  Initialize fresh project")
 20	print("  a <branch> [-b [<start-point>]]      Add worktree with optional hooks")
 21	print("  r <branch> [-b] [-f]                 Remove worktree, optionally delete branch")
 22	print("  l                                    List worktrees with status")
 23	print("  f                                    Fetch all remotes")
 24	print("  init [--dry-run] [-y]                Convert existing repo to bare structure")
 25	print("  help                                 Show this help message")
 26end
 27
 28-- Per-command help text (using table.concat for performance)
 29M.COMMAND_HELP = {
 30	c = table.concat({
 31		"wt c <url> [--remote name]... [--own]",
 32		"",
 33		"Clone a repository into bare worktree structure.",
 34		"",
 35		"Arguments:",
 36		"  <url>              Git URL to clone",
 37		"",
 38		"Options:",
 39		"  --remote <name>    Add configured remote from ~/.config/wt/config.lua",
 40		"                     Can be specified multiple times",
 41		"  --own              Treat as your own project: first remote becomes 'origin'",
 42		"                     (default: 'origin' renamed to 'upstream', your remotes added)",
 43		"",
 44		"Examples:",
 45		"  wt c https://github.com/user/repo.git",
 46		"  wt c git@github.com:user/repo.git --remote github --own",
 47	}, "\n"),
 48
 49	n = table.concat({
 50		"wt n <project-name> [--remote name]...",
 51		"",
 52		"Initialize a fresh project with bare worktree structure.",
 53		"",
 54		"Arguments:",
 55		"  <project-name>     Name of the new project directory",
 56		"",
 57		"Options:",
 58		"  --remote <name>    Add configured remote from ~/.config/wt/config.lua",
 59		"                     Can be specified multiple times",
 60		"",
 61		"Examples:",
 62		"  wt n my-project",
 63		"  wt n my-project --remote github --remote gitlab",
 64	}, "\n"),
 65
 66	a = table.concat({
 67		"wt a <branch> [-b [<start-point>]]",
 68		"",
 69		"Add a worktree for a branch.",
 70		"",
 71		"Arguments:",
 72		"  <branch>           Branch name to checkout or create",
 73		"",
 74		"Options:",
 75		"  -b                 Create a new branch",
 76		"  <start-point>      Base commit/branch for new branch (only with -b)",
 77		"",
 78		"If run from inside an existing worktree, hooks from .wt.lua will be applied.",
 79		"",
 80		"Examples:",
 81		"  wt a main                    # Checkout existing branch",
 82		"  wt a feature/new -b          # Create new branch from HEAD",
 83		"  wt a feature/new -b main     # Create new branch from main",
 84	}, "\n"),
 85
 86	r = table.concat({
 87		"wt r <branch> [-b] [-f]",
 88		"",
 89		"Remove a worktree.",
 90		"",
 91		"Arguments:",
 92		"  <branch>           Branch name of worktree to remove",
 93		"",
 94		"Options:",
 95		"  -b                 Also delete the branch after removing worktree",
 96		"  -f                 Force removal even with uncommitted changes",
 97		"",
 98		"Examples:",
 99		"  wt r feature/old             # Remove worktree, keep branch",
100		"  wt r feature/old -b          # Remove worktree and delete branch",
101		"  wt r feature/old -f          # Force remove with uncommitted changes",
102	}, "\n"),
103
104	l = table.concat({
105		"wt l",
106		"",
107		"List all worktrees with status information.",
108		"",
109		"Displays a table showing:",
110		"  - Branch name",
111		"  - Relative path from project root",
112		"  - Commit status (ahead/behind remote)",
113		"  - Working tree status (clean/dirty)",
114	}, "\n"),
115
116	f = table.concat({
117		"wt f",
118		"",
119		"Fetch from all configured remotes.",
120		"",
121		"Runs 'git fetch --all' in the bare repository.",
122	}, "\n"),
123
124	init = table.concat({
125		"wt init [--dry-run] [-y]",
126		"",
127		"Convert an existing git repository to bare worktree structure.",
128		"",
129		"Options:",
130		"  --dry-run          Show what would be done without making changes",
131		"  -y                 Skip confirmation prompt",
132		"",
133		"This command:",
134		"  1. Moves .git/ to .bare/",
135		"  2. Creates .git file pointing to .bare/",
136		"  3. Creates a worktree for the current branch",
137		"  4. Removes orphaned files from project root",
138	}, "\n"),
139}
140
141---Show help for a specific command
142---@param cmd string
143function M.show_command_help(cmd)
144	local help = M.COMMAND_HELP[cmd]
145	if help then
146		print(help)
147	else
148		M.print_usage()
149	end
150	os.exit(EXIT_SUCCESS)
151end
152
153return M