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
use std::io::Write;
use anyhow::Result;
use clap::Subcommand;
use colored::Colorize;
use tabled::{Table, Tabled};
use super::commit::{author_label, first_line_truncated, short_hash};
use super::{output_json, parse_repo, print_json};
use crate::api::BitbucketClient;
use crate::cli::pagination::{effective_limit, format_size};
/// Width of the truncated commit-message column in list tables.
const MSG_WIDTH: usize = 50;
#[derive(Subcommand)]
pub enum FileCommands {
/// List files and directories in a repository
Ls {
/// Repository in format workspace/repo-slug
repo: String,
/// Directory path to list (default: repository root)
#[arg(default_value = "")]
path: String,
/// Branch, tag, or commit hash (default: the repository's main branch)
#[arg(short = 'r', long = "ref", value_name = "REF")]
git_ref: Option<String>,
/// Number of results per page (max 100)
#[arg(short, long, default_value = "25")]
limit: u32,
},
/// Print the raw contents of a file
Cat {
/// Repository in format workspace/repo-slug
repo: String,
/// File path within the repository
path: String,
/// Branch, tag, or commit hash (default: the repository's main branch)
#[arg(short = 'r', long = "ref", value_name = "REF")]
git_ref: Option<String>,
},
/// List the commits that touched a file
History {
/// Repository in format workspace/repo-slug
repo: String,
/// File path within the repository
path: String,
/// Branch, tag, or commit hash to start from (default: the repository's main branch)
#[arg(short = 'r', long = "ref", value_name = "REF")]
git_ref: Option<String>,
/// Number of results per page (max 100)
#[arg(short, long, default_value = "25")]
limit: u32,
},
}
#[derive(Tabled)]
struct SourceRow {
#[tabled(rename = "TYPE")]
entry_type: String,
#[tabled(rename = "SIZE")]
size: String,
#[tabled(rename = "PATH")]
path: String,
}
#[derive(Tabled)]
struct HistoryRow {
#[tabled(rename = "COMMIT")]
commit: String,
#[tabled(rename = "AUTHOR")]
author: String,
#[tabled(rename = "DATE")]
date: String,
#[tabled(rename = "MESSAGE")]
message: String,
}
impl FileCommands {
pub async fn run(self) -> Result<()> {
match self {
FileCommands::Ls {
repo,
path,
git_ref,
limit,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let git_ref = resolve_ref(&client, &workspace, &repo_slug, git_ref).await?;
let entries = client
.list_source(
&workspace,
&repo_slug,
&git_ref,
&path,
None,
Some(effective_limit(limit)),
)
.await?;
if output_json() {
return print_json(&entries.values);
}
if entries.values.is_empty() {
println!("No entries found at '{}' in {} ({})", path, repo, git_ref);
return Ok(());
}
let rows: Vec<SourceRow> = entries
.values
.iter()
.map(|e| SourceRow {
entry_type: entry_type_label(&e.entry_type).to_string(),
size: e.size.map(format_size).unwrap_or_else(|| "-".to_string()),
path: e.path.clone(),
})
.collect();
println!("{}", Table::new(rows));
if entries.next.is_some() {
println!(
"\n{} More entries available. Use --limit to see more.",
"ℹ".blue()
);
}
Ok(())
}
FileCommands::Cat {
repo,
path,
git_ref,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let git_ref = resolve_ref(&client, &workspace, &repo_slug, git_ref).await?;
let content = client
.get_file_raw(&workspace, &repo_slug, &git_ref, &path)
.await?;
// Emit the file verbatim: no added trailing newline, and no
// table/color decoration, so output is pipe-friendly.
print!("{}", content);
std::io::stdout().flush()?;
Ok(())
}
FileCommands::History {
repo,
path,
git_ref,
limit,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let git_ref = resolve_ref(&client, &workspace, &repo_slug, git_ref).await?;
let history = client
.get_file_history(
&workspace,
&repo_slug,
&git_ref,
&path,
Some(effective_limit(limit)),
)
.await?;
if output_json() {
return print_json(&history.values);
}
if history.values.is_empty() {
println!("No history found for '{}' in {} ({})", path, repo, git_ref);
return Ok(());
}
let rows: Vec<HistoryRow> = history
.values
.iter()
.map(|entry| {
let commit = entry.commit.as_ref();
HistoryRow {
commit: commit.map(|c| short_hash(&c.hash)).unwrap_or_default(),
author: commit
.map(|c| author_label(c.author.as_ref()))
.unwrap_or_else(|| "-".to_string()),
date: commit
.and_then(|c| c.date)
.map(|d| d.format("%Y-%m-%d").to_string())
.unwrap_or_default(),
message: commit
.and_then(|c| c.message.as_deref())
.map(|m| first_line_truncated(m, MSG_WIDTH))
.unwrap_or_default(),
}
})
.collect();
println!("{}", Table::new(rows));
if history.next.is_some() {
println!(
"\n{} More history available. Use --limit to see more.",
"ℹ".blue()
);
}
Ok(())
}
}
}
}
/// Resolve the ref to browse: an explicit --ref wins; otherwise the
/// repository's configured main branch, falling back to "main".
async fn resolve_ref(
client: &BitbucketClient,
workspace: &str,
repo_slug: &str,
git_ref: Option<String>,
) -> Result<String> {
if let Some(r) = git_ref {
return Ok(r);
}
let repository = client.get_repository(workspace, repo_slug).await?;
Ok(repository
.mainbranch
.map(|b| b.name)
.unwrap_or_else(|| "main".to_string()))
}
/// Map Bitbucket's source entry types to short labels for the TYPE column.
fn entry_type_label(entry_type: &str) -> &str {
match entry_type {
"commit_file" => "file",
"commit_directory" => "dir",
other => other,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entry_type_label_maps_known_types() {
assert_eq!(entry_type_label("commit_file"), "file");
assert_eq!(entry_type_label("commit_directory"), "dir");
}
#[test]
fn entry_type_label_passes_through_unknown_types() {
assert_eq!(entry_type_label("commit_submodule"), "commit_submodule");
}
}