1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
9 from environment
import IsTest
10 from file_system
import FileNotFoundError
, FileSystem
, StatInfo
11 from future
import Future
12 from local_file_system
import LocalFileSystem
13 from path_util
import IsDirectory
16 class LocalGitFileSystem(FileSystem
):
17 '''Class to fetch filesystem data from this script's local git repository.
20 def Create(cls
, branch
='master', commit
=None):
22 return LocalFileSystem
.Create('')
23 return LocalGitFileSystem(branch
, commit
)
25 def __init__(self
, branch
, pinned_commit
):
27 self
._pinned
_commit
= pinned_commit
28 if self
._pinned
_commit
:
29 commit
= self
._pinned
_commit
31 if branch
!= 'master':
32 commit
= 'branch-heads/%s' % branch
34 commit
= 'origin/master'
36 self
._commit
= local_git_util
.ParseRevision(commit
)
38 # We ignore ImportErrors here. It means we're running in AppEngine, so
39 # this doesn't need to work anyway.
42 def Read(self
, paths
, skip_not_found
=False):
44 def get_entry_name(entry
):
45 if entry
['type'] == 'tree':
46 return entry
['name'] + '/'
52 return [get_entry_name(e
)
53 for e
in local_git_util
.ListDir(path
, self
._commit
)]
55 return local_git_util
.ReadFile(path
, self
._commit
)
56 except FileNotFoundError
as e
:
61 results
= dict((path
, read_path(path
)) for path
in paths
)
62 return Future(value
=dict((k
, v
) for k
, v
in results
.iteritems()
66 return Future(value
=())
68 def GetCommitID(self
):
69 '''Returns a future that resolves to the commit ID for this file system's
72 return Future(value
=self
._commit
)
74 def GetPreviousCommitID(self
):
75 '''Returns a future that resolves to the parent commit ID of this file
78 return Future(value
=local_git_util
.GetParentRevision(self
._commit
))
80 def StatAsync(self
, path
):
82 def get_child_versions(path
):
83 return dict((e
['name'], e
['id'])
84 for e
in local_git_util
.ListDir(path
, self
._commit
))
86 def get_file_version(dir, filename
):
88 return next(e
['id'] for e
in local_git_util
.ListDir(dir, self
._commit
)
89 if e
['name'] == filename
)
91 raise FileNotFoundError('%s not found in revision %s' %
94 dir, filename
= posixpath
.split(path
)
96 version
= local_git_util
.GetRootTree(self
._commit
)
97 child_versions
= get_child_versions('')
98 elif IsDirectory(path
):
99 parent_dir
, stat_dir
= posixpath
.split(dir)
100 version
= get_file_version(parent_dir
, stat_dir
)
101 child_versions
= get_child_versions(dir)
103 version
= get_file_version(dir, filename
)
104 child_versions
= None
106 #print 'Accessing local git for stat on %s (%s)' % (path, version)
107 return Future(value
=StatInfo(version
, child_versions
))
109 def GetIdentity(self
):
110 if self
._branch
== 'master':
111 # A master FS always carries the same identity even if pinned to a commit.
113 elif self
._pinned
_commit
is not None:
114 str_id
= self
._pinned
_commit
116 str_id
= 'branch-heads/%s' % self
._branch
117 return '@'.join((self
.__class
__.__name
__, str_id
))
119 def GetVersion(self
):
120 return self
._pinned
_commit