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
b3b12b5e
Commit
b3b12b5e
authored
Jul 26, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #694 from sfromm/git
Update git module to handle branches better
parents
19fc8eea
1727bd3b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
43 deletions
+53
-43
library/git
+53
-43
No files found.
library/git
View file @
b3b12b5e
...
@@ -71,8 +71,8 @@ for x in items:
...
@@ -71,8 +71,8 @@ for x in items:
dest
=
params
[
'dest'
]
dest
=
params
[
'dest'
]
repo
=
params
[
'repo'
]
repo
=
params
[
'repo'
]
branch
=
params
.
get
(
'branch'
,
'master'
)
version
=
params
.
get
(
'version'
,
'HEAD'
)
version
=
params
.
get
(
'version'
,
'HEAD'
)
remote
=
params
.
get
(
'remote'
,
'origin'
)
# ===========================================
# ===========================================
...
@@ -84,7 +84,7 @@ def get_version(dest):
...
@@ -84,7 +84,7 @@ def get_version(dest):
sha
=
sha
[
0
]
.
split
()[
1
]
sha
=
sha
[
0
]
.
split
()[
1
]
return
sha
return
sha
def
clone
(
repo
,
dest
,
branch
):
def
clone
(
repo
,
dest
):
''' makes a new git repo if it does not already exist '''
''' makes a new git repo if it does not already exist '''
try
:
try
:
os
.
makedirs
(
os
.
path
.
dirname
(
dest
))
os
.
makedirs
(
os
.
path
.
dirname
(
dest
))
...
@@ -94,14 +94,7 @@ def clone(repo, dest, branch):
...
@@ -94,14 +94,7 @@ def clone(repo, dest, branch):
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
(
out
,
err
)
=
cmd
.
communicate
()
(
out
,
err
)
=
cmd
.
communicate
()
rc
=
cmd
.
returncode
rc
=
cmd
.
returncode
return
(
rc
,
out
,
err
)
if
branch
is
None
or
rc
!=
0
:
return
(
out
,
err
)
os
.
chdir
(
dest
)
cmd
=
"git checkout -b
%
s origin/
%
s"
%
(
branch
,
branch
)
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
return
cmd
.
communicate
()
def
reset
(
dest
):
def
reset
(
dest
):
'''
'''
...
@@ -121,52 +114,67 @@ def switchLocalBranch( branch ):
...
@@ -121,52 +114,67 @@ def switchLocalBranch( branch ):
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
return
cmd
.
communicate
()
return
cmd
.
communicate
()
def
pull
(
repo
,
dest
,
branch
):
def
get_branches
(
dest
):
''' updates repo from remote sources '''
os
.
chdir
(
dest
)
os
.
chdir
(
dest
)
branches
=
[]
cmd
=
"git branch -a"
cmd
=
"git branch -a"
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
(
gbranch_out
,
gbranch_err
)
=
cmd
.
communicate
()
out
,
err
=
cmd
.
communicate
()
if
cmd
.
returncode
!=
0
:
try
:
fail_json
(
msg
=
"Could not determine branch data - received
%
s"
%
out
)
m
=
re
.
search
(
'^
\
* (
\
S+|
\
(no branch
\
))$'
,
gbranch_out
,
flags
=
re
.
M
)
for
line
in
out
.
split
(
'
\n
'
):
cur_branch
=
m
.
group
(
1
)
branches
.
append
(
line
.
strip
())
m
=
re
.
search
(
'
\
s+remotes/origin/HEAD -> origin/(
\
S+)'
,
gbranch_out
,
flags
=
re
.
M
)
return
branches
default_branch
=
m
.
group
(
1
)
except
:
def
is_remote_branch
(
dest
,
remote
,
branch
):
fail_json
(
msg
=
"could not determine branch data - received:
%
s"
%
gbranch_out
)
branches
=
get_branches
(
dest
)
rbranch
=
'remotes/
%
s/
%
s'
%
(
remote
,
branch
)
if
branch
is
None
:
if
rbranch
in
branches
:
if
cur_branch
!=
default_branch
:
return
True
(
out
,
err
)
=
switchLocalBranch
(
default_branch
)
cmd
=
"git pull -u origin"
elif
branch
==
cur_branch
:
cmd
=
"git pull -u origin"
else
:
else
:
m
=
re
.
search
(
'^
\
s+
%
s$'
%
branch
,
gbranch_out
,
flags
=
re
.
M
)
#see if we've already checked it out
return
False
if
m
is
None
:
cmd
=
"git checkout --track -b
%
s origin/
%
s"
%
(
branch
,
branch
)
def
is_local_branch
(
dest
,
branch
):
branches
=
get_branches
(
dest
)
lbranch
=
'
%
s'
%
branch
if
lbranch
in
branches
:
return
True
else
:
else
:
cmd
=
"git pull -u origin"
return
False
def
pull
(
repo
,
dest
,
version
):
''' updates repo from remote sources '''
os
.
chdir
(
dest
)
branches
=
get_branches
(
dest
)
cur_branch
=
''
for
b
in
branches
:
if
b
.
startswith
(
'* '
):
cur_branch
=
b
if
is_local_branch
(
dest
,
version
)
and
version
!=
cur_branch
:
(
out
,
err
)
=
switchLocalBranch
(
version
)
cmd
=
"git pull -u origin"
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
return
cmd
.
communicate
()
out
,
err
=
cmd
.
communicate
()
rc
=
cmd
.
returncode
return
(
rc
,
out
,
err
)
def
switch
ver
(
version
,
dest
):
def
switch
_version
(
dest
,
remote
,
version
):
''' once pulled, switch to a particular SHA or tag '''
''' once pulled, switch to a particular SHA or tag '''
os
.
chdir
(
dest
)
os
.
chdir
(
dest
)
cmd
=
''
if
version
!=
'HEAD'
:
if
version
!=
'HEAD'
:
cmd
=
"git checkout
%
s --force"
%
version
if
not
is_local_branch
(
dest
,
version
)
and
is_remote_branch
(
dest
,
remote
,
version
):
cmd
=
"git checkout --track -b
%
s
%
s/
%
s"
%
(
version
,
remote
,
version
)
else
:
cmd
=
"git checkout --force
%
s"
%
version
else
:
else
:
# is there a better way to do this?
# is there a better way to do this?
cmd
=
"git rebase origin"
cmd
=
"git rebase origin"
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
cmd
=
subprocess
.
Popen
(
cmd
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
(
out
,
err
)
=
cmd
.
communicate
()
(
out
,
err
)
=
cmd
.
communicate
()
return
(
out
,
err
)
rc
=
cmd
.
returncode
return
(
rc
,
out
,
err
)
gitconfig
=
os
.
path
.
join
(
dest
,
'.git'
,
'config'
)
gitconfig
=
os
.
path
.
join
(
dest
,
'.git'
,
'config'
)
...
@@ -178,14 +186,16 @@ out, err, status = (None, None, None)
...
@@ -178,14 +186,16 @@ out, err, status = (None, None, None)
before
=
None
before
=
None
if
not
os
.
path
.
exists
(
gitconfig
):
if
not
os
.
path
.
exists
(
gitconfig
):
(
out
,
err
)
=
clone
(
repo
,
dest
,
branch
)
(
rc
,
out
,
err
)
=
clone
(
repo
,
dest
)
if
rc
!=
0
:
fail_json
(
out
=
out
,
err
=
err
,
rc
=
rc
)
else
:
else
:
# else do a pull
# else do a pull
before
=
get_version
(
dest
)
before
=
get_version
(
dest
)
(
rc
,
out
,
err
)
=
reset
(
dest
)
(
rc
,
out
,
err
)
=
reset
(
dest
)
if
rc
!=
0
:
if
rc
!=
0
:
fail_json
(
out
=
out
,
err
=
err
)
fail_json
(
out
=
out
,
err
=
err
,
rc
=
rc
)
(
out
,
err
)
=
pull
(
repo
,
dest
,
branch
)
(
rc
,
out
,
err
)
=
pull
(
repo
,
dest
,
version
)
# handle errors from clone or pull
# handle errors from clone or pull
...
@@ -195,7 +205,7 @@ if out.find('error') != -1 or err.find('ERROR') != -1:
...
@@ -195,7 +205,7 @@ if out.find('error') != -1 or err.find('ERROR') != -1:
# switch to version specified regardless of whether
# switch to version specified regardless of whether
# we cloned or pulled
# we cloned or pulled
(
out
,
err
)
=
switchver
(
version
,
dest
)
(
rc
,
out
,
err
)
=
switch_version
(
dest
,
remote
,
version
)
if
err
.
find
(
'error'
)
!=
-
1
:
if
err
.
find
(
'error'
)
!=
-
1
:
fail_json
(
out
=
out
,
err
=
err
)
fail_json
(
out
=
out
,
err
=
err
)
...
...
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