1
1
use super :: utils:: { repo, work_dir} ;
2
- use crate :: error:: Result ;
2
+ use crate :: error:: { Error , Result } ;
3
3
use scopetime:: scope_time;
4
4
use std:: {
5
5
fs:: { File , OpenOptions } ,
@@ -18,6 +18,14 @@ pub fn add_to_ignore(
18
18
19
19
let repo = repo ( repo_path) ?;
20
20
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
+
21
29
let ignore_file = work_dir ( & repo) ?. join ( GITIGNORE ) ;
22
30
23
31
let optional_newline = ignore_file. exists ( )
@@ -52,8 +60,9 @@ fn file_ends_with_newline(file: &Path) -> Result<bool> {
52
60
#[ cfg( test) ]
53
61
mod tests {
54
62
use super :: * ;
55
- use crate :: sync:: tests:: repo_init;
63
+ use crate :: sync:: { tests:: repo_init, utils :: repo_write_file } ;
56
64
use io:: BufRead ;
65
+ use pretty_assertions:: assert_eq;
57
66
use std:: { fs:: File , io, path:: Path } ;
58
67
59
68
#[ test]
@@ -124,4 +133,20 @@ mod tests {
124
133
125
134
Ok ( ( ) )
126
135
}
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
+ }
127
152
}
0 commit comments