heddle-cli-args 0.28.6

Heddle's command-line argument surface.
Documentation
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
// SPDX-License-Identifier: Apache-2.0
//! Shared clap Args types — one flag name, one meaning.
//!
//! Clap definitions, human help, and the machine catalog all flatten these
//! types. Behavior-contract tests below lock the shared meaning so the
//! surfaces cannot drift.

use std::path::{Path, PathBuf};

use clap::{Args, ValueEnum};

/// `--path` / `--symbol` / `--line` code scope.
///
/// `--path` names a file. `--symbol` and `--line` further pin that file;
/// either or both may be set, and both require `--path`.
#[derive(Clone, Debug, Default, Args)]
pub struct CodeScopeArgs {
    /// File path to act on.
    #[arg(long)]
    pub path: Option<String>,

    /// Anchor symbol. Requires `--path`.
    #[arg(long, requires = "path")]
    pub symbol: Option<String>,

    /// Anchor line (1-indexed). Requires `--path`. May combine with `--symbol`.
    #[arg(long, requires = "path")]
    pub line: Option<u32>,
}

impl CodeScopeArgs {
    /// True when any scope flag is set.
    pub fn is_set(&self) -> bool {
        self.path.is_some() || self.symbol.is_some() || self.line.is_some()
    }
}

/// Historical revision selector. Always `--state`.
///
/// Accepts short or full state IDs, marker names, `HEAD`, `@`, or `HEAD~N`.
/// Combined with `--path`, this is the revision to read; without `--path`,
/// it is the state-level target.
#[derive(Clone, Debug, Default, Args)]
pub struct HistoricalRevisionArgs {
    /// Historical revision or state-level target.
    #[arg(long)]
    pub state: Option<String>,
}

impl HistoricalRevisionArgs {
    pub fn as_deref(&self) -> Option<&str> {
        self.state.as_deref()
    }
}

/// Split `--path` / `--state` into a file target, a state target, and a
/// historical revision to read.
///
/// `--path` always names a file. `--state` with `--path` is the historical
/// revision of that file. `--state` alone is a state-level target.
pub fn split_path_and_revision<'a>(
    path: Option<&'a str>,
    state: Option<&'a str>,
) -> (Option<&'a str>, Option<&'a str>, Option<&'a str>) {
    match (path, state) {
        (Some(path), state) => (Some(path), None, state),
        (None, Some(state)) => (None, Some(state), None),
        (None, None) => (None, None, None),
    }
}

/// `--remote` flag with no injected default.
///
/// Resolution order (see [`RemoteChoiceArgs::requested`]): explicit flag,
/// else the configured default from `heddle remote set-default`, else an
/// actionable error. Never injects `origin`.
#[derive(Clone, Debug, Default, Args)]
pub struct RemoteChoiceArgs {
    /// Hosted remote name. Omit to use `heddle remote set-default`.
    #[arg(long)]
    pub remote: Option<String>,
}

impl RemoteChoiceArgs {
    /// Explicit `--remote` value, if the user passed one.
    pub fn requested(&self) -> Option<&str> {
        self.remote
            .as_deref()
            .map(str::trim)
            .filter(|value| !value.is_empty())
    }
}

/// Inline `--body` / `-m` or `--file` authored text.
///
/// One body-input convention across discuss/context writes. `--body` and
/// `--file` conflict.
#[derive(Clone, Debug, Default, Args)]
pub struct AuthoredMessageArgs {
    /// Inline body text.
    #[arg(short = 'm', long = "body")]
    pub body: Option<String>,

    /// Read the body from a file.
    #[arg(long, value_name = "PATH", conflicts_with = "body")]
    pub file: Option<PathBuf>,
}

impl AuthoredMessageArgs {
    pub fn is_set(&self) -> bool {
        self.body.is_some() || self.file.is_some()
    }
}

/// Hosted server choice shared by grant/promote/import/claim-style commands.
#[derive(Clone, Debug, Default, Args)]
pub struct HostedServerArgs {
    /// Hosted Heddle server. Omit when the destination is a URL, or to use the configured default.
    #[arg(long)]
    pub server: Option<String>,
}

impl HostedServerArgs {
    pub fn as_deref(&self) -> Option<&str> {
        self.server.as_deref()
    }
}

