Checkouts the given path at the given commit.
(self, path, commit)
| 878 | index.add(git_path) |
| 879 | |
| 880 | def checkout_file(self, path, commit): |
| 881 | """Checkouts the given path at the given commit.""" |
| 882 | _check_path_is_repo_relative(path) |
| 883 | |
| 884 | git_path = _get_git_path(path) |
| 885 | o = self.gl_repo.git_repo[commit.tree[git_path].id] |
| 886 | assert o.type != pygit2.GIT_OBJ_COMMIT |
| 887 | assert o.type != pygit2.GIT_OBJ_TAG |
| 888 | |
| 889 | if o.type == pygit2.GIT_OBJ_BLOB: |
| 890 | full_path = os.path.join(self.gl_repo.root, path) |
| 891 | dirname = os.path.dirname(full_path) |
| 892 | if not os.path.exists(dirname): |
| 893 | try: |
| 894 | os.makedirs(dirname) |
| 895 | except OSError as exc: # guard against race condition |
| 896 | if exc.errno != errno.EEXIST: |
| 897 | raise |
| 898 | with io.open(full_path, mode='wb') as dst: |
| 899 | dst.write(o.data) |
| 900 | |
| 901 | # So as to not get confused with the status of the file we also add it. |
| 902 | # This prevents getting into a situation in which the staged version is |
| 903 | # different from the working version. In such a case, the file would |
| 904 | # appear as modified to Gitless when it shouldn't. This is also consistent |
| 905 | # with the behavior of `git checkout <commit> <file>` that also adds the |
| 906 | # file to the staging area. |
| 907 | with self._index as index: |
| 908 | index.add(git_path) |
| 909 | |
| 910 | elif o.type == pygit2.GIT_OBJ_TREE: |
| 911 | raise PathIsDirectoryError( |
| 912 | 'Path {0} at {1} is a directory and not a file'.format( |
| 913 | path, commit.id)) |
| 914 | else: |
| 915 | raise Exception('Unexpected object type {0}'.format(o.type)) |
| 916 | |
| 917 | def get_paths(self, path, commit): |
| 918 | """Return a generator of all filepaths under path at commit.""" |