Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
12eaefb7
Commit
12eaefb7
authored
Oct 17, 2013
by
jctanner
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4539 from sayap/git-sha1
git: Always return the before/after revisions, even in check mode.
parents
8666f8ea
8519d586
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
51 deletions
+43
-51
library/source_control/git
+43
-51
No files found.
library/source_control/git
View file @
12eaefb7
...
@@ -41,7 +41,8 @@ options:
...
@@ -41,7 +41,8 @@ options:
default: "HEAD"
default: "HEAD"
description:
description:
- What version of the repository to check out. This can be the
- What version of the repository to check out. This can be the
git I(SHA), the literal string C(HEAD), a branch name, or a tag name.
full 40-character I(SHA-1) hash, the literal string C(HEAD), a
branch name, or a tag name.
remote:
remote:
required: false
required: false
default: "origin"
default: "origin"
...
@@ -107,7 +108,7 @@ import tempfile
...
@@ -107,7 +108,7 @@ import tempfile
def
get_version
(
git_path
,
dest
):
def
get_version
(
git_path
,
dest
):
''' samples the version of the git repo '''
''' samples the version of the git repo '''
os
.
chdir
(
dest
)
os
.
chdir
(
dest
)
cmd
=
"
%
s show
--abbrev-commit
"
%
(
git_path
,)
cmd
=
"
%
s show"
%
(
git_path
,)
sha
=
os
.
popen
(
cmd
)
.
read
()
.
split
(
"
\n
"
)
sha
=
os
.
popen
(
cmd
)
.
read
()
.
split
(
"
\n
"
)
sha
=
sha
[
0
]
.
split
()[
1
]
sha
=
sha
[
0
]
.
split
()[
1
]
return
sha
return
sha
...
@@ -121,8 +122,9 @@ def clone(git_path, module, repo, dest, remote, depth, version):
...
@@ -121,8 +122,9 @@ def clone(git_path, module, repo, dest, remote, depth, version):
pass
pass
os
.
chdir
(
dest_dirname
)
os
.
chdir
(
dest_dirname
)
cmd
=
[
git_path
,
'clone'
,
'-o'
,
remote
,
'--recursive'
]
cmd
=
[
git_path
,
'clone'
,
'-o'
,
remote
,
'--recursive'
]
if
version
and
version
!=
'HEAD'
:
if
is_remote_branch
(
git_path
,
module
,
dest
,
repo
,
version
)
\
cmd
.
extend
([
'--branch'
,
str
(
version
)
])
or
is_remote_tag
(
git_path
,
module
,
dest
,
repo
,
version
):
cmd
.
extend
([
'--branch'
,
version
])
if
depth
:
if
depth
:
cmd
.
extend
([
'--depth'
,
str
(
depth
)
])
cmd
.
extend
([
'--depth'
,
str
(
depth
)
])
cmd
.
extend
([
repo
,
dest
])
cmd
.
extend
([
repo
,
dest
])
...
@@ -135,24 +137,30 @@ def has_local_mods(git_path, dest):
...
@@ -135,24 +137,30 @@ def has_local_mods(git_path, dest):
lines
=
filter
(
lambda
c
:
not
re
.
search
(
'^
\\
?
\\
?.*$'
,
c
),
lines
)
lines
=
filter
(
lambda
c
:
not
re
.
search
(
'^
\\
?
\\
?.*$'
,
c
),
lines
)
return
len
(
lines
)
>
0
return
len
(
lines
)
>
0
def
reset
(
git_path
,
module
,
dest
,
force
):
def
reset
(
git_path
,
module
,
dest
):
'''
'''
Resets the index and working tree to HEAD.
Resets the index and working tree to HEAD.
Discards any changes to tracked files in working
Discards any changes to tracked files in working
tree since that commit.
tree since that commit.
'''
'''
os
.
chdir
(
dest
)
os
.
chdir
(
dest
)
if
not
force
and
has_local_mods
(
git_path
,
dest
):
module
.
fail_json
(
msg
=
"Local modifications exist in repository (force=no)."
)
cmd
=
"
%
s reset --hard HEAD"
%
(
git_path
,)
cmd
=
"
%
s reset --hard HEAD"
%
(
git_path
,)
return
module
.
run_command
(
cmd
,
check_rc
=
True
)
return
module
.
run_command
(
cmd
,
check_rc
=
True
)
def
get_remote_head
(
git_path
,
module
,
dest
,
version
,
remote
):
def
get_remote_head
(
git_path
,
module
,
dest
,
version
,
remote
):
cmd
=
''
cloning
=
False
if
remote
==
module
.
params
[
'repo'
]:
cloning
=
True
else
:
os
.
chdir
(
dest
)
os
.
chdir
(
dest
)
if
version
==
'HEAD'
:
if
version
==
'HEAD'
:
version
=
get_head_branch
(
git_path
,
module
,
dest
,
remote
)
if
cloning
:
if
is_remote_branch
(
git_path
,
module
,
dest
,
remote
,
version
):
# cloning the repo, just get the remote's HEAD version
cmd
=
'
%
s ls-remote
%
s -h HEAD'
%
(
git_path
,
remote
)
else
:
head_branch
=
get_head_branch
(
git_path
,
module
,
dest
,
remote
)
cmd
=
'
%
s ls-remote
%
s -h refs/heads/
%
s'
%
(
git_path
,
remote
,
head_branch
)
elif
is_remote_branch
(
git_path
,
module
,
dest
,
remote
,
version
):
cmd
=
'
%
s ls-remote
%
s -h refs/heads/
%
s'
%
(
git_path
,
remote
,
version
)
cmd
=
'
%
s ls-remote
%
s -h refs/heads/
%
s'
%
(
git_path
,
remote
,
version
)
elif
is_remote_tag
(
git_path
,
module
,
dest
,
remote
,
version
):
elif
is_remote_tag
(
git_path
,
module
,
dest
,
remote
,
version
):
cmd
=
'
%
s ls-remote
%
s -t refs/tags/
%
s'
%
(
git_path
,
remote
,
version
)
cmd
=
'
%
s ls-remote
%
s -t refs/tags/
%
s'
%
(
git_path
,
remote
,
version
)
...
@@ -167,9 +175,8 @@ def get_remote_head(git_path, module, dest, version, remote):
...
@@ -167,9 +175,8 @@ def get_remote_head(git_path, module, dest, version, remote):
return
rev
return
rev
def
is_remote_tag
(
git_path
,
module
,
dest
,
remote
,
version
):
def
is_remote_tag
(
git_path
,
module
,
dest
,
remote
,
version
):
os
.
chdir
(
dest
)
cmd
=
'
%
s ls-remote
%
s -t refs/tags/
%
s'
%
(
git_path
,
remote
,
version
)
cmd
=
'
%
s ls-remote
%
s -t refs/tags/
%
s'
%
(
git_path
,
remote
,
version
)
(
rc
,
out
,
err
)
=
module
.
run_command
(
cmd
)
(
rc
,
out
,
err
)
=
module
.
run_command
(
cmd
,
check_rc
=
True
)
if
version
in
out
:
if
version
in
out
:
return
True
return
True
else
:
else
:
...
@@ -186,10 +193,10 @@ def get_branches(git_path, module, dest):
...
@@ -186,10 +193,10 @@ def get_branches(git_path, module, dest):
branches
.
append
(
line
.
strip
())
branches
.
append
(
line
.
strip
())
return
branches
return
branches
def
is_remote_branch
(
git_path
,
module
,
dest
,
remote
,
branch
):
def
is_remote_branch
(
git_path
,
module
,
dest
,
remote
,
version
):
branches
=
get_branches
(
git_path
,
module
,
dest
)
cmd
=
'
%
s ls-remote
%
s -h refs/heads/
%
s'
%
(
git_path
,
remote
,
version
)
rbranch
=
'remotes/
%
s/
%
s'
%
(
remote
,
branch
)
(
rc
,
out
,
err
)
=
module
.
run_command
(
cmd
,
check_rc
=
True
)
if
rbranch
in
branches
:
if
version
in
out
:
return
True
return
True
else
:
else
:
return
False
return
False
...
@@ -335,8 +342,9 @@ def main():
...
@@ -335,8 +342,9 @@ def main():
local_mods
=
False
local_mods
=
False
if
not
os
.
path
.
exists
(
gitconfig
):
if
not
os
.
path
.
exists
(
gitconfig
):
if
module
.
check_mode
:
if
module
.
check_mode
:
module
.
exit_json
(
changed
=
True
)
remote_head
=
get_remote_head
(
git_path
,
module
,
dest
,
version
,
repo
)
(
rc
,
out
,
err
)
=
clone
(
git_path
,
module
,
repo
,
dest
,
remote
,
depth
,
version
)
module
.
exit_json
(
changed
=
True
,
before
=
before
,
after
=
remote_head
)
clone
(
git_path
,
module
,
repo
,
dest
,
remote
,
depth
,
version
)
elif
not
update
:
elif
not
update
:
# Just return having found a repo already in the dest path
# Just return having found a repo already in the dest path
# this does no checking that the repo is the actual repo
# this does no checking that the repo is the actual repo
...
@@ -347,44 +355,28 @@ def main():
...
@@ -347,44 +355,28 @@ def main():
# else do a pull
# else do a pull
local_mods
=
has_local_mods
(
git_path
,
dest
)
local_mods
=
has_local_mods
(
git_path
,
dest
)
before
=
get_version
(
git_path
,
dest
)
before
=
get_version
(
git_path
,
dest
)
# if force, do a reset
if
local_mods
and
module
.
check_mode
:
module
.
exit_json
(
changed
=
True
,
msg
=
'Local modifications exist'
)
(
rc
,
out
,
err
)
=
reset
(
git_path
,
module
,
dest
,
force
)
if
rc
!=
0
:
module
.
fail_json
(
msg
=
err
)
# exit if already at desired sha version
# abbreviate version in case full sha is given
if
before
==
str
(
version
)[:
7
]:
module
.
exit_json
(
changed
=
False
)
# check or get changes from remote
remote_head
=
get_remote_head
(
git_path
,
module
,
dest
,
version
,
remote
)
remote_head
=
get_remote_head
(
git_path
,
module
,
dest
,
version
,
remote
)
if
module
.
check_mode
:
if
local_mods
:
changed
=
False
# failure should happen regardless of check mode
if
remote_head
==
version
:
if
not
force
:
# get_remote_head returned version as-is
module
.
fail_json
(
msg
=
"Local modifications exist in repository (force=no)."
)
# were given a sha1 object, see if it is present
# if force and in non-check mode, do a reset
(
rc
,
out
,
err
)
=
module
.
run_command
(
"
%
s show
%
s"
%
(
git_path
,
version
))
if
not
module
.
check_mode
:
if
version
in
out
:
reset
(
git_path
,
module
,
dest
)
changed
=
False
# exit if already at desired sha version
else
:
if
before
==
remote_head
:
changed
=
True
if
local_mods
:
else
:
module
.
exit_json
(
changed
=
True
,
before
=
before
,
after
=
remote_head
,
remote_head
=
remote_head
[
0
:
7
]
msg
=
"Local modifications exist"
)
if
before
!=
remote_head
:
changed
=
True
else
:
else
:
changed
=
False
module
.
exit_json
(
changed
=
False
,
before
=
before
,
after
=
remote_head
)
module
.
exit_json
(
changed
=
changed
,
before
=
before
,
after
=
remote_head
)
if
module
.
check_mode
:
(
rc
,
out
,
err
)
=
fetch
(
git_path
,
module
,
repo
,
dest
,
version
,
remote
)
module
.
exit_json
(
changed
=
True
,
before
=
before
,
after
=
remote_head
)
if
rc
!=
0
:
fetch
(
git_path
,
module
,
repo
,
dest
,
version
,
remote
)
module
.
fail_json
(
msg
=
err
)
# switch to version specified regardless of whether
# switch to version specified regardless of whether
# we cloned or pulled
# we cloned or pulled
(
rc
,
out
,
err
)
=
switch_version
(
git_path
,
module
,
dest
,
remote
,
version
)
switch_version
(
git_path
,
module
,
dest
,
remote
,
version
)
if
rc
!=
0
:
module
.
fail_json
(
msg
=
err
)
# determine if we changed anything
# determine if we changed anything
after
=
get_version
(
git_path
,
dest
)
after
=
get_version
(
git_path
,
dest
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment