|
1 |
| -use anyhow::Context; |
2 |
| -use gix::fs::Stack; |
3 |
| -use gix::pathspec::Pattern; |
4 |
| -use std::path::{Path, PathBuf}; |
| 1 | +pub(super) mod function { |
| 2 | + use anyhow::Context; |
| 3 | + use gix::fs::Stack; |
| 4 | + use gix::pathspec::Pattern; |
| 5 | + use std::path::{Path, PathBuf}; |
5 | 6 |
|
6 |
| -pub fn doit( |
7 |
| - dry_run: bool, |
8 |
| - worktree_dir: &Path, |
9 |
| - destination_dir: PathBuf, |
10 |
| - patterns: Vec<Pattern>, |
11 |
| -) -> anyhow::Result<()> { |
12 |
| - let prefix = if dry_run { "WOULD" } else { "Will" }; |
13 |
| - let repo = gix::open(worktree_dir)?; |
14 |
| - let index = repo.index()?; |
15 |
| - let mut specs = repo.pathspec( |
16 |
| - true, |
17 |
| - // TODO: ideally this could accept patterns already. |
18 |
| - patterns.into_iter().map(|p| p.to_bstring()), |
19 |
| - true, |
20 |
| - &index, |
21 |
| - gix::worktree::stack::state::attributes::Source::WorktreeThenIdMapping, |
22 |
| - )?; |
23 |
| - let mut create_dir = CreateDir { dry_run }; |
24 |
| - let mut stack = gix::fs::Stack::new(destination_dir); |
25 |
| - for (rela_path, _entry) in specs |
26 |
| - .index_entries_with_paths(&index) |
27 |
| - .context("Didn't find a single entry to copy")? |
28 |
| - { |
29 |
| - let rela_path = gix::path::from_bstr(rela_path); |
30 |
| - let src = worktree_dir.join(&rela_path); |
31 |
| - stack.make_relative_path_current(&rela_path, &mut create_dir)?; |
32 |
| - let dst = stack.current(); |
| 7 | + pub fn copy_royal( |
| 8 | + dry_run: bool, |
| 9 | + worktree_dir: &Path, |
| 10 | + destination_dir: PathBuf, |
| 11 | + patterns: Vec<Pattern>, |
| 12 | + ) -> anyhow::Result<()> { |
| 13 | + let prefix = if dry_run { "WOULD" } else { "Will" }; |
| 14 | + let repo = gix::open(worktree_dir)?; |
| 15 | + let index = repo.index()?; |
| 16 | + let mut specs = repo.pathspec( |
| 17 | + true, |
| 18 | + // TODO: ideally this could accept patterns already. |
| 19 | + patterns.into_iter().map(|p| p.to_bstring()), |
| 20 | + true, |
| 21 | + &index, |
| 22 | + gix::worktree::stack::state::attributes::Source::WorktreeThenIdMapping, |
| 23 | + )?; |
| 24 | + let mut create_dir = CreateDir { dry_run }; |
| 25 | + let mut stack = gix::fs::Stack::new(destination_dir); |
| 26 | + for (rela_path, _entry) in specs |
| 27 | + .index_entries_with_paths(&index) |
| 28 | + .context("Didn't find a single entry to copy")? |
| 29 | + { |
| 30 | + let rela_path = gix::path::from_bstr(rela_path); |
| 31 | + let src = worktree_dir.join(&rela_path); |
| 32 | + stack.make_relative_path_current(&rela_path, &mut create_dir)?; |
| 33 | + let dst = stack.current(); |
33 | 34 |
|
34 |
| - eprintln!( |
35 |
| - "{prefix} copy '{src}' to '{dst}'", |
36 |
| - src = src.display(), |
37 |
| - dst = dst.display() |
38 |
| - ); |
39 |
| - if !dry_run { |
40 |
| - let content = std::fs::read_to_string(&src).with_context(|| { |
41 |
| - format!( |
42 |
| - "Need UTF-8 decodable content in '{src}' - skip binaries with pathspec", |
43 |
| - src = src.display() |
44 |
| - ) |
45 |
| - })?; |
46 |
| - std::fs::write(dst, remapped(content))? |
| 35 | + eprintln!( |
| 36 | + "{prefix} copy '{src}' to '{dst}'", |
| 37 | + src = src.display(), |
| 38 | + dst = dst.display() |
| 39 | + ); |
| 40 | + if !dry_run { |
| 41 | + let content = std::fs::read_to_string(&src).with_context(|| { |
| 42 | + format!( |
| 43 | + "Need UTF-8 decodable content in '{src}' - skip binaries with pathspec", |
| 44 | + src = src.display() |
| 45 | + ) |
| 46 | + })?; |
| 47 | + std::fs::write(dst, remapped(&content))? |
| 48 | + } |
47 | 49 | }
|
| 50 | + Ok(()) |
48 | 51 | }
|
49 |
| - Ok(()) |
50 |
| -} |
51 | 52 |
|
52 |
| -fn remapped(i: String) -> String { |
53 |
| - i.chars() |
54 |
| - .filter_map(|c| { |
55 |
| - Some(if c.is_alphabetic() { |
56 |
| - if c.is_uppercase() { |
57 |
| - match (c as u32) % 10 { |
58 |
| - 0 => 'A', |
59 |
| - 1 => 'E', |
60 |
| - 2 => 'I', |
61 |
| - 3 => 'O', |
62 |
| - 4 => 'U', |
63 |
| - 5 => 'X', |
64 |
| - 6 => 'R', |
65 |
| - 7 => 'S', |
66 |
| - 8 => 'T', |
67 |
| - 9 => 'Y', |
68 |
| - _ => unreachable!(), |
| 53 | + pub fn remapped(i: &str) -> String { |
| 54 | + i.chars() |
| 55 | + .filter_map(|c| { |
| 56 | + Some(if c.is_alphabetic() { |
| 57 | + if c.is_uppercase() { |
| 58 | + match (c as u32) % 10 { |
| 59 | + 0 => 'A', |
| 60 | + 1 => 'E', |
| 61 | + 2 => 'I', |
| 62 | + 3 => 'O', |
| 63 | + 4 => 'U', |
| 64 | + 5 => 'X', |
| 65 | + 6 => 'R', |
| 66 | + 7 => 'S', |
| 67 | + 8 => 'T', |
| 68 | + 9 => 'Y', |
| 69 | + _ => unreachable!(), |
| 70 | + } |
| 71 | + } else { |
| 72 | + match (c as u32) % 10 { |
| 73 | + 0 => 'a', |
| 74 | + 1 => 'e', |
| 75 | + 2 => 'i', |
| 76 | + 3 => 'o', |
| 77 | + 4 => 'u', |
| 78 | + 5 => 'x', |
| 79 | + 6 => 'r', |
| 80 | + 7 => 's', |
| 81 | + 8 => 't', |
| 82 | + 9 => 'y', |
| 83 | + _ => unreachable!(), |
| 84 | + } |
69 | 85 | }
|
| 86 | + } else if c.is_whitespace() || c.is_ascii_punctuation() || c.is_ascii_digit() { |
| 87 | + c |
70 | 88 | } else {
|
71 |
| - match (c as u32) % 10 { |
72 |
| - 0 => 'a', |
73 |
| - 1 => 'e', |
74 |
| - 2 => 'i', |
75 |
| - 3 => 'o', |
76 |
| - 4 => 'u', |
77 |
| - 5 => 'x', |
78 |
| - 6 => 'r', |
79 |
| - 7 => 's', |
80 |
| - 8 => 't', |
81 |
| - 9 => 'y', |
82 |
| - _ => unreachable!(), |
83 |
| - } |
84 |
| - } |
85 |
| - } else if c.is_whitespace() || c.is_ascii_punctuation() || c.is_ascii_digit() { |
86 |
| - c |
87 |
| - } else { |
88 |
| - return None; |
| 89 | + return None; |
| 90 | + }) |
89 | 91 | })
|
90 |
| - }) |
91 |
| - .collect() |
92 |
| -} |
| 92 | + .collect() |
| 93 | + } |
93 | 94 |
|
94 |
| -struct CreateDir { |
95 |
| - dry_run: bool, |
96 |
| -} |
| 95 | + struct CreateDir { |
| 96 | + dry_run: bool, |
| 97 | + } |
97 | 98 |
|
98 |
| -impl gix::fs::stack::Delegate for CreateDir { |
99 |
| - fn push_directory(&mut self, stack: &Stack) -> std::io::Result<()> { |
100 |
| - if !self.dry_run && !stack.current().is_dir() { |
101 |
| - std::fs::create_dir(stack.current())?; |
| 99 | + impl gix::fs::stack::Delegate for CreateDir { |
| 100 | + fn push_directory(&mut self, stack: &Stack) -> std::io::Result<()> { |
| 101 | + if !self.dry_run && !stack.current().is_dir() { |
| 102 | + std::fs::create_dir(stack.current())?; |
| 103 | + } |
| 104 | + Ok(()) |
102 | 105 | }
|
103 |
| - Ok(()) |
104 |
| - } |
105 | 106 |
|
106 |
| - fn push(&mut self, _is_last_component: bool, _stack: &Stack) -> std::io::Result<()> { |
107 |
| - Ok(()) |
108 |
| - } |
| 107 | + fn push(&mut self, _is_last_component: bool, _stack: &Stack) -> std::io::Result<()> { |
| 108 | + Ok(()) |
| 109 | + } |
109 | 110 |
|
110 |
| - fn pop_directory(&mut self) {} |
| 111 | + fn pop_directory(&mut self) {} |
| 112 | + } |
111 | 113 | }
|
| 114 | +pub use function::remapped; |
0 commit comments