A Gitless's repository. Attributes: path: absolute path to the Gitless's dir (the .git dir). root: absolute path to the root of this repository. cwd: the current working directory relative to the root of this repository ('' if they are equal). config: the repository's config
| 100 | |
| 101 | |
| 102 | class Repository(object): |
| 103 | """A Gitless's repository. |
| 104 | |
| 105 | Attributes: |
| 106 | path: absolute path to the Gitless's dir (the .git dir). |
| 107 | root: absolute path to the root of this repository. |
| 108 | cwd: the current working directory relative to the root of this |
| 109 | repository ('' if they are equal). |
| 110 | config: the repository's configuration. |
| 111 | current_branch: the current branch (a Branch object). |
| 112 | remotes: the configured remotes (see RemoteCollection). |
| 113 | """ |
| 114 | |
| 115 | def __init__(self): |
| 116 | """Create a Repository out of the current working repository.""" |
| 117 | try: |
| 118 | path = error_on_none(pygit2.discover_repository(os.getcwd())) |
| 119 | except KeyError: |
| 120 | raise NotInRepoError('You are not in a Gitless\'s repository') |
| 121 | |
| 122 | self.git_repo = pygit2.Repository(path) |
| 123 | self.remotes = RemoteCollection(self.git_repo.remotes, self) |
| 124 | self.path = self.git_repo.path |
| 125 | self.root = self.path[:-6] # strip trailing /.git/ |
| 126 | self.config = self.git_repo.config |
| 127 | |
| 128 | @property |
| 129 | def cwd(self): |
| 130 | ret = os.path.relpath(os.getcwd(), self.root) |
| 131 | return '' if ret == '.' else ret |
| 132 | |
| 133 | def revparse_single(self, revision): |
| 134 | if '/' in revision: # might be a remote branch |
| 135 | remote, remote_branch = revision.split('/', 1) |
| 136 | try: |
| 137 | return self.remotes[remote].lookup_branch(remote_branch).head |
| 138 | except KeyError: |
| 139 | pass |
| 140 | try: |
| 141 | return self.git_repo.revparse_single(revision) |
| 142 | except (KeyError, ValueError): |
| 143 | raise ValueError('No commit found for {0}'.format(revision)) |
| 144 | |
| 145 | def merge_base(self, b1, b2): |
| 146 | try: |
| 147 | return self.git_repo.merge_base(b1.target, b2.target) |
| 148 | except KeyError: |
| 149 | raise GlError('No common commit found between {0} and {1}'.format(b1, b2)) |
| 150 | |
| 151 | def _fuse_commits_fp(self, b): |
| 152 | return os.path.join( |
| 153 | self.path, 'GL_FUSE_CIS_{0}'.format(b.branch_name.replace('/', '_'))) |
| 154 | |
| 155 | def _ref_exists(self, ref): |
| 156 | try: |
| 157 | self.git_repo.lookup_reference(ref) |
| 158 | return True |
| 159 | except KeyError: |