Skip to content

Commit 68ce9e2

Browse files
Merge pull request #20 from floydwch/fix-getting-setting
fix getting setting
2 parents 221fb4f + 702ceb3 commit 68ce9e2

1 file changed

Lines changed: 34 additions & 35 deletions

File tree

‎gitignore_plugin.py‎

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
def start(): # Gets invoked at the bottom of this file.
2424
"""
2525
Regularly (every 5s) updates the file_exclude_patterns setting.
26-
"""
26+
"""
2727
if is_first_launch():
2828
migrate_exclude_patterns()
2929
record_first_launch()
30-
30+
3131
def run():
3232
update_file_exclude_patterns()
3333
sublime.set_timeout(run, 5000)
@@ -38,13 +38,13 @@ def update_file_exclude_patterns():
3838
"""
3939
Updates the "file_exclude_patterns" preference to include all .gitignored
4040
files.
41-
41+
4242
Also includes any additional files or folders listed in the
4343
"extra_file_exclude_patterns" and "extra_folder_exclude_patterns" settings.
4444
"""
4545
s = sublime.load_settings("Preferences.sublime-settings")
46-
file_exclude_patterns = s.get('extra_file_exclude_patterns', [])
47-
folder_exclude_patterns = s.get('extra_folder_exclude_patterns', [])
46+
file_exclude_patterns = s.get('extra_file_exclude_patterns', []) or []
47+
folder_exclude_patterns = s.get('extra_folder_exclude_patterns', []) or []
4848
for path in all_ignored_paths():
4949
is_directory = os.path.isdir(path)
5050
if platform.system() == 'Windows':
@@ -63,13 +63,13 @@ def update_file_exclude_patterns():
6363
file_exclude_patterns.append(path)
6464

6565
new_files = set(file_exclude_patterns)
66-
old_files = set(s.get('file_exclude_patterns', []))
66+
old_files = set(s.get('file_exclude_patterns', []) or [])
6767
new_folders = set(folder_exclude_patterns)
68-
old_folders = set(s.get('folder_exclude_patterns', []))
69-
68+
old_folders = set(s.get('folder_exclude_patterns', []) or [])
69+
7070
# Only make changes if anything has actually changed, to avoid spamming the
7171
# sublime console
72-
if new_files != old_files or new_folders != old_folders:
72+
if new_files != old_files or new_folders != old_folders:
7373
s.set('file_exclude_patterns', list(file_exclude_patterns))
7474
s.set('folder_exclude_patterns', list(folder_exclude_patterns))
7575
sublime.save_settings("Preferences.sublime-settings")
@@ -79,32 +79,32 @@ def all_ignored_paths():
7979
Returns a list of all .gitignored files or folders contained in repos
8080
contained within or containing any folders open in any open windows.
8181
"""
82-
82+
8383
open_folders = set()
8484
for window in sublime.windows():
8585
open_folders.update(window.folders())
86-
86+
8787
all_ignored_paths = set()
8888
for folder in open_folders:
8989
ignored_paths = folder_ignored_paths(folder)
9090
all_ignored_paths.update(ignored_paths)
91-
91+
9292
return list(all_ignored_paths)
9393

9494
def folder_ignored_paths(folder):
9595
"""
9696
Returns a list (without duplicates, in the case of weird repo-nesting) of
9797
all files/folders within the given folder that are git ignored.
98-
99-
The folder itself need not be the top level of a git repo, nor even within
98+
99+
The folder itself need not be the top level of a git repo, nor even within
100100
a git repo.
101101
"""
102-
102+
103103
all_ignored_paths = set()
104-
104+
105105
# First find all repos CONTAINED in this folder:
106106
repos = set(find_git_repos(folder))
107-
107+
108108
# Then, additionally, if this folder is itself contained within a repo,
109109
# find the .git folder of the repo containing it:
110110
if is_in_git_repo(folder):
@@ -114,30 +114,30 @@ def folder_ignored_paths(folder):
114114
for git_repo in repos:
115115
ignored_paths = repo_ignored_paths(git_repo)
116116
all_ignored_paths.update(ignored_paths)
117-
117+
118118
return list(all_ignored_paths)
119119

