Skip to content

Commit

Permalink
Modify git.clone action to checkout branch after cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Oct 2, 2024
1 parent b09179a commit a69c897
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/clone_and_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def setup(lab: str, zid: str):
directory = actions.git.clone(
f"git@nw-syd-gitlab.cseunsw.tech:COMP2511/{
term}/students/{zid}/{lab}.git",
# branch="marking",
branch="marking",
)
return {
"directory": directory,
Expand Down
4 changes: 2 additions & 2 deletions markten/__utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def show_banner():
class TextCollector:
"""
Collects text when called. When stringified, it produces the output, joined
by newlines.
by newlines. With leading and trailing whitespace stripped.
"""

def __init__(self) -> None:
Expand All @@ -24,4 +24,4 @@ def __call__(self, line: str) -> Any:
self.__output.append(line)

def __str__(self) -> str:
return '\n'.join(self.__output)
return '\n'.join(self.__output).strip()
2 changes: 2 additions & 0 deletions markten/actions/__async_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async def read_stream(
async def run_process(
cmd: tuple[str, ...],
stdin: str = '',
cwd: str | None = None,
*,
on_stdout: Callable[[str], None] | None = None,
on_stderr: Callable[[str], None] | None = None,
Expand All @@ -32,6 +33,7 @@ async def run_process(
"""
process = await asyncio.create_subprocess_exec(
*cmd,
cwd=cwd,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
Expand Down
28 changes: 19 additions & 9 deletions markten/actions/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class clone(MarkTenAction):
"""

def __init__(self, repo_url: str, /, branch: str | None = None) -> None:
self.repo = repo_url
self.branch = branch
self.repo = repo_url.strip()
self.branch = branch.strip() if branch else None

def get_name(self) -> str:
return "git clone"
Expand All @@ -41,13 +41,7 @@ async def run(self, task) -> Path:
task.fail("mktemp failed")
raise RuntimeError("mktemp failed")

# Perform the git clone
if self.branch:
branch_args = ["-b", self.branch, "--single-branch"]
else:
branch_args = []

program = ("git", "clone", *branch_args, self.repo, str(clone_path))
program = ("git", "clone", self.repo, str(clone_path))
task.running(' '.join(program))

clone = await run_process(
Expand All @@ -58,6 +52,22 @@ async def run(self, task) -> Path:
task.fail(f"git clone exited with error code: {clone}")
raise Exception("Task failed")

if self.branch:
program = (
"git",
"checkout",
"-b",
self.branch,
f"origin/{self.branch}",
)
task.running(' '.join(program))
task.log(' '.join(program))
clone = await run_process(
program,
cwd=str(clone_path),
on_stderr=task.log,
)

task.succeed(f"Cloned {self.repo} to {clone_path}")
return Path(str(clone_path))

Expand Down

0 comments on commit a69c897

Please sign in to comment.