nils-git-cli 0.7.3

CLI crate for nils-git-cli in the nils-cli workspace.
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
use crate::common;
use common::{GitCliHarness, init_bare_remote, init_repo};
use nils_test_support::cmd::{CmdOutput, run_with};
use nils_test_support::git::{commit_file, git};
use std::fs;
use std::path::Path;

fn run_with_stdin(harness: &GitCliHarness, cwd: &Path, args: &[&str], stdin: &str) -> CmdOutput {
    let options = harness.cmd_options(cwd).with_stdin_str(stdin);
    run_with(&harness.git_cli_bin(), args, &options)
}

fn setup_repo_with_two_commits() -> tempfile::TempDir {
    let dir = init_repo();
    commit_file(dir.path(), "second.txt", "two\n", "second");
    dir
}

fn setup_repo_with_three_commits() -> tempfile::TempDir {
    let dir = init_repo();
    commit_file(dir.path(), "second.txt", "two\n", "second");
    commit_file(dir.path(), "third.txt", "three\n", "third");
    dir
}

fn git_path(dir: &Path, path: &str) -> std::path::PathBuf {
    let raw = git(dir, &["rev-parse", "--git-path", path]);
    std::path::PathBuf::from(raw.trim_end_matches(['\n', '\r']))
}

#[test]
fn reset_soft_abort_prints_prompt_and_aborts() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();

    let output = run_with_stdin(&harness, dir.path(), &["reset", "soft", "1"], "n\n");

    assert_eq!(output.code, 1);
    assert!(output.stdout_text().contains("๐Ÿงพ Commits to be rewound:"));
    assert!(output.stdout_text().contains("๐Ÿšซ Aborted"));
    assert_eq!(output.stderr_text(), "");
}

#[test]
fn reset_mixed_invalid_count_errors() {
    let harness = GitCliHarness::new();
    let dir = tempfile::TempDir::new().expect("tempdir");

    let output = harness.run(dir.path(), &["reset", "mixed", "0"]);

    assert_eq!(output.code, 2);
    assert!(
        output
            .stderr_text()
            .contains("โŒ Invalid commit count: 0 (must be a positive integer).")
    );
}

#[test]
fn reset_soft_insufficient_commits_errors() {
    let harness = GitCliHarness::new();
    let dir = init_repo();

    let output = harness.run(dir.path(), &["reset", "soft", "2"]);

    assert_eq!(output.code, 1);
    assert_eq!(output.stdout_text(), "");
    assert!(
        output
            .stderr_text()
            .contains("โŒ Cannot resolve HEAD~2 (not enough commits?).")
    );
}

#[test]
fn reset_hard_confirm_succeeds() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();

    let output = run_with_stdin(&harness, dir.path(), &["reset", "hard", "1"], "y\n");

    assert_eq!(output.code, 0);
    assert!(
        output
            .stdout_text()
            .contains("โ“ Are you absolutely sure? [y/N] ")
    );
    assert!(
        output
            .stdout_text()
            .contains("โœ… Hard reset completed. HEAD moved back to HEAD~1.")
    );
}

#[test]
fn reset_undo_dirty_tree_default_aborts() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_three_commits();
    git(dir.path(), &["reset", "--soft", "HEAD~1"]);

    let output = run_with_stdin(&harness, dir.path(), &["reset", "undo"], "\n");

    assert_eq!(output.code, 1);
    assert!(output.stdout_text().contains("Choose how to proceed:"));
    assert!(output.stdout_text().contains("๐Ÿšซ Aborted"));
}

#[test]
fn reset_undo_no_reflog_entry_errors() {
    let harness = GitCliHarness::new();
    let dir = tempfile::TempDir::new().expect("tempdir");
    git(dir.path(), &["init", "-q"]);

    let output = harness.run(dir.path(), &["reset", "undo"]);

    assert_eq!(output.code, 1);
    assert!(
        output
            .stdout_text()
            .contains("โŒ Cannot resolve HEAD@{1} (no previous HEAD position in reflog).")
    );
}

