refactor(wt): extract cmd.fetch module

Amolith created

Assisted-by: Claude Opus 4.5 via Amp

Change summary

dist/wt              | 48 +++++++++++++++++++++++++++++++--------------
src/main.lua         | 18 ++--------------
src/wt/cmd/fetch.lua | 28 ++++++++++++++++++++++++++
3 files changed, 64 insertions(+), 30 deletions(-)

Detailed changes

dist/wt 🔗

@@ -861,6 +861,36 @@ end
 return M
 ]]
 
+_EMBEDDED_MODULES["wt.cmd.fetch"] = [[-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
+--
+-- SPDX-License-Identifier: GPL-3.0-or-later
+
+local exit = require("wt.exit")
+local shell = require("wt.shell")
+local git = require("wt.git")
+
+---@class wt.cmd.fetch
+local M = {}
+
+---Fetch all remotes with pruning
+function M.cmd_fetch()
+	local root, err = git.find_project_root()
+	if not root then
+		shell.die(err --[[@as string]])
+		return
+	end
+
+	local git_dir = root .. "/.bare"
+	local output, code = shell.run_cmd("GIT_DIR=" .. git_dir .. " git fetch --all --prune")
+	io.write(output)
+	if code ~= 0 then
+		os.exit(exit.EXIT_SYSTEM_ERROR)
+	end
+end
+
+return M
+]]
+
 
 if _VERSION < "Lua 5.2" then
 	io.stderr:write("error: wt requires Lua 5.2 or later\n")
@@ -912,6 +942,9 @@ local help_mod = require("wt.help")
 local print_usage = help_mod.print_usage
 local show_command_help = help_mod.show_command_help
 
+local fetch_mod = require("wt.cmd.fetch")
+local cmd_fetch = fetch_mod.cmd_fetch
+
 ---@param args string[]
 local function cmd_clone(args)
 	-- Parse arguments: <url> [--remote name]... [--own]
@@ -1640,21 +1673,6 @@ local function cmd_list()
 	table_handle:close()
 end
 
-local function cmd_fetch()
-	local root, err = find_project_root()
-	if not root then
-		die(err --[[@as string]])
-		return
-	end
-
-	local git_dir = root .. "/.bare"
-	local output, code = run_cmd("GIT_DIR=" .. git_dir .. " git fetch --all --prune")
-	io.write(output)
-	if code ~= 0 then
-		os.exit(EXIT_SYSTEM_ERROR)
-	end
-end
-
 ---List directory entries (excluding . and ..)
 ---@param path string
 ---@return string[]

src/main.lua 🔗

@@ -54,6 +54,9 @@ local help_mod = require("wt.help")
 local print_usage = help_mod.print_usage
 local show_command_help = help_mod.show_command_help
 
+local fetch_mod = require("wt.cmd.fetch")
+local cmd_fetch = fetch_mod.cmd_fetch
+
 ---@param args string[]
 local function cmd_clone(args)
 	-- Parse arguments: <url> [--remote name]... [--own]
@@ -782,21 +785,6 @@ local function cmd_list()
 	table_handle:close()
 end
 
-local function cmd_fetch()
-	local root, err = find_project_root()
-	if not root then
-		die(err --[[@as string]])
-		return
-	end
-
-	local git_dir = root .. "/.bare"
-	local output, code = run_cmd("GIT_DIR=" .. git_dir .. " git fetch --all --prune")
-	io.write(output)
-	if code ~= 0 then
-		os.exit(EXIT_SYSTEM_ERROR)
-	end
-end
-
 ---List directory entries (excluding . and ..)
 ---@param path string
 ---@return string[]

src/wt/cmd/fetch.lua 🔗

@@ -0,0 +1,28 @@
+-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
+--
+-- SPDX-License-Identifier: GPL-3.0-or-later
+
+local exit = require("wt.exit")
+local shell = require("wt.shell")
+local git = require("wt.git")
+
+---@class wt.cmd.fetch
+local M = {}
+
+---Fetch all remotes with pruning
+function M.cmd_fetch()
+	local root, err = git.find_project_root()
+	if not root then
+		shell.die(err --[[@as string]])
+		return
+	end
+
+	local git_dir = root .. "/.bare"
+	local output, code = shell.run_cmd("GIT_DIR=" .. git_dir .. " git fetch --all --prune")
+	io.write(output)
+	if code ~= 0 then
+		os.exit(exit.EXIT_SYSTEM_ERROR)
+	end
+end
+
+return M