Skip to content

Commit

Permalink
Merge pull request #22 from thin-edge/clone-branch
Browse files Browse the repository at this point in the history
checkout to branch after cloning
  • Loading branch information
reubenmiller authored Sep 17, 2024
2 parents 726e2e1 + d119c7d commit d30f51f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions gittyup.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ def read_repo_url_from_toml(config_file: str) -> GitOpsRepository:
# Use explicit branch or use the device type as the branch
branch = config_data.get("repository", {}).get("branch", "") or get_device_type()

if url:
if url and branch:
return GitOpsRepository(url, branch)
else:
raise ValueError("Repository URL not found in the TOML file.")
raise ValueError("Repository URL or branch not found in the TOML file.")


def clone_or_pull_repo(repo: GitOpsRepository, clone_dir="repo") -> Optional[str]:
def clone_or_pull_repo(repo_config: GitOpsRepository, clone_dir="repo") -> Optional[str]:
"""
Pulls the new information from the remote repository to the local repository, if there is any.
If local repository does not exist, it is cloned.
Expand Down Expand Up @@ -258,13 +258,20 @@ def clone_or_pull_repo(repo: GitOpsRepository, clone_dir="repo") -> Optional[str
return None
else:
# If the repository doesn't exist, clone it
logger.debug(f"Cloning the repository '{repo.url}' into '{clone_dir}'...")
logger.debug(f"Cloning the repository '{repo_config.url}' into '{clone_dir}'...")
logger.debug("Using branch '%s'", repo_config.branch)

try:
repo = git.Repo.clone_from(repo.url, clone_dir)
repo = git.Repo.clone_from(repo_config.url, clone_dir)
except GitCommandError as e:
logger.error(f"Error during git clone: {e}")

g = repo.git
g.checkout(repo_config.branch)

repo = git.Repo(clone_dir)
assert repo.active_branch.name == repo_config.branch, f"${repo.active_branch} != ${repo_config.branch}"

logger.info(f"Repository cloned into '{clone_dir}'.")

return repo.head.commit.hexsha
Expand Down

0 comments on commit d30f51f

Please sign in to comment.