@@ -2,6 +2,7 @@ import { existsSync, readFileSync, statSync } from "node:fs";
22import { dirname , resolve } from "node:path" ;
33import { Command } from "commander" ;
44import type {
5+ CliInput ,
56 CommonOptions ,
67 HelpCommandInput ,
78 LayoutMode ,
@@ -398,6 +399,24 @@ async function parseDifftoolCommand(tokens: string[], argv: string[]): Promise<P
398399 } ;
399400}
400401
402+ function requireReloadableCliInput ( input : ParsedCliInput ) : CliInput {
403+ if ( input . kind === "help" || input . kind === "pager" || input . kind === "mcp-serve" ) {
404+ throw new Error (
405+ "Session reload requires a Hunk review command after --, such as `diff` or `show`." ,
406+ ) ;
407+ }
408+
409+ if ( input . kind === "session" ) {
410+ throw new Error ( "Session reload cannot invoke another session command." ) ;
411+ }
412+
413+ if ( input . kind === "patch" && ( ! input . file || input . file === "-" ) ) {
414+ throw new Error ( "Session reload does not support `patch -` or stdin-backed patch input." ) ;
415+ }
416+
417+ return input ;
418+ }
419+
401420/** Parse `hunk session ...` as live-session daemon-backed commands. */
402421async function parseSessionCommand ( tokens : string [ ] ) : Promise < ParsedCliInput > {
403422 const [ subcommand , ...rest ] = tokens ;
@@ -417,6 +436,8 @@ async function parseSessionCommand(tokens: string[]): Promise<ParsedCliInput> {
417436 " hunk session context <session-id>" ,
418437 " hunk session context --repo <path>" ,
419438 " hunk session navigate <session-id> --file <path> (--hunk <n> | --old-line <n> | --new-line <n>)" ,
439+ " hunk session reload <session-id> -- diff [ref] [-- <pathspec...>]" ,
440+ " hunk session reload <session-id> -- show [ref] [-- <pathspec...>]" ,
420441 " hunk session comment add <session-id> --file <path> (--old-line <n> | --new-line <n>) --summary <text>" ,
421442 " hunk session comment list <session-id>" ,
422443 " hunk session comment rm <session-id> <comment-id>" ,
@@ -551,6 +572,64 @@ async function parseSessionCommand(tokens: string[]): Promise<ParsedCliInput> {
551572 } ;
552573 }
553574
575+ if ( subcommand === "reload" ) {
576+ const separatorIndex = rest . indexOf ( "--" ) ;
577+ const outerTokens = separatorIndex === - 1 ? rest : rest . slice ( 0 , separatorIndex ) ;
578+
579+ const command = new Command ( "session reload" )
580+ . description ( "replace the contents of one live Hunk session" )
581+ . argument ( "[sessionId]" )
582+ . option ( "--repo <path>" , "target the live session whose repo root matches this path" )
583+ . option ( "--json" , "emit structured JSON" ) ;
584+
585+ let parsedSessionId : string | undefined ;
586+ let parsedOptions : { repo ?: string ; json ?: boolean } = { } ;
587+
588+ command . action ( ( sessionId : string | undefined , options : { repo ?: string ; json ?: boolean } ) => {
589+ parsedSessionId = sessionId ;
590+ parsedOptions = options ;
591+ } ) ;
592+
593+ if ( outerTokens . includes ( "--help" ) || outerTokens . includes ( "-h" ) ) {
594+ return {
595+ kind : "help" ,
596+ text :
597+ `${ command . helpInformation ( ) . trimEnd ( ) } \n\n` +
598+ [
599+ "Examples:" ,
600+ " hunk session reload --repo . -- diff" ,
601+ " hunk session reload --repo . -- diff main...feature -- src/ui" ,
602+ " hunk session reload --repo . -- show HEAD~1 -- README.md" ,
603+ ] . join ( "\n" ) +
604+ "\n" ,
605+ } ;
606+ }
607+
608+ if ( separatorIndex === - 1 ) {
609+ throw new Error (
610+ "Pass the replacement Hunk command after `--`, for example `hunk session reload <session-id> -- diff`." ,
611+ ) ;
612+ }
613+
614+ const nestedTokens = rest . slice ( separatorIndex + 1 ) ;
615+ if ( nestedTokens . length === 0 ) {
616+ throw new Error (
617+ "Pass the replacement Hunk command after `--`, for example `hunk session reload <session-id> -- diff`." ,
618+ ) ;
619+ }
620+
621+ await parseStandaloneCommand ( command , outerTokens ) ;
622+ const nextInput = requireReloadableCliInput ( await parseCli ( [ "bun" , "hunk" , ...nestedTokens ] ) ) ;
623+
624+ return {
625+ kind : "session" ,
626+ action : "reload" ,
627+ output : resolveJsonOutput ( parsedOptions ) ,
628+ selector : resolveExplicitSessionSelector ( parsedSessionId , parsedOptions . repo ) ,
629+ nextInput,
630+ } ;
631+ }
632+
554633 if ( subcommand === "comment" ) {
555634 const [ commentSubcommand , ...commentRest ] = rest ;
556635 if ( ! commentSubcommand || commentSubcommand === "--help" || commentSubcommand === "-h" ) {
0 commit comments