#[test]
fn reset_undo_clean_tree_fast_path() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();
    git(dir.path(), &["reset", "--hard", "HEAD~1"]);

    let output = harness.run(dir.path(), &["reset", "undo"]);

    assert_eq!(output.code, 0);
    assert!(
        output
            .stdout_text()
            .contains("โœ… Working tree clean. Proceeding with: git reset --hard ")
    );
    assert!(
        output
            .stdout_text()
            .contains("โœ… Repository reset back to previous HEAD: ")
    );
}

#[test]
fn reset_undo_dirty_choice_soft_succeeds() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_three_commits();
    git(dir.path(), &["reset", "--soft", "HEAD~1"]);

    let output = run_with_stdin(&harness, dir.path(), &["reset", "undo"], "1\n");

    assert_eq!(output.code, 0);
    assert!(
        output
            .stdout_text()
            .contains("๐Ÿงท Preserving INDEX (staged) and working tree. Running: git reset --soft ")
    );
    assert!(
        output
            .stdout_text()
            .contains("โœ… HEAD moved back while preserving index + working tree: ")
    );
}

#[test]
fn reset_undo_dirty_choice_mixed_succeeds() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_three_commits();
    git(dir.path(), &["reset", "--soft", "HEAD~1"]);

    let output = run_with_stdin(&harness, dir.path(), &["reset", "undo"], "2\n");

    assert_eq!(output.code, 0);
    assert!(output.stdout_text().contains(
        "๐Ÿงท Preserving working tree but clearing INDEX (unstage all). Running: git reset --mixed "
    ));
    assert!(
        output
            .stdout_text()
            .contains("โœ… HEAD moved back; working tree preserved; index reset: ")
    );
}

#[test]
fn reset_undo_dirty_choice_hard_decline_aborts() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_three_commits();
    git(dir.path(), &["reset", "--soft", "HEAD~1"]);

    let output = run_with_stdin(&harness, dir.path(), &["reset", "undo"], "3\nn\n");

    assert_eq!(output.code, 1);
    assert!(
        output
            .stdout_text()
            .contains("โ“ Are you absolutely sure? [y/N] ")
    );
    assert!(output.stdout_text().contains("๐Ÿšซ Aborted"));
}

#[test]
fn reset_back_head_abort() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();

    let output = run_with_stdin(&harness, dir.path(), &["reset", "back-head"], "n\n");

    assert_eq!(output.code, 1);
    assert!(
        output
            .stdout_text()
            .contains("โ“ Proceed with 'git checkout HEAD@{1}'? [y/N] ")
    );
    assert!(output.stdout_text().contains("๐Ÿšซ Aborted"));
}

#[test]
fn reset_back_head_checkout_failure_prints_error() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();
    fs::write(dir.path().join("second.txt"), "conflict\n").expect("write dirty change");

    let output = run_with_stdin(&harness, dir.path(), &["reset", "back-head"], "y\n");

    assert_eq!(output.code, 1);
    assert!(
        output
            .stdout_text()
            .contains("โŒ Checkout failed (likely due to local changes or invalid reflog state).")
    );
}

#[test]
fn reset_back_checkout_detached_head_refuses() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();
    git(dir.path(), &["checkout", "HEAD~1"]);

    let output = harness.run(dir.path(), &["reset", "back-checkout"]);

    assert_eq!(output.code, 1);
    assert!(output.stdout_text().contains(
        "โŒ You are in a detached HEAD state. This function targets branch-to-branch checkouts."
    ));
}

#[test]
fn reset_back_checkout_missing_reflog_entry_errors() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();
    let logs_head = git_path(dir.path(), "logs/HEAD");
    if logs_head.exists() {
        fs::remove_file(&logs_head).expect("remove reflog");
    }

    let output = harness.run(dir.path(), &["reset", "back-checkout"]);

    assert_eq!(output.code, 1);
    assert!(
        output
            .stdout_text()
            .contains("โŒ Could not find a previous checkout that switched to main.")
    );
}

