quinjet 0.0.40

A fast, live, keyboard-first Git source-control interface for the terminal
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
445
446
447
448
449
450
451
452
453
454
pub(crate) mod command;
mod completion;
mod package_manager;
mod pr_verbs;
mod render;
mod review;
mod update;
mod watch;

use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;

use anyhow::{Context, Result};
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::Shell;
use clap_mangen::Man;
pub(crate) use command::{Command, Outcome, Session};
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use pr_verbs::*;
use serde::Serialize;

use crate::git::diff::{DiffDocument, DiffIndex};
use crate::git::github::{
    CheckRunLog, GitHubRepository, PullRequest, PullRequestCheck, PullRequestCheckStatus,
    PullRequestCommentMode, PullRequestDiffIndex, PullRequestEdit, PullRequestLockReason,
    PullRequestMergeMethod, PullRequestMergeMode, PullRequestOperation, PullRequestReviewDecision,
    PullRequestReviewKind, PullRequestReviewOperation, PullRequestReviewSide,
    PullRequestReviewThreadSubject, PullRequestSnapshot, PullRequestUpdateMethod,
};
use crate::git::status::{Change, ChangeArea};
use crate::git::{ConflictChoice, GitOperation, LocalDiffRequest, Repository};
use crate::theme::{AppearanceChoice, ThemeName};

pub(crate) const EXIT_FAILURE: u8 = 1;
pub(crate) const EXIT_NOT_FOUND: u8 = 3;
pub(crate) const EXIT_UNAVAILABLE: u8 = 4;

const PROGRAM: &str = "quinjet";
const ROOT_HELP: &str = "Examples:
  quinjet
  quinjet status --json
  quinjet -C ../project diff --staged
  quinjet pr checks 42 --watch

Documentation: https://github.com/pulkitxm/quinjet/wiki/Command-Line";

const CHECK_WATCH_INTERVAL: u64 = 5;
const CHECK_WATCH_FLOOR: u64 = 2;
const LOG_WATCH_INTERVAL: u64 = 8;
const LOG_WATCH_FLOOR: u64 = 3;

#[derive(Debug)]
pub(crate) struct Failure {
    pub code: u8,
    pub message: String,
    pub hint: Option<String>,
}

impl Failure {
    pub(crate) fn new(code: u8, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            hint: None,
        }
    }

    pub(crate) fn hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(hint.into());
        self
    }
}

impl std::fmt::Display for Failure {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}", self.message)
    }
}

impl std::error::Error for Failure {}

pub(crate) struct TerminalOptions {
    pub path: PathBuf,
    pub no_mouse: bool,
    pub webhook_listen: Option<String>,
    pub theme: ThemeName,
    pub appearance: AppearanceChoice,
    pub pull_request: Option<u64>,
}

#[expect(
    variant_size_differences,
    reason = "the terminal options are already boxed; the only way to level this is to box one byte"
)]
pub(crate) enum Launch {
    Terminal(Box<TerminalOptions>),
    Finished(u8),
}

#[derive(Debug, Parser)]
#[command(name = "quinjet", version, about)]
#[command(subcommand_negates_reqs = true)]
#[command(propagate_version = true)]
#[command(after_long_help = ROOT_HELP)]
struct Cli {
    #[command(subcommand)]
    command: Option<Verb>,

    /// Repository to run a subcommand against
    #[arg(
        short = 'C',
        long = "path",
        value_name = "DIR",
        default_value = ".",
        global = true,
        value_hint = ValueHint::DirPath
    )]
    repository: PathBuf,

    /// Print one JSON document on stdout instead of text
    #[arg(long, global = true)]
    json: bool,

    /// Open the terminal interface focused on this pull request
    #[arg(long = "pr", value_name = "NUMBER")]
    pull_request: Option<u64>,
}

