diff options
author | Kevin Degi <kdegi@codeaurora.org> | 2015-07-27 13:33:43 -0600 |
---|---|---|
committer | Kevin Degi <kdegi@codeaurora.org> | 2015-07-27 13:33:43 -0600 |
commit | b1a07b8276c02894ae1901d186d916bde2693f05 (patch) | |
tree | e70a0ae484ff12f32fb8bdaa01841ce48dbb2bee | |
parent | b3d6e67196b4f9d85f3c11453a9502a6e71364d7 (diff) | |
download | git-repo-b1a07b8276c02894ae1901d186d916bde2693f05.tar.gz |
InitGitDir: Clean up created directories
If _InitGitDir fails, it leaves any progress it had made on the file
system. This can cause subsequent calls to repo sync to behave
differently. This is especially evident when _CheckDirReference() fails,
since it will not be invoked when sync is retried because both the
source and destination directories already exist.
To address this, have _InitGitDir() clean up any directories it has created
if it catches an exception. Also behave the same way for _InitWorkTree().
Change-Id: Ic16bb3feea649e115b59bd44be294e89e3692aeb
-rw-r--r-- | project.py | 114 |
1 files changed, 63 insertions, 51 deletions
@@ -2169,54 +2169,61 @@ class Project(object): | |||
2169 | def _InitGitDir(self, mirror_git=None): | 2169 | def _InitGitDir(self, mirror_git=None): |
2170 | init_git_dir = not os.path.exists(self.gitdir) | 2170 | init_git_dir = not os.path.exists(self.gitdir) |
2171 | init_obj_dir = not os.path.exists(self.objdir) | 2171 | init_obj_dir = not os.path.exists(self.objdir) |
2172 | # Initialize the bare repository, which contains all of the objects. | 2172 | try: |
2173 | if init_obj_dir: | 2173 | # Initialize the bare repository, which contains all of the objects. |
2174 | os.makedirs(self.objdir) | 2174 | if init_obj_dir: |
2175 | self.bare_objdir.init() | 2175 | os.makedirs(self.objdir) |
2176 | self.bare_objdir.init() | ||
2176 | 2177 | ||
2177 | # If we have a separate directory to hold refs, initialize it as well. | 2178 | # If we have a separate directory to hold refs, initialize it as well. |
2178 | if self.objdir != self.gitdir: | 2179 | if self.objdir != self.gitdir: |
2179 | if init_git_dir: | 2180 | if init_git_dir: |
2180 | os.makedirs(self.gitdir) | 2181 | os.makedirs(self.gitdir) |
2181 | 2182 | ||
2182 | if init_obj_dir or init_git_dir: | 2183 | if init_obj_dir or init_git_dir: |
2183 | self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False, | 2184 | self._ReferenceGitDir(self.objdir, self.gitdir, share_refs=False, |
2184 | copy_all=True) | 2185 | copy_all=True) |
2185 | self._CheckDirReference(self.objdir, self.gitdir, share_refs=False) | 2186 | self._CheckDirReference(self.objdir, self.gitdir, share_refs=False) |
2186 | 2187 | ||
2187 | if init_git_dir: | 2188 | if init_git_dir: |
2188 | mp = self.manifest.manifestProject | 2189 | mp = self.manifest.manifestProject |
2189 | ref_dir = mp.config.GetString('repo.reference') or '' | 2190 | ref_dir = mp.config.GetString('repo.reference') or '' |
2190 | 2191 | ||
2191 | if ref_dir or mirror_git: | 2192 | if ref_dir or mirror_git: |
2192 | if not mirror_git: | 2193 | if not mirror_git: |
2193 | mirror_git = os.path.join(ref_dir, self.name + '.git') | 2194 | mirror_git = os.path.join(ref_dir, self.name + '.git') |
2194 | repo_git = os.path.join(ref_dir, '.repo', 'projects', | 2195 | repo_git = os.path.join(ref_dir, '.repo', 'projects', |
2195 | self.relpath + '.git') | 2196 | self.relpath + '.git') |
2196 | 2197 | ||
2197 | if os.path.exists(mirror_git): | 2198 | if os.path.exists(mirror_git): |
2198 | ref_dir = mirror_git | 2199 | ref_dir = mirror_git |
2199 | 2200 | ||
2200 | elif os.path.exists(repo_git): | 2201 | elif os.path.exists(repo_git): |
2201 | ref_dir = repo_git | 2202 | ref_dir = repo_git |
2202 | 2203 | ||
2203 | else: | 2204 | else: |
2204 | ref_dir = None | 2205 | ref_dir = None |
2205 | 2206 | ||
2206 | if ref_dir: | 2207 | if ref_dir: |
2207 | _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'), | 2208 | _lwrite(os.path.join(self.gitdir, 'objects/info/alternates'), |
2208 | os.path.join(ref_dir, 'objects') + '\n') | 2209 | os.path.join(ref_dir, 'objects') + '\n') |
2209 | 2210 | ||
2210 | self._UpdateHooks() | 2211 | self._UpdateHooks() |
2211 | 2212 | ||
2212 | m = self.manifest.manifestProject.config | 2213 | m = self.manifest.manifestProject.config |
2213 | for key in ['user.name', 'user.email']: | 2214 | for key in ['user.name', 'user.email']: |
2214 | if m.Has(key, include_defaults=False): | 2215 | if m.Has(key, include_defaults=False): |
2215 | self.config.SetString(key, m.GetString(key)) | 2216 | self.config.SetString(key, m.GetString(key)) |
2216 | if self.manifest.IsMirror: | 2217 | if self.manifest.IsMirror: |
2217 | self.config.SetString('core.bare', 'true') | 2218 | self.config.SetString('core.bare', 'true') |
2218 | else: | 2219 | else: |
2219 | self.config.SetString('core.bare', None) | 2220 | self.config.SetString('core.bare', None) |
2221 | except Exception: | ||
2222 | if init_obj_dir and os.path.exists(self.objdir): | ||
2223 | shutil.rmtree(self.objdir) | ||
2224 | if init_git_dir and os.path.exists(self.gitdir): | ||
2225 | shutil.rmtree(self.gitdir) | ||
2226 | raise | ||
2220 | 2227 | ||
2221 | def _UpdateHooks(self): | 2228 | def _UpdateHooks(self): |
2222 | if os.path.exists(self.gitdir): | 2229 | if os.path.exists(self.gitdir): |
@@ -2363,23 +2370,28 @@ class Project(object): | |||
2363 | def _InitWorkTree(self): | 2370 | def _InitWorkTree(self): |
2364 | dotgit = os.path.join(self.worktree, '.git') | 2371 | dotgit = os.path.join(self.worktree, '.git') |
2365 | init_dotgit = not os.path.exists(dotgit) | 2372 | init_dotgit = not os.path.exists(dotgit) |
2366 | if init_dotgit: | 2373 | try: |
2367 | os.makedirs(dotgit) | 2374 | if init_dotgit: |
2368 | self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True, | 2375 | os.makedirs(dotgit) |
2369 | copy_all=False) | 2376 | self._ReferenceGitDir(self.gitdir, dotgit, share_refs=True, |
2377 | copy_all=False) | ||
2370 | 2378 | ||
2371 | self._CheckDirReference(self.gitdir, dotgit, share_refs=True) | 2379 | self._CheckDirReference(self.gitdir, dotgit, share_refs=True) |
2372 | 2380 | ||
2373 | if init_dotgit: | 2381 | if init_dotgit: |
2374 | _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId()) | 2382 | _lwrite(os.path.join(dotgit, HEAD), '%s\n' % self.GetRevisionId()) |
2375 | 2383 | ||
2376 | cmd = ['read-tree', '--reset', '-u'] | 2384 | cmd = ['read-tree', '--reset', '-u'] |
2377 | cmd.append('-v') | 2385 | cmd.append('-v') |
2378 | cmd.append(HEAD) | 2386 | cmd.append(HEAD) |
2379 | if GitCommand(self, cmd).Wait() != 0: | 2387 | if GitCommand(self, cmd).Wait() != 0: |
2380 | raise GitError("cannot initialize work tree") | 2388 | raise GitError("cannot initialize work tree") |
2381 | 2389 | ||
2382 | self._CopyAndLinkFiles() | 2390 | self._CopyAndLinkFiles() |
2391 | except Exception: | ||
2392 | if init_dotgit: | ||
2393 | shutil.rmtree(dotgit) | ||
2394 | raise | ||
2383 | 2395 | ||
2384 | def _gitdir_path(self, path): | 2396 | def _gitdir_path(self, path): |
2385 | return os.path.realpath(os.path.join(self.gitdir, path)) | 2397 | return os.path.realpath(os.path.join(self.gitdir, path)) |