Skip to content

Commit acf2a17

Browse files
Add function that sublime-ignores all git-ignored files
1 parent 67362a9 commit acf2a17

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

gitignore_plugin.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
1+
import sublime
12
import subprocess
23
import os
4+
import os.path
35

46
# Used for output suppression when calling subprocess functions; see
57
# http://stackoverflow.com/questions/10251391/suppressing-output-in-python-subprocess-call
68
devnull = open(os.devnull, 'w')
79

8-
def all_ignored_files(folder):
10+
def update_file_exclude_patterns():
11+
"""
12+
Updates the "file_exclude_patterns" preference to include all .gitignored
13+
files.
14+
15+
Also includes any additional files or folders listed in the
16+
"extra_file_exclude_patterns" and "extra_folder_exclude_patterns" settings.
17+
"""
18+
s = sublime.load_settings("Preferences.sublime-settings")
19+
file_exclude_patterns = s.get('extra_file_exclude_patterns', [])
20+
folder_exclude_patterns = s.get('extra_folder_exclude_patterns', [])
21+
for path in all_ignored_paths():
22+
if os.path.isdir(path):
23+
folder_exclude_patterns.append(path.rstrip('/'))
24+
else:
25+
file_exclude_patterns.append(path)
26+
s.set('file_exclude_patterns', file_exclude_patterns)
27+
s.set('folder_exclude_patterns', folder_exclude_patterns)
28+
sublime.save_settings("Preferences.sublime-settings")
29+
30+
def all_ignored_paths():
31+
"""
32+
Returns a list of all .gitignored files or folders contained in repos
33+
contained within or containing any folders open in any open windows.
34+
"""
35+
36+
open_folders = set()
37+
for window in sublime.windows():
38+
open_folders.update(window.folders())
39+
40+
all_ignored_paths = set()
41+
for folder in open_folders:
42+
ignored_paths = folder_ignored_paths(folder)
43+
all_ignored_paths.update(ignored_paths)
44+
45+
return list(all_ignored_paths)
46+
47+
def folder_ignored_paths(folder):
948
"""
1049
Returns a list (without duplicates, in the case of weird repo-nesting) of
11-
all files within the given folder that are git ignored.
50+
all files/folders within the given folder that are git ignored.
1251
1352
The folder itself need not be the top level of a git repo, nor even within
1453
a git repo.
1554
"""
1655

17-
all_ignored_files = set()
56+
all_ignored_paths = set()
1857

1958
# First find all repos CONTAINED in this folder:
2059
repos = set(find_git_repos(folder))
@@ -24,12 +63,12 @@ def all_ignored_files(folder):
2463
if is_in_git_repo(folder):
2564
repos.add(parent_repo_path(folder))
2665

27-
# Now we find all the ignored files in any of the above repos
66+
# Now we find all the ignored paths in any of the above repos
2867
for git_repo in repos:
29-
ignored_files = list_ignored_files(git_repo)
30-
all_ignored_files.update(ignored_files)
68+
ignored_paths = repo_ignored_paths(git_repo)
69+
all_ignored_paths.update(ignored_paths)
3170

32-
return list(all_ignored_files)
71+
return list(all_ignored_paths)
3372

3473
def is_in_git_repo(folder):
3574
"""
@@ -47,7 +86,8 @@ def is_in_git_repo(folder):
4786

4887
def parent_repo_path(folder):
4988
"""
50-
Takes the path to a folder contained within a git repo, and returns the parent repo.
89+
Takes the path to a folder contained within a git repo, and returns the
90+
parent repo.
5191
"""
5292

5393
return subprocess.Popen(
@@ -75,9 +115,10 @@ def find_git_repos(folder):
75115

76116
return [path.replace('/.git', '') for path in dot_git_folders]
77117

78-
def list_ignored_files(git_repo):
118+
def repo_ignored_paths(git_repo):
79119
"""
80-
Takes the path of a git repo and lists all ignored files in the repo.
120+
Takes the path of a git repo and lists all ignored files/folders in the
121+
repo.
81122
"""
82123

83124
# Trick for listing ignored files nicked from

0 commit comments

Comments
 (0)