stakk 1.11.0

A CLI tool that bridges Jujutsu (jj) bookmarks to GitHub stacked pull requests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
pub mod auth;
pub mod graph;
pub mod submit;

use std::path::PathBuf;

use clap::Args;
use clap::Command;
use clap::Parser;
use clap::Subcommand;
use clap_complete::Shell;

use crate::cli::auth::AuthArgs;
use crate::cli::graph::GraphArgs;
use crate::cli::submit::SubmitArgs;
use crate::config::Config;

/// stakk — bridge Jujutsu bookmarks to GitHub stacked pull requests.
#[derive(Debug, Parser)]
#[command(version, about, after_long_help = env!("CARGO_PKG_REPOSITORY"))]
#[command(args_conflicts_with_subcommands = true)]
pub struct Cli {
    /// Path to a config file (overrides automatic discovery).
    ///
    /// The file is loaded in place of the repo-level stakk.toml;
    /// user-level config is still merged unless inherit = false.
    // Implementation note: this arg exists for --help discoverability only.
    // Config is loaded *before* clap parsing (so config values can be injected
    // as clap defaults), which means clap's parsed value arrives too late.
    // The actual path is resolved by `config::pre_parse_config_path()` from
    // raw `std::env::args()` / `STAKK_CONFIG`.
    #[arg(long, global = true, env = "STAKK_CONFIG", verbatim_doc_comment)]
    pub config: Option<PathBuf>,

    #[command(subcommand)]
    pub command: Option<Commands>,
    /// Default submit arguments (used when no subcommand is given).
    #[command(flatten)]
    pub submit_args: SubmitArgs,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Submit bookmarks as GitHub pull requests (default when no command
    /// given).
    Submit(SubmitArgs),
    /// Manage authentication.
    Auth(AuthArgs),
    /// Show repository status and bookmark stacks.
    Show(ShowArgs),
    /// Generate shell completions for the given shell.
    Completions {
        /// The shell to generate completions for.
        shell: Shell,
    },
}

/// Arguments for the show subcommand.
#[derive(Debug, Args)]
pub struct ShowArgs {
    #[command(flatten)]
    pub graph: GraphArgs,
}

/// Apply config-file defaults to clap's `Command` before parsing.
///
/// This mutates argument default values so they appear in `--help` and
/// take effect when the user does not pass the corresponding flag.
#[expect(
    clippy::needless_pass_by_value,
    reason = "Config is moved into closures captured by mut_subcommand which requires 'static"
)]
pub fn apply_config_defaults(config: Config, cmd: Command) -> Command {
    // Apply to top-level (flattened submit args) first.
    let cmd = apply_submit_and_graph_defaults(&config, cmd);
    // Clone for the closures that mut_subcommand requires ('static).
    let config2 = config.clone();
    let cmd = cmd.mut_subcommand("submit", |sub| {
        apply_submit_and_graph_defaults(&config, sub)
    });
    cmd.mut_subcommand("show", |sub| apply_graph_defaults(&config2, sub))
}

fn set_default(cmd: Command, arg_id: &str, value: &str) -> Command {
    // Leak the value so clap can store it as a `'static` default. This is
    // acceptable because the CLI runs once and exits — the leaked count is
    // bounded by the number of config fields.
    let leaked: &'static str = Box::leak(value.to_string().into_boxed_str());
    cmd.mut_arg(arg_id, |a| a.default_value(leaked))
}

fn apply_submit_defaults(config: &Config, mut cmd: Command) -> Command {
    if let Some(ref remote) = config.remote {
        cmd = set_default(cmd, "remote", remote);
    }
    if let Some(pr_mode) = config.pr_mode {
        cmd = set_default(cmd, "pr_mode", &pr_mode.to_string());
    }
    if let Some(ref template) = config.template {
        cmd = set_default(cmd, "template", template);
    }
    if let Some(sp) = config.stack_placement {
        cmd = set_default(cmd, "stack_placement", &sp.to_string());
    }
    if let Some(ref ap) = config.auto_prefix {
        cmd = set_default(cmd, "auto_prefix", ap);
    }
    if let Some(ref bc) = config.bookmark_command {
        cmd = set_default(cmd, "bookmark_command", bc);
    }
    cmd
}

