esr-rim 1.3.9 → 1.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,69 +1,69 @@
1
- require 'rim/command_helper'
2
- require 'rim/upload_module_helper'
3
-
4
- module RIM
5
-
6
- class UploadHelper < CommandHelper
7
-
8
- def initialize(workspace_root, review, logger, module_infos = nil)
9
- @module_helpers = []
10
- @review = review
11
- super(workspace_root, logger, module_infos)
12
- end
13
-
14
- # upload all module changes into corresponding remote repositories
15
- def upload
16
- # get the name of the current workspace branch
17
- RIM::git_session(@ws_root) do |s|
18
- branch = s.current_branch
19
- if branch.nil?
20
- raise RimException.new("Not on a git branch.")
21
- elsif !branch.start_with?("rim/")
22
- begin
23
- sha1 = s.rev_sha1(branch)
24
- @logger.info("Uploading modules...")
25
- upload_modules(get_upload_revisions(s, sha1))
26
- ensure
27
- s.execute("git checkout -B #{branch}")
28
- end
29
- else
30
- raise RimException.new("The current git branch '#{branch}' is a rim integration branch. Please switch to a non rim branch to proceed.")
31
- end
32
- end
33
- end
34
-
35
- # called to add a module info
36
- def add_module_info(module_info)
37
- @module_helpers.push(UploadModuleHelper.new(@ws_root, module_info, @review, @logger))
38
- end
39
-
40
- private
41
- # upload all modules
42
- def upload_modules(info)
43
- each_module_parallel("uploading", @module_helpers) do |m|
44
- m.upload(info.parent, info.sha1s)
45
- end
46
- end
47
-
48
- # get revisions to upload i.e. the revisions up to the last remote revision
49
- # the function returns the revisions in order of appearal i.e. the oldest first
50
- def get_upload_revisions(session, rev)
51
- # remote revs are where we stop traversal
52
- non_remote_revs = {}
53
- session.all_reachable_non_remote_revs(rev).each do |r|
54
- non_remote_revs[r] = true
55
- end
56
- revisions = []
57
- # make sure we deal only with sha1s
58
- rev = session.rev_sha1(rev)
59
- while rev && non_remote_revs[rev]
60
- revisions.push(rev)
61
- parents = session.parent_revs(rev)
62
- rev = parents.size > 0 ? parents.first : nil
63
- end
64
- Struct.new(:parent, :sha1s).new(rev, revisions.reverse!)
65
- end
66
-
67
- end
68
-
69
- end
1
+ require 'rim/command_helper'
2
+ require 'rim/upload_module_helper'
3
+
4
+ module RIM
5
+
6
+ class UploadHelper < CommandHelper
7
+
8
+ def initialize(workspace_root, review, logger, module_infos = nil)
9
+ @module_helpers = []
10
+ @review = review
11
+ super(workspace_root, logger, module_infos)
12
+ end
13
+
14
+ # upload all module changes into corresponding remote repositories
15
+ def upload
16
+ # get the name of the current workspace branch
17
+ RIM::git_session(@ws_root) do |s|
18
+ branch = s.current_branch
19
+ if branch.nil?
20
+ raise RimException.new("Not on a git branch.")
21
+ elsif !branch.start_with?("rim/")
22
+ begin
23
+ sha1 = s.rev_sha1(branch)
24
+ @logger.info("Uploading modules...")
25
+ upload_modules(get_upload_revisions(s, sha1))
26
+ ensure
27
+ s.execute("git checkout -B #{branch}")
28
+ end
29
+ else
30
+ raise RimException.new("The current git branch '#{branch}' is a rim integration branch. Please switch to a non rim branch to proceed.")
31
+ end
32
+ end
33
+ end
34
+
35
+ # called to add a module info
36
+ def add_module_info(module_info)
37
+ @module_helpers.push(UploadModuleHelper.new(@ws_root, module_info, @review, @logger))
38
+ end
39
+
40
+ private
41
+ # upload all modules
42
+ def upload_modules(info)
43
+ each_module_parallel("uploading", @module_helpers) do |m|
44
+ m.upload(info.parent, info.sha1s)
45
+ end
46
+ end
47
+
48
+ # get revisions to upload i.e. the revisions up to the last remote revision
49
+ # the function returns the revisions in order of appearal i.e. the oldest first
50
+ def get_upload_revisions(session, rev)
51
+ # remote revs are where we stop traversal
52
+ non_remote_revs = {}
53
+ session.all_reachable_non_remote_revs(rev).each do |r|
54
+ non_remote_revs[r] = true
55
+ end
56
+ revisions = []
57
+ # make sure we deal only with sha1s
58
+ rev = session.rev_sha1(rev)
59
+ while rev && non_remote_revs[rev]
60
+ revisions.push(rev)
61
+ parents = session.parent_revs(rev)
62
+ rev = parents.size > 0 ? parents.first : nil
63
+ end
64
+ Struct.new(:parent, :sha1s).new(rev, revisions.reverse!)
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -1,162 +1,163 @@
1
- require 'rim/module_helper'
2
- require 'rim/rim_info'
3
- require 'rim/file_helper'
4
- require 'rim/dirty_check'
5
- require 'rim/status_builder'
6
- require 'tempfile'
7
-
8
- module RIM
9
-
10
- class UploadModuleHelper < ModuleHelper
11
-
12
- def initialize(workspace_root, module_info, review, logger)
13
- super(workspace_root, module_info, logger)
14
- @review = review
15
- end
16
-
17
- # do the module uploads for revisions given by sha
18
- def upload(parent, sha1s)
19
- upload_module_changes(parent, sha1s)
20
- end
21
-
22
- private
23
-
24
- # upload the content of the module
25
- def upload_module_changes(parent_sha1, sha1s)
26
- remote_path = fetch_module
27
- # search for the first revision that is not
28
- tmp_git_path = clone_or_fetch_repository(remote_path, module_tmp_git_path(@remote_path))
29
- RIM::git_session(tmp_git_path) do |dest|
30
- local_branch = nil
31
- remote_branch = nil
32
- infos = nil
33
- if @module_info.subdir
34
- dest_path = File.join([tmp_git_path] + @module_info.subdir.split("/"))
35
- else
36
- dest_path = tmp_git_path
37
- end
38
- RIM::git_session(@ws_root) do |src|
39
- infos = get_branches_and_revision_infos(src, dest, parent_sha1, sha1s)
40
- if infos.branches.size == 1
41
- remote_branch = infos.branches[0]
42
- if dest.has_remote_branch?(remote_branch)
43
- infos.rev_infos.each do |rev_info|
44
- local_branch = create_update_branch(dest, infos.parent_sha1, rev_info.src_sha1) if !local_branch
45
- copy_revision_files(
46
- src,
47
- rev_info.src_sha1,
48
- dest_path,
49
- rev_info.rim_info.ignores
50
- )
51
- commit_changes(dest, local_branch, rev_info.src_sha1, rev_info.message)
52
- end
53
- else
54
- raise RimException.new("The target revision '#{@module_info.target_revision}' of module #{@module_info.local_path} is not a branch. No push can be performed.")
55
- end
56
- elsif infos.branches.size > 1
57
- raise RimException.new("There are commits for module #{@module_info.local_path} on multiple target revisions (#{infos.branches.join(", ")}).")
58
- end
59
- end
60
- # Finally we're done. Push the changes
61
- if local_branch && dest.rev_sha1(local_branch) != infos.parent_sha1
62
- push_branch = @review && @module_info.remote_branch_format && !@module_info.remote_branch_format.empty? \
63
- ? @module_info.remote_branch_format % remote_branch : remote_branch
64
- dest.execute("git push #{@remote_url} #{local_branch}:#{push_branch}")
65
- dest.execute("git checkout --detach #{local_branch}")
66
- dest.execute("git branch -D #{local_branch}")
67
- @logger.info("Commited changes for module #{@module_info.local_path} to remote branch #{push_branch}.")
68
- else
69
- @logger.info("No changes to module #{@module_info.local_path}.")
70
- end
71
- end
72
- end
73
-
74
- # search backwards for all revision infos
75
- def get_branches_and_revision_infos(src_session, dest_session, parent_sha1, sha1s)
76
- infos = []
77
- branches = []
78
- dest_parent_sha1 = nil
79
- (sha1s.size() - 1).step(0, -1) do |i|
80
- info = get_revision_info(src_session, dest_session, sha1s[i])
81
- if !info.dest_sha1 && info.rim_info.target_revision
82
- infos.unshift(info)
83
- branches.push(info.rim_info.target_revision) if !branches.include?(info.rim_info.target_revision)
84
- else
85
- dest_parent_sha1 = info.dest_sha1
86
- break
87
- end
88
- end
89
- dest_parent_sha1 = get_riminfo_for_revision(src_session, parent_sha1).revision_sha1 if !dest_parent_sha1
90
- dest_parent_sha1 = infos.first.rim_info.revision_sha1 if !dest_parent_sha1 && !infos.empty?
91
- return Struct.new(:branches, :parent_sha1, :rev_infos).new(branches, dest_parent_sha1, infos)
92
- end
93
-
94
- RevisionInfo = Struct.new(:dest_sha1, :src_sha1, :rim_info, :message)
95
-
96
- # collect infos for a revision
97
- def get_revision_info(src_session, dest_session, src_sha1)
98
- module_status = StatusBuilder.new.rev_module_status(src_session, src_sha1, @module_info.local_path)
99
- rim_info = get_riminfo_for_revision(src_session, src_sha1)
100
- dest_sha1 = dest_session.rev_sha1("rim-#{src_sha1}")
101
- msg = src_session.execute("git show -s --format=%B #{src_sha1}")
102
- RevisionInfo.new(module_status && module_status.dirty? ? dest_sha1 : rim_info.revision_sha1, src_sha1, rim_info, msg)
103
- end
104
-
105
- # commit changes to session
106
- def commit_changes(session, branch, sha1, msg)
107
- if session.status.lines.any?
108
- # add before commit because the path can be below a not yet added path
109
- session.execute("git add --all")
110
- msg_file = Tempfile.new('message')
111
- begin
112
- msg_file << msg
113
- msg_file.close
114
- session.execute("git commit -F #{msg_file.path}")
115
- ensure
116
- msg_file.close(true)
117
- end
118
- # create tag
119
- session.execute("git tag rim-#{sha1} refs/heads/#{branch}")
120
- end
121
- end
122
-
123
- # get target revision for this module for workspace revision
124
- def get_riminfo_for_revision(session, sha1)
125
- session.execute("git show #{sha1}:#{File.join(@module_info.local_path, RimInfo::InfoFileName)}") do |out, e|
126
- return RimInfo.from_s(!e ? out : "")
127
- end
128
- end
129
-
130
- # create update branch for given revision
131
- def create_update_branch(session, dest_sha1, src_sha1)
132
- branch = "rim/#{src_sha1}"
133
- session.execute("git checkout -B #{branch} #{dest_sha1}")
134
- branch
135
- end
136
-
137
- # copy files from given source revision into destination dir
138
- def copy_revision_files(src_session, src_sha1, dest_dir, ignores)
139
- Dir.mktmpdir do |tmp_dir|
140
- src_session.execute("git archive --format tar #{src_sha1} #{@module_info.local_path} | tar -x -C #{tmp_dir}")
141
- tmp_module_dir = File.join(tmp_dir, @module_info.local_path)
142
- files = FileHelper.find_matching_files(tmp_module_dir, false, "/**/*", File::FNM_DOTMATCH)
143
- files.delete(".")
144
- files.delete("..")
145
- files.delete(RimInfo::InfoFileName)
146
- files -= FileHelper.find_matching_files(tmp_module_dir, false, ignores)
147
- # have source files now. Now clear destination folder and copy
148
- prepare_empty_folder(dest_dir, ".git/**/*")
149
- files.each do |f|
150
- src_path = File.join(tmp_module_dir, f)
151
- if File.file?(src_path)
152
- path = File.join(dest_dir, f)
153
- FileUtils.mkdir_p(File.dirname(path))
154
- FileUtils.cp(src_path, path)
155
- end
156
- end
157
- end
158
- end
159
-
160
- end
161
-
162
- end
1
+ require 'rim/module_helper'
2
+ require 'rim/rim_info'
3
+ require 'rim/file_helper'
4
+ require 'rim/dirty_check'
5
+ require 'rim/status_builder'
6
+ require 'tempfile'
7
+
8
+ module RIM
9
+
10
+ class UploadModuleHelper < ModuleHelper
11
+
12
+ def initialize(workspace_root, module_info, review, logger)
13
+ super(workspace_root, module_info, logger)
14
+ @review = review
15
+ end
16
+
17
+ # do the module uploads for revisions given by sha
18
+ def upload(parent, sha1s)
19
+ upload_module_changes(parent, sha1s)
20
+ end
21
+
22
+ private
23
+
24
+ # upload the content of the module
25
+ def upload_module_changes(parent_sha1, sha1s)
26
+ remote_path = fetch_module
27
+ # search for the first revision that is not
28
+ tmp_git_path = clone_or_fetch_repository(remote_path, module_tmp_git_path(@remote_path))
29
+ RIM::git_session(tmp_git_path) do |dest|
30
+ local_branch = nil
31
+ remote_branch = nil
32
+ infos = nil
33
+ if @module_info.subdir
34
+ dest_path = File.join([tmp_git_path] + @module_info.subdir.split("/"))
35
+ else
36
+ dest_path = tmp_git_path
37
+ end
38
+ RIM::git_session(@ws_root) do |src|
39
+ infos = get_branches_and_revision_infos(src, dest, parent_sha1, sha1s)
40
+ if infos.branches.size == 1
41
+ remote_branch = infos.branches[0]
42
+ if dest.has_remote_branch?(remote_branch)
43
+ infos.rev_infos.each do |rev_info|
44
+ local_branch = create_update_branch(dest, infos.parent_sha1, rev_info.src_sha1) if !local_branch
45
+ copy_revision_files(
46
+ src,
47
+ rev_info.src_sha1,
48
+ dest_path,
49
+ rev_info.rim_info.ignores
50
+ )
51
+ commit_changes(dest, local_branch, rev_info.src_sha1, rev_info.message)
52
+ end
53
+ else
54
+ raise RimException.new("The target revision '#{@module_info.target_revision}' of module #{@module_info.local_path} is not a branch. No push can be performed.")
55
+ end
56
+ elsif infos.branches.size > 1
57
+ raise RimException.new("There are commits for module #{@module_info.local_path} on multiple target revisions (#{infos.branches.join(", ")}).")
58
+ end
59
+ end
60
+ # Finally we're done. Push the changes
61
+ if local_branch && dest.rev_sha1(local_branch) != infos.parent_sha1
62
+ push_branch = @review && @module_info.remote_branch_format && !@module_info.remote_branch_format.empty? \
63
+ ? @module_info.remote_branch_format % remote_branch : remote_branch
64
+ dest.execute("git push #{@remote_url} #{local_branch}:#{push_branch}")
65
+ dest.execute("git checkout --detach #{local_branch}")
66
+ dest.execute("git branch -D #{local_branch}")
67
+ @logger.info("Commited changes for module #{@module_info.local_path} to remote branch #{push_branch}.")
68
+ else
69
+ @logger.info("No changes to module #{@module_info.local_path}.")
70
+ end
71
+ end
72
+ end
73
+
74
+ # search backwards for all revision infos
75
+ def get_branches_and_revision_infos(src_session, dest_session, parent_sha1, sha1s)
76
+ infos = []
77
+ branches = []
78
+ dest_parent_sha1 = nil
79
+ (sha1s.size() - 1).step(0, -1) do |i|
80
+ info = get_revision_info(src_session, dest_session, sha1s[i])
81
+ if !info.dest_sha1 && info.rim_info.target_revision
82
+ infos.unshift(info)
83
+ branches.push(info.rim_info.target_revision) if !branches.include?(info.rim_info.target_revision)
84
+ else
85
+ dest_parent_sha1 = info.dest_sha1
86
+ break
87
+ end
88
+ end
89
+ dest_parent_sha1 = get_riminfo_for_revision(src_session, parent_sha1).revision_sha1 if !dest_parent_sha1
90
+ dest_parent_sha1 = infos.first.rim_info.revision_sha1 if !dest_parent_sha1 && !infos.empty?
91
+ return Struct.new(:branches, :parent_sha1, :rev_infos).new(branches, dest_parent_sha1, infos)
92
+ end
93
+
94
+ RevisionInfo = Struct.new(:dest_sha1, :src_sha1, :rim_info, :message)
95
+
96
+ # collect infos for a revision
97
+ def get_revision_info(src_session, dest_session, src_sha1)
98
+ module_status = StatusBuilder.new.rev_module_status(src_session, src_sha1, @module_info.local_path)
99
+ rim_info = get_riminfo_for_revision(src_session, src_sha1)
100
+ dest_sha1 = dest_session.rev_sha1("rim-#{src_sha1}")
101
+ msg = src_session.execute("git show -s --format=%B #{src_sha1}")
102
+ RevisionInfo.new(module_status && module_status.dirty? ? dest_sha1 : rim_info.revision_sha1, src_sha1, rim_info, msg)
103
+ end
104
+
105
+ # commit changes to session
106
+ def commit_changes(session, branch, sha1, msg)
107
+ if session.status.lines.any?
108
+ # add before commit because the path can be below a not yet added path
109
+ session.execute("git add --all")
110
+ msg_file = Tempfile.new('message')
111
+ begin
112
+ msg_file << msg
113
+ msg_file.close
114
+ session.execute("git commit -F #{msg_file.path}")
115
+ ensure
116
+ msg_file.close(true)
117
+ end
118
+ # create tag
119
+ session.execute("git tag rim-#{sha1} refs/heads/#{branch}")
120
+ end
121
+ end
122
+
123
+ # get target revision for this module for workspace revision
124
+ def get_riminfo_for_revision(session, sha1)
125
+ session.execute("git show #{sha1}:#{File.join(@module_info.local_path, RimInfo::InfoFileName)}") do |out, e|
126
+ return RimInfo.from_s(!e ? out : "")
127
+ end
128
+ end
129
+
130
+ # create update branch for given revision
131
+ def create_update_branch(session, dest_sha1, src_sha1)
132
+ branch = "rim/#{src_sha1}"
133
+ session.execute("git checkout -B #{branch} #{dest_sha1}")
134
+ branch
135
+ end
136
+
137
+ # copy files from given source revision into destination dir
138
+ def copy_revision_files(src_session, src_sha1, dest_dir, ignores)
139
+ Dir.mktmpdir do |tmp_dir|
140
+ tmp_dir = Dir.glob(tmp_dir)[0]
141
+ src_session.execute("git archive --format tar #{src_sha1} #{@module_info.local_path} | tar -C #{tmp_dir} -xf -")
142
+ tmp_module_dir = File.join(tmp_dir, @module_info.local_path)
143
+ files = FileHelper.find_matching_files(tmp_module_dir, false, "/**/*", File::FNM_DOTMATCH)
144
+ files.delete(".")
145
+ files.delete("..")
146
+ files.delete(RimInfo::InfoFileName)
147
+ files -= FileHelper.find_matching_files(tmp_module_dir, false, ignores)
148
+ # have source files now. Now clear destination folder and copy
149
+ prepare_empty_folder(dest_dir, ".git/**/*")
150
+ files.each do |f|
151
+ src_path = File.join(tmp_module_dir, f)
152
+ if File.file?(src_path)
153
+ path = File.join(dest_dir, f)
154
+ FileUtils.mkdir_p(File.dirname(path))
155
+ FileUtils.cp(src_path, path)
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ end
162
+
163
+ end