/// `--dry-run`: perform no mutation.
#[derive(Clone, Debug, Default, Args)]
pub struct DryRunArgs {
    /// Perform no mutation.
    #[arg(long)]
    pub dry_run: bool,
}

impl DryRunArgs {
    pub fn enabled(&self) -> bool {
        self.dry_run
    }
}

/// Explicit clone protocol. Failures are not retried on the other protocol.
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
pub enum CloneSourceArg {
    Git,
    Heddle,
}

impl CloneSourceArg {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Git => "git",
            Self::Heddle => "heddle",
        }
    }
}

/// Derive a destination directory from a clone source when the user omitted one.
///
/// Returns `None` when the basename is missing or unsafe (`/`, `.`, `..`,
/// empty, or still contains a path separator after stripping a trailing
/// `.git`).
pub fn safe_clone_destination_basename(remote: &str) -> Option<String> {
    let path_part = if let Some(rest) = remote
        .strip_prefix("https://")
        .or_else(|| remote.strip_prefix("http://"))
        .or_else(|| remote.strip_prefix("ssh://"))
        .or_else(|| remote.strip_prefix("git://"))
        .or_else(|| remote.strip_prefix("file://"))
    {
        let rest = rest.strip_prefix("git@").unwrap_or(rest);
        match rest.split_once('/') {
            Some((_, path)) if !path.is_empty() => path,
            _ => return None,
        }
    } else {
        match remote.find(':') {
            Some(colon_pos) => {
                let prefix = &remote[..colon_pos];
                let rest = &remote[colon_pos + 1..];
                let is_windows_drive = prefix.len() == 1
                    && prefix
                        .chars()
                        .next()
                        .is_some_and(|c| c.is_ascii_alphabetic())
                    && (rest.starts_with('\\') || rest.starts_with('/'));
                let prefix_has_separator = prefix.contains('/') || prefix.contains('\\');
                if is_windows_drive || prefix_has_separator {
                    remote
                } else if rest.is_empty() {
                    return None;
                } else {
                    rest
                }
            }
            None => remote,
        }
    };
    let is_sep = |c: char| c == '/' || c == '\\';
    let segment = path_part
        .trim_end_matches(is_sep)
        .rsplit(is_sep)
        .find(|part| !part.is_empty())
        .unwrap_or("");
    let name = segment.strip_suffix(".git").unwrap_or(segment);
    if name.is_empty()
        || name == "."
        || name == ".."
        || name == "~"
        || name == "/"
        || name.contains('/')
        || name.contains('\\')
        || name.contains('\0')
        || Path::new(name).is_absolute()
    {
        return None;
    }
    Some(name.to_string())
}

#[cfg(test)]
mod tests {
    use clap::{CommandFactory, Parser};

    use super::*;
    use crate::cli::{
        Cli, Commands, ContextCommands, DiscussCommands, ReviewCommands, ThreadCommands,
    };

    #[test]
    fn remote_choice_has_no_origin_default() {
        match Cli::try_parse_from(["heddle", "review", "approve", "feat"])
            .expect("approve without --remote")
            .command
        {
            Commands::Review {
                command: ReviewCommands::Approve(args),
            } => {
                assert!(args.remote_choice.requested().is_none());
            }
            _ => panic!("expected review approve"),
        }
        match Cli::try_parse_from([
            "heddle", "review", "approve", "feat", "--remote", "upstream",
        ])
        .expect("approve with --remote")
        .command
        {
            Commands::Review {
                command: ReviewCommands::Approve(args),
            } => {
                assert_eq!(args.remote_choice.requested(), Some("upstream"));
            }
            _ => panic!("expected review approve"),
        }
    }

    #[test]
    fn no_remote_flag_injects_origin() {
        let cmd = Cli::command();
        fn walk(command: &clap::Command) {
            for arg in command.get_arguments() {
                if arg.get_long() == Some("remote")
                    && (arg.get_short().is_some() || arg.get_long().is_some())
                    && !arg.is_positional()
                {
                    let defaults: Vec<_> = arg
                        .get_default_values()
                        .iter()
                        .map(|v| v.to_string_lossy().into_owned())
                        .collect();
                    assert!(
                        !defaults.iter().any(|v| v == "origin"),
                        "`{} --remote` must not default to origin (got {defaults:?})",
                        command.get_name()
                    );
                }
            }
            for sub in command.get_subcommands() {
                walk(sub);
            }
        }
        walk(&cmd);
    }