fn apply_graph_defaults(config: &Config, mut cmd: Command) -> Command {
    if let Some(ref br) = config.bookmarks_revset {
        cmd = set_default(cmd, "bookmarks_revset", br);
    }
    if let Some(ref hr) = config.heads_revset {
        cmd = set_default(cmd, "heads_revset", hr);
    }
    cmd
}

fn apply_submit_and_graph_defaults(config: &Config, cmd: Command) -> Command {
    let cmd = apply_submit_defaults(config, cmd);
    apply_graph_defaults(config, cmd)
}

#[cfg(test)]
mod tests {
    use clap::CommandFactory;
    use clap::FromArgMatches;

    use super::*;
    use crate::forge::comment::StackPlacement;

    /// Parse CLI args with the given config applied, returning the `Cli`.
    fn parse_with_config(config: Config, args: &[&str]) -> Cli {
        let cmd = apply_config_defaults(config, Cli::command());
        let matches = cmd.get_matches_from(args);
        Cli::from_arg_matches(&matches).unwrap()
    }

    /// Extract `SubmitArgs` from parsed CLI (handles both top-level and
    /// subcommand).
    fn submit_args(cli: &Cli) -> &SubmitArgs {
        match &cli.command {
            Some(Commands::Submit(args)) => args,
            _ => &cli.submit_args,
        }
    }

    // -- pr_mode tests --

    use crate::cli::submit::PrMode;

    #[test]
    fn pr_mode_default_no_config() {
        let cli = parse_with_config(Config::default(), &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Regular);
    }

    #[test]
    fn pr_mode_config_draft_no_flag() {
        let config = Config {
            pr_mode: Some(PrMode::Draft),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Draft);
    }

    #[test]
    fn pr_mode_config_regular_no_flag() {
        let config = Config {
            pr_mode: Some(PrMode::Regular),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Regular);
    }

    #[test]
    fn pr_mode_config_regular_cli_draft() {
        let config = Config {
            pr_mode: Some(PrMode::Regular),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "--draft", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Draft);
    }

    #[test]
    fn pr_mode_cli_overrides_config() {
        let config = Config {
            pr_mode: Some(PrMode::Draft),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "--pr-mode", "regular", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Regular);
    }

    #[test]
    fn pr_mode_no_config_cli_draft_flag() {
        let cli = parse_with_config(Config::default(), &["stakk", "submit", "--draft", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Draft);
    }

    #[test]
    fn pr_mode_draft_flag_overrides_pr_mode_regular() {
        let cli = parse_with_config(
            Config::default(),
            &["stakk", "submit", "--pr-mode", "regular", "--draft", "bm"],
        );
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Draft);
    }

    // -- pr_mode top-level (no subcommand) --

    #[test]
    fn pr_mode_toplevel_config_draft() {
        let config = Config {
            pr_mode: Some(PrMode::Draft),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Draft);
    }

    #[test]
    fn pr_mode_toplevel_cli_draft_flag() {
        let cli = parse_with_config(Config::default(), &["stakk", "--draft", "bm"]);
        assert_eq!(submit_args(&cli).pr_mode(), PrMode::Draft);
    }

    // -- remote tests --

