Skip to content

Commit 378b1be

Browse files
committed
refactor
* rename `index` to `parent_index`. * add test for clap definition of internal-tools * optimize generated script a bit
1 parent c7a2e80 commit 378b1be

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

gix-blame/src/file/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub fn file(
174174
commit_id: suspect,
175175
blob_id: entry.unwrap_or(ObjectId::null(gix_hash::Kind::Sha1)),
176176
previous_blob_id: ObjectId::null(gix_hash::Kind::Sha1),
177-
index: 0,
177+
parent_index: 0,
178178
};
179179
blame_path.push(blame_path_entry);
180180
}
@@ -307,7 +307,7 @@ pub fn file(
307307
commit_id: suspect,
308308
blob_id: id,
309309
previous_blob_id: ObjectId::null(gix_hash::Kind::Sha1),
310-
index,
310+
parent_index: index,
311311
};
312312
blame_path.push(blame_path_entry);
313313
}
@@ -340,7 +340,7 @@ pub fn file(
340340
commit_id: suspect,
341341
blob_id: id,
342342
previous_blob_id: previous_id,
343-
index,
343+
parent_index: index,
344344
};
345345
blame_path.push(blame_path_entry);
346346
}
@@ -381,7 +381,7 @@ pub fn file(
381381
commit_id: suspect,
382382
blob_id: id,
383383
previous_blob_id: source_id,
384-
index,
384+
parent_index: index,
385385
};
386386
blame_path.push(blame_path_entry);
387387
}

gix-blame/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub struct BlamePathEntry {
175175
pub previous_blob_id: ObjectId,
176176
/// When there is more than one `BlamePathEntry` for a commit, this indicates to which parent
177177
/// commit the change is related.
178-
pub index: usize,
178+
pub parent_index: usize,
179179
}
180180

181181
/// The outcome of [`file()`](crate::file()).