    #[test]
    fn scope_and_state_mean_the_same_on_discuss_context_and_blame() {
        match Cli::try_parse_from([
            "heddle",
            "discuss",
            "new",
            "--path",
            "src/auth.rs",
            "--symbol",
            "verify",
            "--line",
            "12",
            "--state",
            "HEAD~1",
            "--body",
            "why?",
        ])
        .expect("discuss new scope")
        .command
        {
            Commands::Discuss(args) => match args.command {
                DiscussCommands::New(new_args) => {
                    assert_eq!(new_args.scope.path.as_deref(), Some("src/auth.rs"));
                    assert_eq!(new_args.scope.symbol.as_deref(), Some("verify"));
                    assert_eq!(new_args.scope.line, Some(12));
                    assert_eq!(new_args.revision.state.as_deref(), Some("HEAD~1"));
                    assert_eq!(new_args.message.body.as_deref(), Some("why?"));
                }
                _ => panic!("expected discuss new"),
            },
            _ => panic!("expected discuss"),
        }

        match Cli::try_parse_from([
            "heddle",
            "context",
            "get",
            "--path",
            "src/auth.rs",
            "--symbol",
            "verify",
            "--line",
            "12",
            "--state",
            "HEAD~1",
        ])
        .expect("context get scope")
        .command
        {
            Commands::Context {
                for_thread: None,
                command: Some(ContextCommands::Get(args)),
            } => {
                assert_eq!(args.scope.path.as_deref(), Some("src/auth.rs"));
                assert_eq!(args.scope.symbol.as_deref(), Some("verify"));
                assert_eq!(args.scope.line, Some(12));
                assert_eq!(args.revision.state.as_deref(), Some("HEAD~1"));
                let (path, state_target, historical) = split_path_and_revision(
                    args.scope.path.as_deref(),
                    args.revision.state.as_deref(),
                );
                assert_eq!(path, Some("src/auth.rs"));
                assert!(state_target.is_none());
                assert_eq!(historical, Some("HEAD~1"));
            }
            _ => panic!("expected context get"),
        }

        match Cli::try_parse_from(["heddle", "blame", "src/auth.rs", "--state", "HEAD~1"])
            .expect("blame --state")
            .command
        {
            Commands::Blame(args) => {
                assert_eq!(args.path, "src/auth.rs");
                assert_eq!(args.revision.state.as_deref(), Some("HEAD~1"));
            }
            _ => panic!("expected blame"),
        }

        assert!(
            Cli::try_parse_from([
                "heddle",
                "context",
                "get",
                "--path",
                "src/auth.rs",
                "--ref",
                "HEAD",
            ])
            .is_err(),
            "historical selector is --state, not --ref"
        );
    }

    #[test]
    fn dry_run_is_the_no_mutation_flag() {
        match Cli::try_parse_from(["heddle", "undo", "--dry-run"])
            .expect("undo --dry-run")
            .command
        {
            Commands::Undo(args) => assert!(args.dry_run.enabled()),
            _ => panic!("expected undo"),
        }
        match Cli::try_parse_from(["heddle", "thread", "absorb", "child", "--dry-run"])
            .expect("absorb --dry-run")
            .command
        {
            Commands::Thread {
                command: ThreadCommands::Absorb(args),
            } => assert!(args.dry_run.enabled()),
            _ => panic!("expected absorb"),
        }
        assert!(
            Cli::try_parse_from(["heddle", "undo", "--preview"]).is_err(),
            "--preview is not a dry-run alias"
        );
        assert!(
            Cli::try_parse_from(["heddle", "thread", "absorb", "child", "--preview"]).is_err(),
            "--preview is not a dry-run alias"
        );
    }

    #[test]
    fn safe_clone_basename_is_unambiguous_or_none() {
        assert_eq!(
            safe_clone_destination_basename("https://host/acme/widgets.git").as_deref(),
            Some("widgets")
        );
        assert_eq!(
            safe_clone_destination_basename("git@host:acme/widgets.git").as_deref(),
            Some("widgets")
        );
        assert!(safe_clone_destination_basename("https://host/").is_none());
        assert!(safe_clone_destination_basename(".").is_none());
        assert!(safe_clone_destination_basename("..").is_none());
    }
}