Skip to content

Commit

Permalink
gitignore: move function for chaining .gitignore to central place (#65,
Browse files Browse the repository at this point in the history
#87)

I want to be able to reuse the code for chaining two `.gitignore`
files outside of `working_copy.rs`.
  • Loading branch information
martinvonz committed Mar 12, 2022
1 parent 7cc1d16 commit 9f36faa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
18 changes: 18 additions & 0 deletions lib/src/gitignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::sync::Arc;

use itertools::Itertools;
Expand Down Expand Up @@ -183,6 +186,21 @@ impl GitIgnoreFile {
})
}

pub fn chain_with_file(
self: &Arc<GitIgnoreFile>,
prefix: &str,
file: PathBuf,
) -> Arc<GitIgnoreFile> {
if file.is_file() {
let mut file = File::open(file).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
self.chain(prefix, &buf)
} else {
self.clone()
}
}

fn all_lines_reversed<'a>(&'a self) -> Box<dyn Iterator<Item = &GitIgnoreLine> + 'a> {
if let Some(parent) = &self.parent {
Box::new(self.lines.iter().rev().chain(parent.all_lines_reversed()))
Expand Down
25 changes: 3 additions & 22 deletions lib/src/working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,6 @@ impl TreeState {
self.store.write_symlink(path, str_target).unwrap()
}

fn try_chain_gitignore(
base: &Arc<GitIgnoreFile>,
prefix: &str,
file: PathBuf,
) -> Arc<GitIgnoreFile> {
if file.is_file() {
let mut file = File::open(file).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
base.chain(prefix, &buf)
} else {
base.clone()
}
}

// Look for changes to the working copy. If there are any changes, create
// a new tree from it and return it, and also update the dirstate on disk.
pub fn write_tree(&mut self) -> TreeId {
Expand All @@ -316,20 +301,16 @@ impl TreeState {
let mut git_ignore = GitIgnoreFile::empty();
if let Ok(home_dir) = std::env::var("HOME") {
let home_dir_path = PathBuf::from(home_dir);
git_ignore =
TreeState::try_chain_gitignore(&git_ignore, "", home_dir_path.join(".gitignore"));
git_ignore = git_ignore.chain_with_file("", home_dir_path.join(".gitignore"));
}

let mut work = vec![(RepoPath::root(), self.working_copy_path.clone(), git_ignore)];
let mut tree_builder = self.store.tree_builder(self.tree_id.clone());
let mut deleted_files: HashSet<_> = self.file_states.keys().cloned().collect();
while !work.is_empty() {
let (dir, disk_dir, git_ignore) = work.pop().unwrap();
let git_ignore = TreeState::try_chain_gitignore(
&git_ignore,
&dir.to_internal_dir_string(),
disk_dir.join(".gitignore"),
);
let git_ignore = git_ignore
.chain_with_file(&dir.to_internal_dir_string(), disk_dir.join(".gitignore"));
for maybe_entry in disk_dir.read_dir().unwrap() {
let entry = maybe_entry.unwrap();
let file_type = entry.file_type().unwrap();
Expand Down

0 comments on commit 9f36faa

Please sign in to comment.