120120
def is_in_git_repo(folder):
121121
"""
122122
Returns true if the given folder is contained within a git repo
123123
"""
124-
124+
125125
exit_code = subprocess.call(
126126
["git", "rev-parse", "--is-inside-work-tree"],
127127
cwd=folder,
128128
stdout=devnull,
129129
stderr=devnull,
130130
startupinfo=startupinfo
131131
)
132-
132+
133133
return exit_code == 0
134134

135135
def parent_repo_path(folder):
136136
"""
137137
Takes the path to a folder contained within a git repo, and returns the
138138
parent repo.
139139
"""
140-
140+
141141
# abspath call converts forward slashes to backslashes on Windows; we do
142142
# this wherever necessary to keep the format of our paths standardised on
143143
# Windows.
@@ -154,7 +154,7 @@ def find_git_repos(folder):
154154
"""
155155
Returns a list of all git repos within the given ancestor folder.
156156
"""
157-
157+
158158
return [root for root, subfolders, files
159159
in os.walk(folder)
160160
if '.git' in subfolders]
@@ -164,7 +164,7 @@ def repo_ignored_paths(git_repo):
164164
Takes the path of a git repo and lists all ignored files/folders in the
165165
repo.
166166
"""
167-
167+
168168
# Trick for listing ignored files nicked from
169169
# http://stackoverflow.com/a/2196755/1709587
170170
command_output = subprocess.Popen(
@@ -174,38 +174,37 @@ def repo_ignored_paths(git_repo):
174174
startupinfo=startupinfo,
175175
env={'LANG':'C'}
176176
).stdout.read()
177-
177+
178178
command_output = command_output.decode('utf-8', 'ignore')
179-
179+
180180
if command_output.isspace() or command_output == u'':
181-
return []
182-
181+
return []
182+
183183
lines = command_output.strip().split(u'\n')
184184
# Each line in `lines` now looks something like:
185185
# "Would remove foo/bar/yourfile.txt"
186-
186+
187187
relative_paths = [line.replace(u'Would remove ', u'', 1).rstrip(u'/')
188188
for line in lines]
189189
absolute_paths = [os.path.join(git_repo, path) for path in relative_paths]
190-
191190
return absolute_paths
192-
191+
193192
def is_first_launch():
194193
s = sublime.load_settings("gitignorer.sublime-settings")
195194
return not s.get('_sublime_gitignorer_has_run', False)
196-
195+
197196
def migrate_exclude_patterns():
198197
"""
199198
Runs on first launch; purpose is to prevent people who have already set
200199
exclusion patterns from losing them when they install this package.
201200
"""
202201
s = sublime.load_settings("Preferences.sublime-settings")
203-
existing_file_exclude_patterns = s.get('file_exclude_patterns', [])
204-
existing_folder_exclude_patterns = s.get('folder_exclude_patterns', [])
202+
existing_file_exclude_patterns = s.get('file_exclude_patterns', []) or []
203+
existing_folder_exclude_patterns = s.get('folder_exclude_patterns', []) or []
205204
s.set('extra_file_exclude_patterns', existing_file_exclude_patterns)
206205
s.set('extra_folder_exclude_patterns', existing_folder_exclude_patterns)
207206
sublime.save_settings("Preferences.sublime-settings")
208-
207+
209208
def record_first_launch():
210209
s = sublime.load_settings("gitignorer.sublime-settings")
211210
s.set('_sublime_gitignorer_has_run', True)
@@ -225,5 +224,5 @@ def windows_path_to_sublime_path(path):
225224
assert(path[1] == u':')
226225
without_colon = path[0] + path[2:]
227226
return without_colon.replace(u'\\', u'/')
228-
227+
229228
start()

0 commit comments

Comments
 (0)