    #[test]
    fn remote_default_no_config() {
        let cli = parse_with_config(Config::default(), &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).remote, "origin");
    }

    #[test]
    fn remote_config_override() {
        let config = Config {
            remote: Some("upstream".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).remote, "upstream");
    }

    #[test]
    fn remote_cli_overrides_config() {
        let config = Config {
            remote: Some("upstream".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "--remote", "other", "bm"]);
        assert_eq!(submit_args(&cli).remote, "other");
    }

    // -- stack_placement tests --

    #[test]
    fn stack_placement_default_no_config() {
        let cli = parse_with_config(Config::default(), &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).stack_placement, StackPlacement::Comment);
    }

    #[test]
    fn stack_placement_config_body() {
        let config = Config {
            stack_placement: Some(StackPlacement::Body),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).stack_placement, StackPlacement::Body);
    }

    #[test]
    fn stack_placement_cli_overrides_config() {
        let config = Config {
            stack_placement: Some(StackPlacement::Body),
            ..Default::default()
        };
        let cli = parse_with_config(
            config,
            &["stakk", "submit", "--stack-placement", "comment", "bm"],
        );
        assert_eq!(submit_args(&cli).stack_placement, StackPlacement::Comment);
    }

    // -- auto_prefix tests --

    #[test]
    fn auto_prefix_config_override() {
        let config = Config {
            auto_prefix: Some("gb-".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).auto_prefix.as_deref(), Some("gb-"));
    }

    #[test]
    fn auto_prefix_cli_overrides_config() {
        let config = Config {
            auto_prefix: Some("gb-".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "--auto-prefix", "xx-", "bm"]);
        assert_eq!(submit_args(&cli).auto_prefix.as_deref(), Some("xx-"));
    }

    // -- graph revset tests --

    #[test]
    fn bookmarks_revset_config_override() {
        let config = Config {
            bookmarks_revset: Some("all()".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).graph.bookmarks_revset, "all()");
    }

    #[test]
    fn heads_revset_config_override() {
        let config = Config {
            heads_revset: Some("heads(all())".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "submit", "bm"]);
        assert_eq!(submit_args(&cli).graph.heads_revset, "heads(all())");
    }

    #[test]
    fn revset_cli_overrides_config() {
        let config = Config {
            bookmarks_revset: Some("all()".into()),
            ..Default::default()
        };
        let cli = parse_with_config(
            config,
            &["stakk", "submit", "--bookmarks-revset", "mine()", "bm"],
        );
        assert_eq!(submit_args(&cli).graph.bookmarks_revset, "mine()");
    }

    // -- show subcommand gets graph defaults --

    #[test]
    fn show_inherits_graph_defaults() {
        let config = Config {
            bookmarks_revset: Some("custom()".into()),
            heads_revset: Some("heads(custom())".into()),
            ..Default::default()
        };
        let cli = parse_with_config(config, &["stakk", "show"]);
        match &cli.command {
            Some(Commands::Show(args)) => {
                assert_eq!(args.graph.bookmarks_revset, "custom()");
                assert_eq!(args.graph.heads_revset, "heads(custom())");
            }
            other => panic!("expected Show, got {other:?}"),
        }
    }

    // -- env var interaction --

    #[test]
    fn env_var_overrides_config() {
        // env vars are set per-process, so this test just verifies the
        // precedence: CLI > env > config > hardcoded default.
        // We can't easily test env vars in unit tests without side effects,
        // so this test documents the expected clap precedence.
        let config = Config {
            remote: Some("from-config".into()),
            ..Default::default()
        };
        // CLI flag should override config.
        let cli = parse_with_config(config, &["stakk", "submit", "--remote", "from-cli", "bm"]);
        assert_eq!(submit_args(&cli).remote, "from-cli");
    }

    // -- TOML parsing --

    #[test]
    fn toml_deserialize_full() {
        let toml_str = r#"
remote = "upstream"
pr_mode = "draft"
template = "/path/to/template.jinja"
stack_placement = "body"
auto_prefix = "gb-"
bookmark_command = "my-command"
bookmarks_revset = "all()"
heads_revset = "heads(all())"
"#;
        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.remote.as_deref(), Some("upstream"));
        assert_eq!(config.pr_mode, Some(PrMode::Draft));
        assert_eq!(config.template.as_deref(), Some("/path/to/template.jinja"));
        assert_eq!(config.stack_placement, Some(StackPlacement::Body));
        assert_eq!(config.auto_prefix.as_deref(), Some("gb-"));
        assert_eq!(config.bookmark_command.as_deref(), Some("my-command"));
        assert_eq!(config.bookmarks_revset.as_deref(), Some("all()"));
        assert_eq!(config.heads_revset.as_deref(), Some("heads(all())"));
    }

    #[test]
    fn toml_deserialize_empty() {
        let config: Config = toml::from_str("").unwrap();
        assert!(config.remote.is_none());
        assert!(config.pr_mode.is_none());
    }

    #[test]
    fn toml_deserialize_partial() {
        let config: Config = toml::from_str(r#"pr_mode = "regular""#).unwrap();
        assert_eq!(config.pr_mode, Some(PrMode::Regular));
        assert!(config.remote.is_none());
    }

    #[test]
    fn toml_rejects_unknown_field() {
        let result: Result<Config, _> = toml::from_str("bogus = 42");
        assert!(result.is_err());
    }

    #[test]
    fn toml_stack_placement_kebab_case() {
        let config: Config = toml::from_str(r#"stack_placement = "comment""#).unwrap();
        assert_eq!(config.stack_placement, Some(StackPlacement::Comment));
    }

    #[test]
    fn toml_stack_placement_invalid() {
        let result: Result<Config, _> = toml::from_str(r#"stack_placement = "invalid""#);
        assert!(result.is_err());
    }
}