Skip to content

Commit 14a93cb

Browse files
author
Stephan Dilly
committed
support adding file/folder to gitignore (closes #44)
1 parent 8e41dd2 commit 14a93cb

File tree

8 files changed

+68
-7
lines changed

8 files changed

+68
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
![](assets/cmdbar.gif)
1313

1414
### Added
15+
- support adding untracked file/folder to `.gitignore` ([#44](https://github.com/extrawurst/gitui/issues/44))
1516
- support reverse tabbing using shift+tab ([#92](https://github.com/extrawurst/gitui/issues/92))
1617
- switch to using cmd line args instead of `ENV` (`-l` for logging and `--version`) **please convert your GITUI_LOGGING usage** [[@shenek](https://github.com/shenek)] ([#88](https://github.com/extrawurst/gitui/issues/88))
1718
- added missing LICENSE.md files in sub-crates [[@ignatenkobrain](https://github.com/ignatenkobrain)] ([#94](https://github.com/extrawurst/gitui/pull/94))

asyncgit/src/sync/diff.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use git2::{
88
};
99
use scopetime::scope_time;
1010
use std::{fs, path::Path};
11+
use utils::work_dir;
1112

1213
/// type of diff of a single line
1314
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
@@ -127,12 +128,7 @@ pub fn get_diff(
127128
scope_time!("get_diff");
128129

129130
let repo = utils::repo(repo_path)?;
130-
let repo_path = repo.path().parent().ok_or_else(|| {
131-
Error::Generic(
132-
"repositories located at root are not supported."
133-
.to_string(),
134-
)
135-
})?;
131+
let work_dir = work_dir(&repo);
136132
let diff = get_diff_raw(&repo, &p, stage, false)?;
137133

138134
let mut res: FileDiff = FileDiff::default();
@@ -190,7 +186,7 @@ pub fn get_diff(
190186
)
191187
})?;
192188

193-
let newfile_path = repo_path.join(relative_path);
189+
let newfile_path = work_dir.join(relative_path);
194190

195191
if let Some(newfile_content) =
196192
new_file_content(&newfile_path)

asyncgit/src/sync/ignore.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use super::utils::{repo, work_dir};
2+
use crate::error::Result;
3+
use scopetime::scope_time;
4+
use std::{fs::OpenOptions, io::Write};
5+
6+
static GITIGNORE: &str = ".gitignore";
7+
8+
/// add file or path to root ignore file
9+
pub fn add_to_ignore(
10+
repo_path: &str,
11+
path_to_ignore: String,
12+
) -> Result<()> {
13+
scope_time!("add_to_ignore");
14+
15+
let repo = repo(repo_path)?;
16+
17+
let ignore_file = work_dir(&repo).join(GITIGNORE);
18+
19+
let mut file = OpenOptions::new()
20+
.append(true)
21+
.create(true)
22+
.open(ignore_file)?;
23+
24+
writeln!(file, "{}", path_to_ignore)?;
25+
26+
Ok(())
27+
}

asyncgit/src/sync/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod commits_info;
44
pub mod diff;
55
mod hooks;
66
mod hunks;
7+
mod ignore;
78
mod logwalker;
89
mod reset;
910
mod stash;
@@ -14,6 +15,7 @@ pub mod utils;
1415
pub use commits_info::{get_commits_info, CommitId, CommitInfo};
1516
pub use hooks::{hooks_commit_msg, hooks_post_commit, HookResult};
1617
pub use hunks::{stage_hunk, unstage_hunk};
18+
pub use ignore::add_to_ignore;
1719
pub use logwalker::LogWalker;
1820
pub use reset::{
1921
reset_stage, reset_workdir_file, reset_workdir_folder,

asyncgit/src/sync/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ pub fn repo(repo_path: &str) -> Result<Repository> {
4141
Ok(repo)
4242
}
4343

44+
///
45+
pub fn work_dir(repo: &Repository) -> &Path {
46+
repo.workdir().expect("unable to query workdir")
47+
}
48+
4449
/// this does not run any git hooks
4550
pub fn commit(repo_path: &str, msg: &str) -> Result<Oid> {
4651
scope_time!("commit");

src/components/changes.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ impl ChangesComponent {
124124
}
125125
false
126126
}
127+
128+
fn add_to_ignore(&mut self) -> Result<bool> {
129+
if let Some(tree_item) = self.selection() {
130+
sync::add_to_ignore(CWD, tree_item.info.full_path)?;
131+
self.queue
132+
.borrow_mut()
133+
.push_back(InternalEvent::Update(NeedsUpdate::ALL));
134+
return Ok(true);
135+
}
136+
Ok(false)
137+
}
127138
}
128139

129140
impl DrawableComponent for ChangesComponent {
@@ -159,6 +170,11 @@ impl Component for ChangesComponent {
159170
some_selection,
160171
self.focused(),
161172
));
173+
out.push(CommandInfo::new(
174+
commands::IGNORE_ITEM,
175+
some_selection,
176+
self.focused(),
177+
));
162178
} else {
163179
out.push(CommandInfo::new(
164180
commands::UNSTAGE_ITEM,
@@ -210,6 +226,13 @@ impl Component for ChangesComponent {
210226
{
211227
Ok(self.dispatch_reset_workdir())
212228
}
229+
230+
keys::STATUS_IGNORE_FILE
231+
if self.is_working_dir
232+
&& !self.is_empty() =>
233+
{
234+
Ok(self.add_to_ignore()?)
235+
}
213236
_ => Ok(false),
214237
};
215238
}

src/keys.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub const ENTER: KeyEvent = no_mod(KeyCode::Enter);
4242
pub const STATUS_STAGE_FILE: KeyEvent = no_mod(KeyCode::Enter);
4343
pub const STATUS_RESET_FILE: KeyEvent =
4444
with_mod(KeyCode::Char('D'), KeyModifiers::SHIFT);
45+
pub const STATUS_IGNORE_FILE: KeyEvent = no_mod(KeyCode::Char('i'));
4546
pub const STASHING_SAVE: KeyEvent = no_mod(KeyCode::Char('s'));
4647
pub const STASHING_TOGGLE_UNTRACKED: KeyEvent =
4748
no_mod(KeyCode::Char('u'));

src/strings.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ pub mod commands {
140140
"revert changes in selected file or entire path",
141141
CMD_GROUP_CHANGES,
142142
);
143+
///
144+
pub static IGNORE_ITEM: CommandText = CommandText::new(
145+
"Ignore [i]",
146+
"Add file or path to .gitignore",
147+
CMD_GROUP_CHANGES,
148+
);
143149

144150
///
145151
pub static STATUS_FOCUS_LEFT: CommandText = CommandText::new(

0 commit comments

Comments
 (0)