Skip to content

Commit 29f71f5

Browse files
author
Stephan Dilly
committed
do not allow to ignore gitignore (fixes #825)
1 parent d6f43f0 commit 29f71f5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## Fixed
11+
- do not allow to ignore .gitignore files ([#825](https://github.com/extrawurst/gitui/issues/825))
12+
1013
## [0.16.2] - 2021-07-10
1114

1215
**undo last commit**

Diff for: asyncgit/src/sync/ignore.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::utils::{repo, work_dir};
2-
use crate::error::Result;
2+
use crate::error::{Error, Result};
33
use scopetime::scope_time;
44
use std::{
55
fs::{File, OpenOptions},
@@ -18,6 +18,14 @@ pub fn add_to_ignore(
1818

1919
let repo = repo(repo_path)?;
2020

21+
if Path::new(path_to_ignore).file_name()
22+
== Path::new(GITIGNORE).file_name()
23+
{
24+
return Err(Error::Generic(String::from(
25+
"cannot ignore gitignore",
26+
)));
27+
}
28+
2129
let ignore_file = work_dir(&repo)?.join(GITIGNORE);
2230

2331
let optional_newline = ignore_file.exists()
@@ -52,8 +60,9 @@ fn file_ends_with_newline(file: &Path) -> Result<bool> {
5260
#[cfg(test)]
5361
mod tests {
5462
use super::*;
55-
use crate::sync::tests::repo_init;
63+
use crate::sync::{tests::repo_init, utils::repo_write_file};
5664
use io::BufRead;
65+
use pretty_assertions::assert_eq;
5766
use std::{fs::File, io, path::Path};
5867

5968
#[test]
@@ -124,4 +133,20 @@ mod tests {
124133

125134
Ok(())
126135
}
136+
137+
#[test]
138+
fn test_ignore_ignore() {
139+
let ignore_file_path = Path::new(".gitignore");
140+
let (_td, repo) = repo_init().unwrap();
141+
let root = repo.path().parent().unwrap();
142+
let repo_path = root.as_os_str().to_str().unwrap();
143+
144+
repo_write_file(&repo, ".gitignore", "#foo").unwrap();
145+
146+
let res = add_to_ignore(repo_path, ".gitignore");
147+
assert!(res.is_err());
148+
149+
let lines = read_lines(&root.join(ignore_file_path)).unwrap();
150+
assert_eq!(lines.count(), 1);
151+
}
127152
}

0 commit comments

Comments
 (0)