#[derive(Debug, Subcommand)]
enum Verb {
    /// Open the terminal interface
    Tui(TuiArgs),
    /// Show the working tree, the index and the branch
    Status(WatchableArgs),
    /// Print the working-tree diff
    Diff(DiffArgs),
    /// Stage paths, or everything
    Stage(SelectionArgs),
    /// Unstage paths, or everything
    Unstage(SelectionArgs),
    /// Throw away changes to paths
    Discard(DiscardArgs),
    /// Delete paths from the working tree and the index
    #[command(visible_alias = "rm")]
    Remove(RemoveArgs),
    /// Record the staged changes
    Commit(CommitArgs),
    /// Fetch every remote and prune deleted refs
    Fetch,
    /// Pull the current branch
    Pull,
    /// Push the current branch
    Push,
    /// Pull, then push
    Sync,
    /// List commits
    Log(LogArgs),
    /// Show one commit and its patch
    Show(ShowArgs),
    /// Work with branches
    Branch {
        #[command(subcommand)]
        command: BranchVerb,
    },
    /// Work with stashes
    Stash {
        #[command(subcommand)]
        command: StashVerb,
    },
    /// List linked worktrees
    Worktree {
        #[command(subcommand)]
        command: WorktreeVerb,
    },
    /// Apply a commit onto the current branch
    CherryPick(RevisionArgs),
    /// Record a commit that undoes another
    Revert(RevisionArgs),
    /// Take one side of a merge conflict
    Resolve(ResolveArgs),
    /// List the GitHub repositories this checkout points at
    Repos(ReposArgs),
    /// Read or update a pull request
    Pr {
        #[command(subcommand)]
        command: PrVerb,
    },
    /// Print or install shell completions
    #[command(visible_alias = "completion")]
    Completions(CompletionsArgs),
    /// Print the manual page, or write one page per command
    Man(ManArgs),
    /// Describe commands and arguments for automation
    Capabilities,
    /// Update this executable to the latest stable release
    Update(UpdateArgs),
}

impl Verb {
    const fn progress_label(&self) -> Option<&'static str> {
        match self {
            Self::Tui(_) | Self::Completions(_) | Self::Man(_) | Self::Capabilities => None,
            Self::Status(args) if args.watch => None,
            Self::Status(_) => Some("Reading repository status"),
            Self::Diff(_) => Some("Loading the working-tree diff"),
            Self::Stage(_) => Some("Staging changes"),
            Self::Unstage(_) => Some("Unstaging changes"),
            Self::Discard(_) => Some("Reading changes to discard"),
            Self::Remove(_) => Some("Reading files to remove"),
            Self::Commit(_) => Some("Creating commit"),
            Self::Fetch => Some("Fetching remotes"),
            Self::Pull => Some("Pulling changes"),
            Self::Push => Some("Pushing changes"),
            Self::Sync => Some("Synchronizing changes"),
            Self::Log(_) => Some("Reading commit history"),
            Self::Show(_) => Some("Loading commit patch"),
            Self::Branch { .. } => Some("Reading branch state"),
            Self::Stash { .. } => Some("Reading stash state"),
            Self::Worktree { .. } => Some("Reading worktrees"),
            Self::CherryPick(_) => Some("Resolving commit to cherry-pick"),
            Self::Revert(_) => Some("Resolving commit to revert"),
            Self::Resolve(_) => Some("Resolving conflict"),
            Self::Repos(_) => Some("Discovering GitHub repositories"),
            Self::Pr {
                command: PrVerb::View(args) | PrVerb::Conversation(args),
            } if args.watch => None,
            Self::Pr {
                command: PrVerb::Checks(args),
            } if args.watch => None,
            Self::Pr {
                command: PrVerb::Logs(args),
            } if args.watch => None,
            Self::Pr {
                command: PrVerb::Merge(_) | PrVerb::Close(_) | PrVerb::Reopen(_),
            } => Some("Updating pull request"),
            Self::Pr {
                command: PrVerb::Reviews { .. },
            } => Some("Updating pull-request review"),
            Self::Pr { .. } => Some("Loading pull request"),
            Self::Update(_) => Some("Checking for updates"),
        }
    }
}

#[derive(Debug, Args)]
struct CompletionsArgs {
    /// Shell to write or install a completion script for
    #[arg(value_enum, required_unless_present = "install")]
    shell: Option<Shell>,
    /// Install completions and a q launcher on PATH
    #[arg(long)]
    install: bool,
    #[arg(long, hide = true, requires = "install")]
    automatic: bool,
}

#[derive(Debug, Args)]
struct ManArgs {
    /// Write one page per command into this directory instead of printing one
    #[arg(long, value_name = "DIR", value_hint = ValueHint::DirPath)]
    dir: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct UpdateArgs {
    /// Check for a newer release without installing it
    #[arg(long)]
    check: bool,
}

#[derive(Debug, Args)]
struct TuiArgs {
    /// Git repository to open
    #[arg(default_value = ".", value_hint = ValueHint::DirPath)]
    path: PathBuf,
    /// Disable mouse capture
    #[arg(long)]
    no_mouse: bool,
    /// Listen for forwarded GitHub webhooks on a port or host:port
    #[arg(long, value_name = "ADDRESS")]
    webhook_listen: Option<String>,
    /// Color palette to use throughout the interface
    #[arg(long, value_enum, default_value_t)]
    theme: ThemeName,
    /// Use the system, light, or dark variant of the palette
    #[arg(long, value_enum, default_value_t)]
    appearance: AppearanceChoice,
    /// Open the interface focused on this pull request
    #[arg(long = "pr", value_name = "NUMBER")]
    pull_request: Option<u64>,
}

#[derive(Debug, Args)]
struct WatchableArgs {
    /// Keep the reading on screen and refresh it
    #[arg(long)]
    watch: bool,
    /// Seconds between refreshes
    #[arg(
        long,
        value_name = "SECONDS",
        default_value_t = 2,
        requires = "watch",
        value_parser = clap::value_parser!(u64).range(1..),
        value_hint = ValueHint::Other
    )]
    interval: u64,
}

