Class: RIM::SyncHelper

Inherits:
CommandHelper show all
Defined in:
lib/rim/sync_helper.rb

Constant Summary

Constants inherited from Processor

Processor::GerritServer, Processor::MaxThreads

Instance Method Summary collapse

Methods inherited from CommandHelper

#add_unique_module_info, #all_module_paths_from_path, #check_arguments, #check_ready, #create_module_info, #find_file_dir_in_workspace, #get_remote_branch_format, #module_from_path, #module_paths, #modules_from_manifest, #modules_from_paths

Methods included from Manifest

#parse_manifest, #read_manifest

Methods inherited from Processor

#clone_or_fetch_repository, #commit, #each_module_parallel, #get_absolute_remote_url, #get_relative_path, #local_changes?, #module_git_path, #module_tmp_git_path, #remote_path, shorten_path

Constructor Details

#initialize(workspace_root, logger, module_infos = nil) ⇒ SyncHelper

Returns a new instance of SyncHelper.



11
12
13
14
# File 'lib/rim/sync_helper.rb', line 11

def initialize(workspace_root, logger, module_infos = nil)
  @module_infos = []
  super(workspace_root, logger, module_infos)
end

Instance Method Details

#add_module_info(module_info) ⇒ Object

called to add a module info



17
18
19
# File 'lib/rim/sync_helper.rb', line 17

def add_module_info(module_info)
  @module_infos.push(module_info)
end

#sync(message = nil, rebase = nil, split = true) ⇒ Object

sync all module changes into rim branch



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rim/sync_helper.rb', line 22

def sync(message = nil, rebase = nil, split = true)
  # get the name of the current workspace branch
  RIM::git_session(@ws_root) do |s|
    branch = s.current_branch || ''
    rim_branch = "rim/" + branch
    branch_sha1 = nil
    changed_modules = nil
    if branch.empty?
      raise RimException.new("Not on a git branch.")
    elsif branch.start_with?("rim/")
      raise RimException.new("The current git branch '#{branch}' is a rim integration branch. Please switch to a non rim branch to proceed.")
    else
      branch = "refs/heads/#{branch}"
      branch_sha1 = s.rev_sha1(rim_branch)
      remote_rev = get_latest_remote_revision(s, branch)
      rev = get_latest_clean_path_revision(s, branch, remote_rev)
      if !s.has_branch?(rim_branch) || has_ancestor?(s, branch, s.rev_sha1(rim_branch)) || !has_ancestor?(s, rim_branch, remote_rev)
        s.execute("git branch -f #{rim_branch} #{rev}")
        branch_sha1 = s.rev_sha1(rim_branch)
      end
      remote_url = "file://" + @ws_root
      @logger.debug("Folder for temporary git repositories: #{@rim_path}")
      tmpdir = clone_or_fetch_repository(remote_url, module_tmp_git_path(".ws"), "Cloning workspace git...")
      RIM::git_session(tmpdir) do |tmp_session|
        tmp_session.execute("git reset --hard")
        tmp_session.execute("git clean -xdf")
        # use -f here to prevent git checkout from checking for untracked files which might be overwritten. 
        # this is safe since we removed any untracked files before.
        # this is a workaround for a name case problem on windows:
        # if a file's name changes case between the current head and the checkout target,
        # git checkout will report the file with the new name as untracked and will fail
        tmp_session.execute("git checkout -B #{rim_branch} -f remotes/origin/#{rim_branch}")
        changed_modules = sync_modules(tmp_session, message)
        if !split
          tmp_session.execute("git reset --soft #{branch_sha1}")
          commit(tmp_session, message ? message : get_commit_message(changed_modules)) if tmp_session.uncommited_changes?
        end
        tmp_session.execute("git push #{remote_url} #{rim_branch}:#{rim_branch}")
      end
    end
    if !changed_modules.empty?
      if rebase
        s.execute("git rebase #{rim_branch}")
        @logger.info("Changes have been commited to branch #{rim_branch} and workspace has been rebased successfully.")
      else
        @logger.info("Changes have been commited to branch #{rim_branch}. Rebase to apply changes to workspace.")
      end
    else
      @logger.info("No changes.")
    end
  end
end