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
//! `git ls-files` — show information about files in the index and working tree.
//!
//! ```no_run
//! use git_spawn::{GitCommand, LsFilesCommand};
//!
//! # async fn example() -> git_spawn::Result<()> {
//! let mut cmd = LsFilesCommand::new();
//! cmd.current_dir("/repo").cached();
//! let out = cmd.execute().await?;
//! for path in out.stdout.lines() {
//! println!("{path}");
//! }
//! # Ok(())
//! # }
//! ```
use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
/// Builder for `git ls-files`.
#[derive(Debug, Clone, Default)]
pub struct LsFilesCommand {
/// Shared executor.
pub executor: CommandExecutor,
/// `--cached` / `-c`.
pub cached: bool,
/// `--deleted` / `-d`.
pub deleted: bool,
/// `--modified` / `-m`.
pub modified: bool,
/// `--others` / `-o`.
pub others: bool,
/// `--ignored` / `-i`.
pub ignored: bool,
/// `--stage` / `-s`.
pub stage: bool,
/// `--unmerged` / `-u`.
pub unmerged: bool,
/// `--exclude-standard`.
pub exclude_standard: bool,
/// `-z` NUL-terminate.
pub null_terminate: bool,
/// Pathspec filters.
pub paths: Vec<String>,
}
impl LsFilesCommand {
/// New command.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Show cached files (default behavior).
pub fn cached(&mut self) -> &mut Self {
self.cached = true;
self
}
/// Show deleted files.
pub fn deleted(&mut self) -> &mut Self {
self.deleted = true;
self
}
/// Show modified files.
pub fn modified(&mut self) -> &mut Self {
self.modified = true;
self
}
/// Show untracked (other) files.
pub fn others(&mut self) -> &mut Self {
self.others = true;
self
}
/// Show ignored files (combine with `--others`).
pub fn ignored(&mut self) -> &mut Self {
self.ignored = true;
self
}
/// Include stage numbers and SHAs.
pub fn stage(&mut self) -> &mut Self {
self.stage = true;
self
}
/// Show unmerged files only.
pub fn unmerged(&mut self) -> &mut Self {
self.unmerged = true;
self
}
/// Honor `.gitignore` when listing others.
pub fn exclude_standard(&mut self) -> &mut Self {
self.exclude_standard = true;
self
}
/// NUL-terminate output (useful for paths with newlines).
pub fn null_terminate(&mut self) -> &mut Self {
self.null_terminate = true;
self
}
/// Filter by path.
pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
self.paths.push(p.into());
self
}
}
#[async_trait]
impl GitCommand for LsFilesCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["ls-files".to_string()];
if self.cached {
args.push("--cached".into());
}
if self.deleted {
args.push("--deleted".into());
}
if self.modified {
args.push("--modified".into());
}
if self.others {
args.push("--others".into());
}
if self.ignored {
args.push("--ignored".into());
}
if self.stage {
args.push("--stage".into());
}
if self.unmerged {
args.push("--unmerged".into());
}
if self.exclude_standard {
args.push("--exclude-standard".into());
}
if self.null_terminate {
args.push("-z".into());
}
if !self.paths.is_empty() {
args.push("--".into());
args.extend(self.paths.iter().cloned());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}