Skip to content

Commit 41984eb

Browse files
committed
fix
1 parent 8d83a7a commit 41984eb

File tree

6 files changed

+138
-34
lines changed

6 files changed

+138
-34
lines changed

gitfs/cache/gitignore.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import fnmatch
1919

2020
from six import string_types
21-
21+
from gitfs.log import log
2222

2323
class CachedIgnore(object):
2424
def __init__(self, ignore=False, submodules=False, exclude=False,
@@ -38,6 +38,8 @@ def __init__(self, ignore=False, submodules=False, exclude=False,
3838
def update(self):
3939
self.items = ['.git', '.git/*', '/.git/*', '*.keep', '*.gitmodules']
4040

41+
log.debug("[ignore] CachedIgnore.update" )
42+
4143
self.items += self._parse_ignore_file(self.ignore)
4244
self.items += self._parse_ignore_file(self.exclude)
4345

@@ -57,6 +59,7 @@ def update(self):
5759
def _parse_ignore_file(self, ignore_file):
5860
items = []
5961

62+
log.debug("[ignore] _parse_ignore_file: %s", ignore_file)
6063
if ignore_file and os.path.exists(ignore_file):
6164
with open(ignore_file) as gitignore:
6265
for item in gitignore.readlines():
@@ -77,7 +80,9 @@ def __contains__(self, path):
7780
def check_key(self, key):
7881
for item in self.items:
7982
if self._check_item_and_key(item, key):
83+
log.debug("[ignore] check_key match:%s,%s", item, key)
8084
return True
85+
8186
return False
8287

8388
def _check_item_and_key(self, item, key):

gitfs/repository.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515

1616
import os
17+
import sys
1718
from collections import namedtuple
1819
from shutil import rmtree
1920
from stat import S_IFDIR, S_IFREG, S_IFLNK
2021

21-
from pygit2 import (clone_repository, Signature, GIT_SORT_TOPOLOGICAL,
22+
from pygit2 import (clone_repository, discover_repository, init_repository, Signature, GIT_SORT_TOPOLOGICAL,
2223
GIT_FILEMODE_TREE, GIT_STATUS_CURRENT,
2324
GIT_FILEMODE_LINK, GIT_FILEMODE_BLOB, GIT_BRANCH_REMOTE,
24-
GIT_BRANCH_LOCAL, GIT_FILEMODE_BLOB_EXECUTABLE)
25+
GIT_BRANCH_LOCAL, GIT_FILEMODE_BLOB_EXECUTABLE, GIT_REPOSITORY_INIT_NO_REINIT)
2526
from six import iteritems
2627

2728
from gitfs.cache import CommitCache
@@ -185,10 +186,38 @@ def clone(cls, remote_url, path, branch=None, credentials=None):
185186
clone. The default is to use the remote's default branch.
186187
187188
"""
189+
190+
hasExistingRepo = False
191+
try:
192+
existingRepoPath = discover_repository(path)
193+
if existingRepoPath<>"":
194+
hasExistingRepo = True
195+
except Exception, e:
196+
log.debug("[Exception] discover_repository repo not found: %s", str(e))
197+
pass
188198

189-
repo = clone_repository(remote_url, path, checkout_branch=branch,
199+
if hasExistingRepo == False:
200+
log.debug("clone_repository %s", path)
201+
202+
try:
203+
repo = clone_repository(remote_url, path, checkout_branch=branch,
190204
callbacks=credentials)
191-
repo.checkout_head()
205+
except Exception, e:
206+
log.error("[Exception] clone_repository failed: %s", str(e))
207+
sys.exit()
208+
209+
repo.checkout_head()
210+
log.info("repo cloned")
211+
else:
212+
log.debug("init_repository %s", existingRepoPath)
213+
try:
214+
repo = init_repository(existingRepoPath)
215+
except Exception, e:
216+
log.error("[Exception] init_repository failed: %s", str(e))
217+
sys.exit()
218+
219+
log.info("existing repo '%s' opened", existingRepoPath)
220+
192221
return cls(repo)
193222

194223
def _is_searched_entry(self, entry_name, searched_entry, path_components):

gitfs/router.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import time
1919
import shutil
2020
import inspect
21+
import sys
2122

2223
from pwd import getpwnam
2324
from grp import getgrnam
@@ -80,7 +81,11 @@ def __init__(self, remote_url, repo_path, mount_path,
8081
self.max_size = kwargs['max_size']
8182
self.max_offset = kwargs['max_offset']
8283

83-
self.repo.commits.update()
84+
try:
85+
self.repo.commits.update()
86+
except Exception, e:
87+
log.error("[Exception] repo.commits.update failed: %s", str(e))
88+
sys.exit()
8489

8590
self.workers = []
8691

@@ -99,7 +104,7 @@ def destroy(self, path):
99104
worker.join()
100105
log.debug('Workers stopped')
101106

102-
shutil.rmtree(self.repo_path)
107+
#shutil.rmtree(self.repo_path)
103108
log.info('Successfully umounted %s', self.mount_path)
104109

105110
def __call__(self, operation, *args):

gitfs/utils/decorators/not_in.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from six import string_types
2121

2222
from fuse import FuseOSError
23-
23+
from gitfs.log import log
2424

2525
class not_in(object):
2626
def __init__(self, look_at, check=None):
@@ -34,7 +34,17 @@ def decorated(their_self, *args, **kwargs):
3434
if isinstance(self.look_at, string_types):
3535
self.look_at = getattr(their_self, self.look_at)
3636

37-
self.check_args(f, args)
37+
#self.check_args(f, args)
38+
39+
gitignore = False
40+
try:
41+
self.check_args(f, args)
42+
except Exception, e:
43+
gitignore = True
44+
pass
45+
46+
kwargs["gitignore"] = gitignore
47+
3848
result = f(their_self, *args, **kwargs)
3949

4050
return result

gitfs/views/current.py

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ def __init__(self, *args, **kwargs):
3838

3939
@write_operation
4040
@not_in("ignore", check=["old", "new"])
41-
def rename(self, old, new):
41+
def rename(self, old, new, gitignore):
4242
new = re.sub(self.regex, '', new)
4343
result = super(CurrentView, self).rename(old, new)
4444

45+
if gitignore == True:
46+
log.debug("[ignore] rename:%s", name)
47+
return result
48+
4549
message = "Rename {} to {}".format(old, new)
4650
self._stage(**{
4751
'remove': os.path.split(old)[1],
@@ -54,9 +58,13 @@ def rename(self, old, new):
5458

5559
@write_operation
5660
@not_in("ignore", check=["target"])
57-
def symlink(self, name, target):
61+
def symlink(self, name, target, gitignore):
5862
result = os.symlink(target, self.repo._full_path(name))
59-
63+
64+
if gitignore == True:
65+
log.debug("[ignore] symlink:%s", name)
66+
return result
67+
6068
message = "Create symlink to {} for {}".format(target, name)
6169
self._stage(add=name, message=message)
6270

@@ -65,12 +73,16 @@ def symlink(self, name, target):
6573

6674
@write_operation
6775
@not_in("ignore", check=["target"])
68-
def link(self, name, target):
76+
def link(self, name, target, gitignore):
6977
if target.startswith('/%s/' % self.current_path):
7078
target = target.replace('/%s/' % self.current_path, '/')
7179

7280
result = super(CurrentView, self).link(target, name)
73-
81+
82+
if gitignore == True:
83+
log.debug("[ignore] link:%s", name)
84+
return result
85+
7486
message = "Create link to {} for {}".format(target, name)
7587
self._stage(add=name, message=message)
7688

@@ -96,17 +108,22 @@ def getattr(self, path, fh=None):
96108

97109
@write_operation
98110
@not_in("ignore", check=["path"])
99-
def write(self, path, buf, offset, fh):
111+
def write(self, path, buf, offset, fh, gitignore):
100112
"""
101113
We don't like big big files, so we need to be really carefull
102114
with them. First we check for offset, then for size. If any of this
103115
is off limit, raise EFBIG error and delete the file.
104116
"""
105117

106-
if offset + len(buf) > self.max_size:
118+
if ( self.max_size > 0 ) and ( offset + len(buf) > self.max_size ):
107119
raise FuseOSError(errno.EFBIG)
108120

109121
result = super(CurrentView, self).write(path, buf, offset, fh)
122+
123+
if gitignore == True:
124+
log.debug("[ignore] write:%s. Wrote %s bytes to %s", path, len(buf), path)
125+
return result
126+
110127
self.dirty[fh] = {
111128
'message': 'Update {}'.format(path),
112129
'stage': True
@@ -117,9 +134,13 @@ def write(self, path, buf, offset, fh):
117134

118135
@write_operation
119136
@not_in("ignore", check=["path"])
120-
def mkdir(self, path, mode):
137+
def mkdir(self, path, mode, gitignore):
121138
result = super(CurrentView, self).mkdir(path, mode)
122-
139+
140+
if gitignore == True:
141+
log.debug("[ignore] mkdir:%s", path)
142+
return result
143+
123144
keep_path = "{}/.keep".format(path)
124145
full_path = self.repo._full_path(keep_path)
125146
if not os.path.exists(keep_path):
@@ -141,10 +162,15 @@ def mkdir(self, path, mode):
141162

142163
return result
143164

144-
def create(self, path, mode, fi=None):
165+
@not_in("ignore", check=["path"])
166+
def create(self, path, mode, fi=None, gitignore=False):
145167
fh = self.open_for_write(path, os.O_WRONLY | os.O_CREAT)
146168
super(CurrentView, self).chmod(path, mode)
147169

170+
if gitignore == True:
171+
log.debug("[ignore] create:%s", path)
172+
return fh
173+
148174
self.dirty[fh] = {
149175
'message': "Created {}".format(path),
150176
'stage': True
@@ -155,10 +181,11 @@ def create(self, path, mode, fi=None):
155181

156182
@write_operation
157183
@not_in("ignore", check=["path"])
158-
def chmod(self, path, mode):
184+
def chmod(self, path, mode, gitignore):
159185
"""
160186
Executes chmod on the file at os level and then it commits the change.
161187
"""
188+
162189
str_mode = ('%o' % mode)[-4:]
163190
if str_mode not in ['0755', '0644']:
164191
raise FuseOSError(errno.EINVAL)
@@ -168,6 +195,10 @@ def chmod(self, path, mode):
168195
if os.path.isdir(self.repo._full_path(path)):
169196
return result
170197

198+
if gitignore == True:
199+
log.debug("[ignore] chmod:%s", path)
200+
return result
201+
171202
message = 'Chmod to {} on {}'.format(str_mode, path)
172203
self._stage(add=path, message=message)
173204

@@ -177,13 +208,17 @@ def chmod(self, path, mode):
177208

178209
@write_operation
179210
@not_in("ignore", check=["path"])
180-
def fsync(self, path, fdatasync, fh):
211+
def fsync(self, path, fdatasync, fh, gitignore):
181212
"""
182213
Each time you fsync, a new commit and push are made
183214
"""
184215

185216
result = super(CurrentView, self).fsync(path, fdatasync, fh)
186-
217+
218+
if gitignore == True:
219+
log.debug("[ignore] fsync:%s", path)
220+
return result
221+
187222
message = 'Fsync {}'.format(path)
188223
self._stage(add=path, message=message)
189224

@@ -192,10 +227,16 @@ def fsync(self, path, fdatasync, fh):
192227

193228
@write_operation
194229
@not_in("ignore", check=["path"])
195-
def open_for_write(self, path, flags):
196-
global writers
230+
def open_for_write(self, path, flags, gitignore):
197231
fh = self.open_for_read(path, flags)
198-
writers += 1
232+
233+
if gitignore == True:
234+
log.debug("[ignore] open_for_write:%s", path)
235+
return fh
236+
237+
global writers
238+
writers += 1
239+
199240
self.dirty[fh] = {
200241
'message': "Opened {} for write".format(path),
201242
'stage': False
@@ -207,21 +248,30 @@ def open_for_write(self, path, flags):
207248
def open_for_read(self, path, flags):
208249
full_path = self.repo._full_path(path)
209250
log.info("CurrentView: Open %s for read", path)
251+
210252
return os.open(full_path, flags)
211253

212254
def open(self, path, flags):
213255
write_mode = flags & (os.O_WRONLY | os.O_RDWR |
214256
os.O_APPEND | os.O_CREAT)
215257
if write_mode:
258+
log.debug("[ignore] open.write_mode: %s", path)
216259
return self.open_for_write(path, flags)
260+
261+
log.debug("[ignore] open.read_mode: %s", path)
217262
return self.open_for_read(path, flags)
218263

219-
def release(self, path, fh):
264+
@not_in("ignore", check=["path"])
265+
def release(self, path, fh, gitignore):
220266
"""
221267
Check for path if something was written to. If so, commit and push
222268
the changed to upstream.
223269
"""
224270

271+
if gitignore == True:
272+
log.debug("[ignore] release:%s", path)
273+
return os.close(fh)
274+
225275
if fh in self.dirty:
226276
message = self.dirty[fh]['message']
227277
should_stage = self.dirty[fh].get('stage', False)
@@ -238,7 +288,7 @@ def release(self, path, fh):
238288

239289
@write_operation
240290
@not_in("ignore", check=["path"])
241-
def rmdir(self, path):
291+
def rmdir(self, path, gitignore):
242292
message = 'Delete the {} directory'.format(path)
243293

244294
# Unlink all the files
@@ -248,7 +298,8 @@ def rmdir(self, path):
248298
deleting_file = os.path.join(root, _file)
249299
if os.path.exists(deleting_file):
250300
result = super(CurrentView, self).unlink(os.path.join(path, _file))
251-
self._stage(remove=os.path.join(path, _file), message=message)
301+
if gitignore == False:
302+
self._stage(remove=os.path.join(path, _file), message=message)
252303

253304
# Delete the actual directory
254305
result = super(CurrentView, self).rmdir("{}/".format(path))
@@ -258,9 +309,13 @@ def rmdir(self, path):
258309

259310
@write_operation
260311
@not_in("ignore", check=["path"])
261-
def unlink(self, path):
312+
def unlink(self, path, gitignore):
262313
result = super(CurrentView, self).unlink(path)
263-
314+
315+
if gitignore == True:
316+
log.debug("[ignore] unlink:%s", path)
317+
return result
318+
264319
message = 'Deleted {}'.format(path)
265320
self._stage(remove=path, message=message)
266321

0 commit comments

Comments
 (0)