#[derive(Debug, Args)]
struct DiffArgs {
    /// Limit the diff to these paths
    #[arg(value_name = "PATH", value_hint = ValueHint::AnyPath)]
    paths: Vec<PathBuf>,
    /// Only what is staged
    #[arg(long)]
    staged: bool,
    /// Only what is not staged
    #[arg(long, conflicts_with = "staged")]
    unstaged: bool,
    /// Print whole files instead of three lines of context
    #[arg(long)]
    expanded: bool,
}

#[derive(Debug, Args)]
#[group(required = true, multiple = false)]
struct SelectionArgs {
    /// Paths to act on
    #[arg(value_name = "PATH", value_hint = ValueHint::AnyPath)]
    paths: Vec<PathBuf>,
    /// Act on every change instead
    #[arg(long, conflicts_with = "paths")]
    all: bool,
}

#[derive(Debug, Args)]
struct DiscardArgs {
    #[command(flatten)]
    selection: SelectionArgs,
    /// Confirm; without it the command reports what it would discard
    #[arg(long)]
    yes: bool,
}

#[derive(Debug, Args)]
struct RemoveArgs {
    #[command(flatten)]
    selection: SelectionArgs,
    /// Confirm; without it the command reports what it would remove
    #[arg(long)]
    yes: bool,
}

#[derive(Debug, Args)]
struct CommitArgs {
    /// Commit message
    #[arg(short, long)]
    message: String,
    /// Replace the previous commit
    #[arg(long)]
    amend: bool,
}

#[derive(Debug, Args)]
struct LogArgs {
    /// Branch, tag, or commit to read from
    #[arg(default_value = "HEAD", value_name = "REVISION", value_hint = ValueHint::Other)]
    revision: String,
    /// Commits to skip
    #[arg(long, default_value_t = 0, value_hint = ValueHint::Other)]
    skip: usize,
    /// Commits to print
    #[arg(long, short = 'n', default_value_t = 30, value_hint = ValueHint::Other)]
    limit: usize,
}

#[derive(Debug, Args)]
struct ShowArgs {
    /// Commit to show
    #[arg(default_value = "HEAD", value_name = "REVISION", value_hint = ValueHint::Other)]
    revision: String,
    /// Print whole files instead of three lines of context
    #[arg(long)]
    expanded: bool,
}

#[derive(Debug, Args)]
struct RevisionArgs {
    /// Commit to act on
    #[arg(value_name = "REVISION", value_hint = ValueHint::Other)]
    revision: String,
    /// Confirm; without it the command reports what it would do
    #[arg(long)]
    yes: bool,
}

#[derive(Debug, Args)]
struct ResolveArgs {
    /// Conflicted path
    #[arg(value_name = "PATH", value_hint = ValueHint::AnyPath)]
    path: PathBuf,
    #[command(flatten)]
    side: ConflictSide,
}

#[derive(Debug, Args)]
#[group(required = true, multiple = false)]
struct ConflictSide {
    /// Keep the version already on this branch
    #[arg(long)]
    ours: bool,
    /// Keep the version being merged in
    #[arg(long)]
    theirs: bool,
    /// Accept the file as it stands and stage it
    #[arg(long)]
    stage: bool,
}

mod entry;
mod output;
mod pull_request;
mod repository;
pub(crate) mod support;
mod verbs;

pub(crate) use entry::dispatch;
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use output::*;
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use pull_request::*;
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use repository::*;
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use review::*;
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use support::*;
pub(crate) use support::{open_url, report, stdout_is_terminal};
#[cfg_attr(not(test), expect(clippy::wildcard_imports, reason = "shared"))]
use verbs::*;

#[cfg(test)]
#[expect(
    unused_results,
    reason = "test helpers return values the assertions do not use"
)]
mod tests;