Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/pyinfra/operations/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,20 @@ def repo(
git_dir = unix_path_join(dest, ".git")
is_repo = host.get_fact(Directory, path=git_dir)

# The depth argument can be passed to clone, fetch and pull operations.
# We prepare the argument list to reuse it later on
depth_options: list[str | QuoteString] = []
if depth is not None:
depth_options.extend(["--depth", str(depth)])

# Cloning new repo?
if not is_repo:
options: list[str | QuoteString] = []
if depth is not None:
options.extend(["--depth", str(depth)])
clone_options: list[str | QuoteString] = []
clone_options.extend(depth_options)
if branch:
options.extend(["--branch", QuoteString(branch)])
clone_options.extend(["--branch", QuoteString(branch)])

git_commands.append(StringCommand("clone", QuoteString(src), *options, "."))
git_commands.append(StringCommand("clone", QuoteString(src), *clone_options, "."))

# Ensuring existing repo
else:
Expand All @@ -177,10 +182,11 @@ def repo(
current_branch = host.get_fact(GitBranch, repo=dest)
if branch is not None and current_branch != branch:
# fetch to ensure we have the branch/tag locally
fetch_options: list[str | QuoteString] = []
fetch_options.extend(depth_options)
if fetch_tags:
git_commands.append(StringCommand("fetch", "--tags"))
else:
git_commands.append(StringCommand("fetch"))
fetch_options.append("--tags")
git_commands.append(StringCommand("fetch", *fetch_options))

git_commands.append(StringCommand("checkout", QuoteString(branch)))
if branch and branch in (host.get_fact(GitTag, repo=dest) or []):
Expand Down Expand Up @@ -209,10 +215,12 @@ def repo(
host.noop(
f"git repository {dest} is already up to date",
)
elif rebase:
git_commands.append("pull --rebase")
else:
git_commands.append("pull")
pull_options: list[str | QuoteString] = []
pull_options.extend(depth_options)
if rebase:
pull_options.append("--rebase")
git_commands.append(StringCommand("pull", *pull_options))

if update_submodules:
if recursive_submodules:
Expand Down
44 changes: 44 additions & 0 deletions tests/operations/git.repo/branch_pull_depth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"args": ["myrepo", "/home/myrepo"],
"kwargs": {
"branch": "mybranch",
"depth": 1,
"fetch_tags": true
},
"facts": {
"git.GitConfig": {
"repo=/home/myrepo, system=False": {
"remote.origin.url": ["myrepo"]
}
},
"files.Directory": {
"path=/home/myrepo": {},
"path=/home/myrepo/.git": {
"mode": 0
}
},
"git.GitBranch": {
"repo=/home/myrepo": "master"
},
"git.GitTag": {
"repo=/home/myrepo": [
]
},
"git.GitLocalCommit": {
"ref=mybranch, repo=/home/myrepo": null
},
"git.GitRemoteBranchCommit": {
"branch=mybranch, remote=origin, repo=/home/myrepo": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
}
},
"commands": [
"cd /home/myrepo && git fetch --depth 1 --tags",
"cd /home/myrepo && git checkout mybranch",
"cd /home/myrepo && git pull --depth 1"
],
"second_output_commands": [
"cd /home/myrepo && git pull --depth 1"
],
"idempotent": false,
"disable_idempotent_warning_reason": "git branch pull always executed"
}
19 changes: 19 additions & 0 deletions tests/operations/git.repo/clone_depth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"args": ["myrepo", "/home/myrepo"],
"kwargs": {
"branch": "mybranch",
"user": "myuser",
"depth": 1,
"pull": false
},
"facts": {
"files.Directory": {
"path=/home/myrepo": {},
"path=/home/myrepo/.git": null
}
},
"commands": [
"cd /home/myrepo && git clone myrepo --depth 1 --branch mybranch .",
"chown -R myuser /home/myrepo"
]
}
Loading