1#!/usr/bin/env lua
2
3-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
4--
5-- SPDX-License-Identifier: GPL-3.0-or-later
6
7if _VERSION < "Lua 5.2" then
8 io.stderr:write("error: wt requires Lua 5.2 or later\n")
9 os.exit(1)
10end
11
12local exit = require("wt.exit")
13local EXIT_SUCCESS = exit.EXIT_SUCCESS
14
15local shell = require("wt.shell")
16local run_cmd = shell.run_cmd
17local run_cmd_silent = shell.run_cmd_silent
18local die = shell.die
19
20local path_mod = require("wt.path")
21local branch_to_path = path_mod.branch_to_path
22local split_path = path_mod.split_path
23local relative_path = path_mod.relative_path
24local path_inside = path_mod.path_inside
25local escape_pattern = path_mod.escape_pattern
26
27local git_mod = require("wt.git")
28local find_project_root = git_mod.find_project_root
29local detect_source_worktree = git_mod.detect_source_worktree
30local parse_branch_remotes = git_mod.parse_branch_remotes
31local parse_worktree_list = git_mod.parse_worktree_list
32
33local config_mod = require("wt.config")
34local resolve_url_template = config_mod.resolve_url_template
35local extract_project_name = config_mod.extract_project_name
36local load_global_config = config_mod.load_global_config
37local load_project_config = config_mod.load_project_config
38
39local hooks_mod = require("wt.hooks")
40local load_hook_permissions = hooks_mod.load_hook_permissions
41local save_hook_permissions = hooks_mod.save_hook_permissions
42local summarize_hooks = hooks_mod.summarize_hooks
43local run_hooks = hooks_mod.run_hooks
44
45local help_mod = require("wt.help")
46local print_usage = help_mod.print_usage
47local show_command_help = help_mod.show_command_help
48
49local fetch_mod = require("wt.cmd.fetch")
50local cmd_fetch = fetch_mod.cmd_fetch
51
52local list_mod = require("wt.cmd.list")
53local cmd_list = list_mod.cmd_list
54
55local remove_mod = require("wt.cmd.remove")
56local cmd_remove = remove_mod.cmd_remove
57
58local add_mod = require("wt.cmd.add")
59local cmd_add = add_mod.cmd_add
60
61local new_mod = require("wt.cmd.new")
62local cmd_new = new_mod.cmd_new
63
64local clone_mod = require("wt.cmd.clone")
65local cmd_clone = clone_mod.cmd_clone
66
67local init_mod = require("wt.cmd.init")
68local cmd_init = init_mod.cmd_init
69
70-- Main entry point
71
72local function main()
73 local command = arg[1]
74
75 if not command or command == "help" or command == "--help" or command == "-h" then
76 print_usage()
77 os.exit(EXIT_SUCCESS)
78 end
79
80 ---@cast command string
81
82 -- Collect remaining args
83 local subargs = {}
84 for i = 2, #arg do
85 table.insert(subargs, arg[i])
86 end
87
88 -- Check for --help on any command
89 if subargs[1] == "--help" or subargs[1] == "-h" then
90 show_command_help(command)
91 end
92
93 if command == "c" then
94 cmd_clone(subargs)
95 elseif command == "n" then
96 cmd_new(subargs)
97 elseif command == "a" then
98 cmd_add(subargs)
99 elseif command == "r" then
100 cmd_remove(subargs)
101 elseif command == "l" then
102 cmd_list()
103 elseif command == "f" then
104 cmd_fetch()
105 elseif command == "init" then
106 cmd_init(subargs)
107 else
108 die("unknown command: " .. command)
109 end
110end
111
112-- Export for testing when required as module
113if pcall(debug.getlocal, 4, 1) then
114 return {
115 -- URL/project parsing
116 extract_project_name = extract_project_name,
117 resolve_url_template = resolve_url_template,
118 -- Path manipulation
119 branch_to_path = branch_to_path,
120 split_path = split_path,
121 relative_path = relative_path,
122 path_inside = path_inside,
123 -- Config loading
124 load_global_config = load_global_config,
125 load_project_config = load_project_config,
126 -- Git output parsing (testable without git)
127 parse_branch_remotes = parse_branch_remotes,
128 parse_worktree_list = parse_worktree_list,
129 escape_pattern = escape_pattern,
130 -- Hook helpers (re-exported from wt.hooks)
131 summarize_hooks = summarize_hooks,
132 load_hook_permissions = load_hook_permissions,
133 save_hook_permissions = save_hook_permissions,
134 run_hooks = run_hooks,
135 -- Project root detection (re-exported from wt.git)
136 find_project_root = find_project_root,
137 detect_source_worktree = detect_source_worktree,
138 -- Command execution (for integration tests)
139 run_cmd = run_cmd,
140 run_cmd_silent = run_cmd_silent,
141 -- Exit codes (re-exported from wt.exit)
142 EXIT_SUCCESS = exit.EXIT_SUCCESS,
143 EXIT_USER_ERROR = exit.EXIT_USER_ERROR,
144 EXIT_SYSTEM_ERROR = exit.EXIT_SYSTEM_ERROR,
145 }
146end
147
148main()