-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor workspace initialization and fix bug in
tsrc apply-manifest
Workspace instances are now built with a WorkspaceConfig and LocalManifest instances. LocalManifest is abstract and is either implemented by: * ClonedManifest, which represents a manifest cloned in <workspace_path>/.tsrc/manifest, * or ManifestCopy, which represents a manifest located in the file system This allows for a more robust implementation of the `apply-manifest` command Fix #268
- Loading branch information
1 parent
e17921a
commit 4865e9f
Showing
11 changed files
with
170 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from pathlib import Path | ||
|
||
import tsrc.manifest | ||
from tsrc.workspace.local_manifest import LocalManifest | ||
|
||
|
||
class ClonedManifest(LocalManifest): | ||
"""Represent a manifest repository that has been cloned locally | ||
inside `<workspace>/.tsrc/manifest`. | ||
Usage: | ||
>>> cloned_manifest = ClonedManifest(workspace / ".tsrc/manifest"), | ||
url="git@acme.com/manifest.git", branch="devel" | ||
) | ||
# First, update the cloned repository using the remote git URL and the | ||
# branch passed in the constructor | ||
>> cloned_manifest.update() | ||
# Then, read the `manifest.yml` file from the clone repository: | ||
>>> manifest = cloned_manifest.get_manifest() | ||
""" | ||
|
||
def __init__(self, clone_path: Path, *, url: str, branch: str) -> None: | ||
self.clone_path = clone_path | ||
self.url = url | ||
self.branch = branch | ||
|
||
def update(self) -> None: | ||
if self.clone_path.exists(): | ||
self._reset_manifest_clone() | ||
else: | ||
self._clone_manifest() | ||
|
||
def get_manifest(self) -> tsrc.manifest.Manifest: | ||
return tsrc.manifest.load(self.clone_path / "manifest.yml") | ||
|
||
def _reset_manifest_clone(self) -> None: | ||
tsrc.git.run(self.clone_path, "remote", "set-url", "origin", self.url) | ||
|
||
tsrc.git.run(self.clone_path, "fetch") | ||
tsrc.git.run(self.clone_path, "checkout", "-B", self.branch) | ||
# fmt: off | ||
tsrc.git.run( | ||
self.clone_path, "branch", self.branch, | ||
"--set-upstream-to", f"origin/{self.branch}" | ||
) | ||
# fmt: on | ||
ref = f"origin/{self.branch}" | ||
tsrc.git.run(self.clone_path, "reset", "--hard", ref) | ||
|
||
def _clone_manifest(self) -> None: | ||
parent = self.clone_path.parent | ||
name = self.clone_path.name | ||
parent.mkdir(parents=True, exist_ok=True) | ||
tsrc.git.run( | ||
self.clone_path.parent, "clone", self.url, "--branch", self.branch, name | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,14 @@ | ||
from pathlib import Path | ||
from typing import List, Optional, Tuple, cast # noqa | ||
import abc | ||
|
||
import tsrc | ||
import tsrc.manifest | ||
|
||
|
||
class LocalManifest: | ||
"""Represent a manifest repository that has been cloned locally | ||
inside `<workspace>/.tsrc/manifest`. | ||
Usage: | ||
>>> local_manifest = LocalManifest(Path(workspace / ".tsrc/manifest") | ||
# First, update the cloned repository using a remote git URL and a | ||
# branch: | ||
>>> manifest.update("git@acme.com/manifest.git", branch="devel") | ||
# Then, read the `manifest.yml` file from the clone repository: | ||
>>> manifest = local_manifest.get_manifest() | ||
""" | ||
|
||
def __init__(self, clone_path: Path) -> None: | ||
self.clone_path = clone_path | ||
|
||
def update(self, url: str, *, branch: str) -> None: | ||
if self.clone_path.exists(): | ||
self._reset_manifest_clone(url, branch=branch) | ||
else: | ||
self._clone_manifest(url, branch=branch) | ||
class LocalManifest(metaclass=abc.ABCMeta): | ||
@abc.abstractmethod | ||
def update(self) -> None: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def get_manifest(self) -> tsrc.manifest.Manifest: | ||
return tsrc.manifest.load(self.clone_path / "manifest.yml") | ||
|
||
def _reset_manifest_clone(self, url: str, *, branch: str) -> None: | ||
tsrc.git.run(self.clone_path, "remote", "set-url", "origin", url) | ||
|
||
tsrc.git.run(self.clone_path, "fetch") | ||
tsrc.git.run(self.clone_path, "checkout", "-B", branch) | ||
# fmt: off | ||
tsrc.git.run( | ||
self.clone_path, "branch", branch, | ||
"--set-upstream-to", f"origin/{branch}" | ||
) | ||
# fmt: on | ||
ref = f"origin/{branch}" | ||
tsrc.git.run(self.clone_path, "reset", "--hard", ref) | ||
|
||
def _clone_manifest(self, url: str, *, branch: str) -> None: | ||
parent = self.clone_path.parent | ||
name = self.clone_path.name | ||
parent.mkdir(parents=True, exist_ok=True) | ||
tsrc.git.run(self.clone_path.parent, "clone", url, "--branch", branch, name) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from pathlib import Path | ||
|
||
import tsrc.manifest | ||
from tsrc.workspace.local_manifest import LocalManifest | ||
|
||
|
||
class ManifestCopy(LocalManifest): | ||
def __init__(self, manifest_copy_path: Path): | ||
self.manifest_copy_path = manifest_copy_path | ||
|
||
def update(self) -> None: | ||
pass | ||
|
||
def get_manifest(self) -> tsrc.manifest.Manifest: | ||
return tsrc.manifest.load(self.manifest_copy_path) |