@@ -19,22 +19,28 @@ def clone_projects
19
19
logger . info "Start cloning #{ project . name } repository"
20
20
path = project . clone_root_path
21
21
22
+ raise "Unsupported project type '#{ project . type } ' - only git is supported" unless project . type == 'git'
23
+
22
24
if File . exist? ( path )
23
25
logger . warn "Skipping clone, project path #{ path } already exists"
24
26
else
25
- raise "Unsupported project type '#{ project . type } ' - only git is supported" unless project . type == 'git'
26
-
27
- # git:
28
- # Don't download whole history
29
- # Note we don't use --single-branch, because the repo is downloaded
30
- # once but may be used with several tags.
31
- clone_cmd = "git clone --no-single-branch --depth 1 #{ project . connection } #{ path } "
32
-
27
+ # Don't download whole history. This just fetches HEAD, the correct ref is fetched below.
28
+ clone_cmd = "git clone --single-branch --depth 1 #{ project . connection } #{ path } "
33
29
Cmd . execute_successfully ( clone_cmd )
34
30
end
35
31
36
32
Dir . chdir ( path ) do
37
- execute_reset_cmd ( project . type , project . tag )
33
+ # this works with tags, branch names and (full-length) hashes
34
+ # first move to a different (temporary) branch, in case we are already on the branch that we want to update
35
+ Cmd . execute_successfully ( 'git checkout -b fetched/temp' )
36
+ # fetches any new commits. Could also be a tag.
37
+ Cmd . execute_successfully ( "git fetch --depth 1 origin #{ project . tag } " )
38
+ # update the branch to work on based on the new fetch. Creates a new branch if it doesn't exist already
39
+ Cmd . execute_successfully ( "git branch --force fetched/#{ project . tag } FETCH_HEAD" )
40
+ # checkout the updated branch
41
+ Cmd . execute_successfully ( "git checkout fetched/#{ project . tag } " )
42
+ # remove the temporary branch
43
+ Cmd . execute_successfully ( 'git branch -D fetched/temp' )
38
44
end
39
45
logger . info "Cloning #{ project . name } completed"
40
46
end
@@ -93,13 +99,5 @@ def run_as_script(path, command)
93
99
end
94
100
stdout
95
101
end
96
-
97
- def execute_reset_cmd ( type , tag )
98
- raise "Unsupported project type '#{ type } ' - only git is supported" unless type == 'git'
99
-
100
- reset_cmd = "git checkout #{ tag } ; git reset --hard #{ tag } "
101
-
102
- Cmd . execute_successfully ( reset_cmd )
103
- end
104
102
end
105
103
end
0 commit comments