tests/it/src/commands/blame_copy_royal.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub(super) mod function {
66
use anyhow::Context;
77
use gix::{
88
blame::BlamePathEntry,
9-
bstr::{BStr, BString, ByteSlice},
9+
bstr::{BString, ByteSlice},
1010
objs::FindExt,
1111
ObjectId,
1212
};
@@ -72,14 +72,11 @@ pub(super) mod function {
7272
options,
7373
)?;
7474

75-
let blame_path = outcome
75+
let blame_infos = outcome
7676
.blame_path
7777
.expect("blame path to be present as `debug_track_path == true`");
7878

79-
// TODO
80-
// Potentially make `"assets"` configurable (it is in `git_to_sh`).
8179
let assets = destination_dir.join("assets");
82-
8380
eprintln!("{prefix} create directory '{assets}'", assets = assets.display());
8481

8582
if !dry_run {
@@ -88,17 +85,9 @@ pub(super) mod function {
8885

8986
let mut buf = Vec::new();
9087

91-
for blame_path_entry in &blame_path {
92-
let src: &BStr = blame_path_entry.source_file_path.as_bstr();
88+
eprintln!("{prefix} perform {} asset copy operations", blame_infos.len(),);
89+
for blame_path_entry in &blame_infos {
9390
let dst = assets.join(format!("{}.commit", blame_path_entry.commit_id));
94-
95-
eprintln!(
96-
"{prefix} copy file '{}' at commit {} to '{dst}'",
97-
src,
98-
blame_path_entry.commit_id,
99-
dst = dst.display()
100-
);
101-
10291
if !dry_run {
10392
let blob = repo.objects.find_blob(&blame_path_entry.blob_id, &mut buf)?.data;
10493

@@ -113,18 +102,15 @@ pub(super) mod function {
113102
})?;
114103

115104
let blob = crate::commands::copy_royal::remapped(blob);
116-
117105
std::fs::write(dst, blob)?;
118106
}
119107
}
120108
}
121109

122-
let mut blame_script = BlameScript::new(blame_path, Options { verbatim });
123-
110+
let mut blame_script = BlameScript::new(blame_infos, Options { verbatim });
124111
blame_script.generate()?;
125112

126113
let script_file = destination_dir.join("create-history.sh");
127-
128114
eprintln!(
129115
"{prefix} write script file at '{script_file}'",
130116
script_file = script_file.display()
@@ -174,16 +160,25 @@ echo create-history.sh >> .gitignore
174160
git rm {src}
175161
"
176162
),
177-
BlameScriptOperation::CommitFile(src, commit_id) => write!(
178-
f,
179-
r"# make file {src} contain content at commit {commit_id}
180-
mkdir -p $(dirname {src})
163+
BlameScriptOperation::CommitFile(src, commit_id) => {
164+
write!(
165+
f,
166+
r"# make file {src} contain content at commit {commit_id}
167+
"
168+
)?;
169+
if let Some(pos) = src.rfind_byte(b'/') {
170+
let dirname = src[..pos].as_bstr();
171+
write!(f, "mkdir -p \"{dirname}\"\n")?;
172+
}
173+
write!(
174+
f,
175+
r"#
181176
cp ./assets/{commit_id}.commit ./{src}
182-
# create commit
183177
git add {src}
184178
git commit -m {commit_id}
185179
"
186-
),
180+
)
181+
}
187182
BlameScriptOperation::CheckoutTag(commit_id) => writeln!(f, "git checkout tag-{commit_id}"),
188183
BlameScriptOperation::PrepareMerge(commit_ids) => writeln!(
189184
f,
@@ -200,18 +195,18 @@ git commit -m {commit_id}
200195
}
201196

202197
struct BlameScript {
203-
blame_path: Vec<BlamePathEntry>,
198+
blame_infos: Vec<BlamePathEntry>,
204199
seen: BTreeSet<ObjectId>,
205200
script: Vec<BlameScriptOperation>,
206201
options: Options,
207202
}
208203

209204
impl BlameScript {
210-
fn new(blame_path: Vec<BlamePathEntry>, options: Options) -> Self {
205+
fn new(blame_infos: Vec<BlamePathEntry>, options: Options) -> Self {
211206
let script = vec![BlameScriptOperation::InitRepository];
212207

213208
Self {
214-
blame_path,
209+
blame_infos,
215210
seen: BTreeSet::default(),
216211
script,
217212
options,
@@ -224,9 +219,9 @@ git commit -m {commit_id}
224219
// methods can rely on the assumption that the root comes first, followed by its
225220
// descendants. That way, we can use a simple `for` loop to iterate through
226221
// `self.blame_path` below.
227-
self.blame_path.reverse();
222+
self.blame_infos.reverse();
228223

229-
for blame_path_entry in self.blame_path.clone() {
224+
for blame_path_entry in self.blame_infos.clone() {
230225
if !self.seen.contains(&blame_path_entry.commit_id) {
231226
self.process_entry(&blame_path_entry)?;
232227
}
@@ -309,13 +304,13 @@ git commit -m {commit_id}
309304
// commits where there’s changes against each parent. Each of these changes would
310305
// produce a diff that’s represented in `self.blame_path`.
311306
let mut children: Vec<_> = self
312-
.blame_path
307+
.blame_infos
313308
.iter()
314309
.enumerate()
315310
.filter(|(_, x)| x.commit_id == child.commit_id)
316311
.collect();
317312

318-
children.sort_by_key(|(_, x)| x.index);
313+
children.sort_by_key(|(_, x)| x.parent_index);
319314

320315
let parents = children
321316
.iter()
@@ -325,7 +320,7 @@ git commit -m {commit_id}
325320

326321
// When we search for a parent we only have to consider entries up to and
327322
// excluding `index` as anything after `index` can only be a child.
328-
self.blame_path[..(*index)]
323+
self.blame_infos[..(*index)]
329324
.iter()
330325
.rfind(|&x| {
331326
x.blob_id == parent_blob_id && Some(&x.source_file_path) == parent_source_file_path.as_ref()

tests/it/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ fn main() -> anyhow::Result<()> {
5151

5252
mod args;
5353
use args::{Args, Subcommands};
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::*;
58+
59+
#[test]
60+
fn clap() {
61+
use clap::CommandFactory;
62+
Args::command().debug_assert();
63+
}
64+
}

0 commit comments

Comments
 (0)