Skip to content

Commit 1a532cb

Browse files
committed
Improve idempotency in git.repo operation.
1 parent cae4dbc commit 1a532cb

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pyinfra/operations/git.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def repo(
117117

118118
# Store git commands for directory prefix
119119
git_commands = []
120-
is_repo = host.get_fact(Directory, path=unix_path_join(dest, '.git'))
120+
git_dir = unix_path_join(dest, '.git')
121+
is_repo = host.get_fact(Directory, path=git_dir)
121122

122123
# Cloning new repo?
123124
if not is_repo:
@@ -126,12 +127,20 @@ def repo(
126127
else:
127128
git_commands.append('clone {0} .'.format(src))
128129

130+
host.create_fact(GitBranch, kwargs={'repo': dest}, data=branch)
131+
host.create_fact(
132+
Directory,
133+
kwargs={'path': git_dir},
134+
data={'user': user, 'group': group},
135+
)
136+
129137
# Ensuring existing repo
130138
else:
131139
current_branch = host.get_fact(GitBranch, repo=dest)
132140
if current_branch != branch:
133141
git_commands.append('fetch') # fetch to ensure we have the branch locally
134142
git_commands.append('checkout {0}'.format(branch))
143+
host.create_fact(GitBranch, kwargs={'repo': dest}, data=branch)
135144

136145
if pull:
137146
if rebase:
@@ -155,8 +164,8 @@ def repo(
155164
for cmd in git_commands:
156165
yield cmd
157166

158-
# Apply any user or group
159-
if user or group:
167+
# Apply any user or group if we did anything
168+
if git_commands and (user or group):
160169
yield chown(dest, user, group, recursive=True)
161170

162171

0 commit comments

Comments
 (0)