#[test]
fn reset_back_checkout_sha_like_from_refuses() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();
    git(dir.path(), &["checkout", "HEAD~1"]);
    git(dir.path(), &["checkout", "main"]);

    let output = harness.run(dir.path(), &["reset", "back-checkout"]);

    assert_eq!(output.code, 1);
    assert!(
        output
            .stdout_text()
            .contains("โŒ Previous 'from' looks like a commit SHA")
    );
}

#[test]
fn reset_remote_yes_mode_resets_and_cleans() {
    let harness = GitCliHarness::new();
    let dir = init_repo();
    let remote = init_bare_remote();
    let remote_path = remote.path().to_string_lossy().to_string();

    git(dir.path(), &["remote", "add", "origin", &remote_path]);
    git(dir.path(), &["push", "-u", "origin", "main"]);

    commit_file(dir.path(), "local.txt", "local\n", "local commit");
    let untracked = dir.path().join("untracked.txt");
    fs::write(&untracked, "temp\n").expect("write untracked file");

    let output = harness.run(
        dir.path(),
        &[
            "reset",
            "remote",
            "--ref",
            "origin/main",
            "-r",
            "origin",
            "-b",
            "main",
            "--no-fetch",
            "--prune",
            "--clean",
            "--set-upstream",
            "-y",
        ],
    );

    assert_eq!(output.code, 0);
    assert!(
        output
            .stdout_text()
            .contains("โœ… Done. 'main' now matches 'origin/main'.")
    );
    assert!(!untracked.exists(), "expected untracked file to be removed");
}

#[test]
fn reset_remote_help_prints_usage() {
    let harness = GitCliHarness::new();
    let dir = tempfile::TempDir::new().expect("tempdir");

    let output = harness.run(dir.path(), &["reset", "remote", "--help"]);

    assert_eq!(output.code, 0);
    assert!(output.stdout_text().contains(
        "git-reset-remote: overwrite current local branch with a remote-tracking branch"
    ));
    assert!(output.stdout_text().contains("Options:"));
}

#[test]
fn reset_remote_detached_head_refuses() {
    let harness = GitCliHarness::new();
    let dir = setup_repo_with_two_commits();
    git(dir.path(), &["checkout", "HEAD~1"]);

    let output = harness.run(dir.path(), &["reset", "remote"]);

    assert_eq!(output.code, 1);
    assert!(
        output
            .stderr_text()
            .contains("โŒ Detached HEAD. Switch to a branch first.")
    );
}

#[test]
fn reset_remote_missing_tracking_ref_errors() {
    let harness = GitCliHarness::new();
    let dir = init_repo();
    let remote = init_bare_remote();
    let remote_path = remote.path().to_string_lossy().to_string();
    git(dir.path(), &["remote", "add", "origin", &remote_path]);

    let output = harness.run(
        dir.path(),
        &["reset", "remote", "--ref", "origin/main", "--no-fetch"],
    );

    assert_eq!(output.code, 1);
    assert!(
        output
            .stderr_text()
            .contains("โŒ Remote-tracking branch not found: origin/main")
    );
}

#[test]
fn reset_remote_clean_prompt_skip_leaves_untracked() {
    let harness = GitCliHarness::new();
    let dir = init_repo();
    let remote = init_bare_remote();
    let remote_path = remote.path().to_string_lossy().to_string();
    git(dir.path(), &["remote", "add", "origin", &remote_path]);
    git(dir.path(), &["push", "-u", "origin", "main"]);

    let untracked = dir.path().join("untracked.txt");
    fs::write(&untracked, "temp\n").expect("write untracked file");

    let output = run_with_stdin(
        &harness,
        dir.path(),
        &[
            "reset",
            "remote",
            "--ref",
            "origin/main",
            "--no-fetch",
            "--clean",
        ],
        "y\nn\n",
    );

    assert_eq!(output.code, 0);
    assert!(output.stdout_text().contains("โ„น๏ธ  Skipped git clean -fd"));
    assert!(untracked.exists(), "expected untracked file to remain");
}