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
//! `git reflog` — manage and inspect reflog information.
use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
/// Actions supported by `git reflog`.
#[derive(Debug, Clone)]
pub enum ReflogAction {
/// `git reflog [show] [<ref>]`.
Show {
/// Ref to inspect (default `HEAD`).
ref_name: Option<String>,
/// `-n N` / `--max-count=N`.
max_count: Option<u32>,
/// Extra arbitrary format.
format: Option<String>,
},
/// `git reflog expire [options] <refs>`.
Expire {
/// `--all`.
all: bool,
/// `--expire=<time>`.
expire: Option<String>,
/// `--expire-unreachable=<time>`.
expire_unreachable: Option<String>,
/// `--stale-fix`.
stale_fix: bool,
/// Refs to expire.
refs: Vec<String>,
},
/// `git reflog delete <entry>…`.
Delete {
/// Entries to delete (e.g. `HEAD@{0}`).
entries: Vec<String>,
/// `--rewrite`.
rewrite: bool,
},
/// `git reflog exists <ref>`.
Exists {
/// Ref to check.
ref_name: String,
},
}
/// Builder for `git reflog`.
#[derive(Debug, Clone)]
pub struct ReflogCommand {
/// Shared executor.
pub executor: CommandExecutor,
/// Action.
pub action: ReflogAction,
}
impl ReflogCommand {
/// `reflog` / `reflog show`.
#[must_use]
pub fn show() -> Self {
Self {
executor: CommandExecutor::default(),
action: ReflogAction::Show {
ref_name: None,
max_count: None,
format: None,
},
}
}
/// Set the ref (for `show`).
#[must_use]
pub fn ref_name(mut self, r: impl Into<String>) -> Self {
if let ReflogAction::Show { ref_name, .. } = &mut self.action {
*ref_name = Some(r.into());
}
self
}
/// `-n` / `--max-count` (for `show`).
#[must_use]
pub fn max_count(mut self, n: u32) -> Self {
if let ReflogAction::Show { max_count, .. } = &mut self.action {
*max_count = Some(n);
}
self
}
/// Set `--format` (for `show`).
#[must_use]
pub fn format(mut self, f: impl Into<String>) -> Self {
if let ReflogAction::Show { format, .. } = &mut self.action {
*format = Some(f.into());
}
self
}
/// `reflog expire`.
#[must_use]
pub fn expire() -> Self {
Self {
executor: CommandExecutor::default(),
action: ReflogAction::Expire {
all: false,
expire: None,
expire_unreachable: None,
stale_fix: false,
refs: vec![],
},
}
}
/// `reflog delete`.
#[must_use]
pub fn delete(entries: Vec<String>) -> Self {
Self {
executor: CommandExecutor::default(),
action: ReflogAction::Delete {
entries,
rewrite: false,
},
}
}
/// `reflog exists <ref>`.
pub fn exists(r: impl Into<String>) -> Self {
Self {
executor: CommandExecutor::default(),
action: ReflogAction::Exists { ref_name: r.into() },
}
}
}
#[async_trait]
impl GitCommand for ReflogCommand {
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!["reflog".to_string()];
match &self.action {
ReflogAction::Show {
ref_name,
max_count,
format,
} => {
args.push("show".into());
if let Some(n) = max_count {
args.push(format!("-n{n}"));
}
if let Some(f) = format {
args.push(format!("--format={f}"));
}
if let Some(r) = ref_name {
args.push(r.clone());
}
}
ReflogAction::Expire {
all,
expire,
expire_unreachable,
stale_fix,
refs,
} => {
args.push("expire".into());
if *all {
args.push("--all".into());
}
if *stale_fix {
args.push("--stale-fix".into());
}
if let Some(e) = expire {
args.push(format!("--expire={e}"));
}
if let Some(e) = expire_unreachable {
args.push(format!("--expire-unreachable={e}"));
}
args.extend(refs.iter().cloned());
}
ReflogAction::Delete { entries, rewrite } => {
args.push("delete".into());
if *rewrite {
args.push("--rewrite".into());
}
args.extend(entries.iter().cloned());
}
ReflogAction::Exists { ref_name } => {
args.push("exists".into());
args.push(ref_name.clone());
}
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}