Class: LocalMerge

Inherits:
Object
  • Object
show all
Defined in:
lib/github_merge/local.rb

Constant Summary collapse

OUT_DIR =
"#{Dir.pwd}/merged"

Instance Method Summary collapse

Constructor Details

#initialize(config, options) ⇒ LocalMerge

Returns a new instance of LocalMerge.



5
6
7
8
# File 'lib/github_merge/local.rb', line 5

def initialize(config, options)
  @config = config
  @options = options
end

Instance Method Details

#beginning_shaObject



61
62
63
64
65
66
67
# File 'lib/github_merge/local.rb', line 61

def beginning_sha
  if @options.all_svn?
    `git log --pretty=format:%H --reverse`.split("\n")[1]
  else
    `git log --pretty=format:%H --reverse`.split("\n").first
  end
end

#clone_reposObject



20
21
22
23
24
# File 'lib/github_merge/local.rb', line 20

def clone_repos
  @config["repositories"].each do |repo|
    `git clone #{repo["url"]} #{repo["sub directory"]}`
  end
end

#create_empty_merged_repoObject



32
33
34
35
# File 'lib/github_merge/local.rb', line 32

def create_empty_merged_repo
  puts 'Creating merged repository'
  `git init #{merge_repo_name}`
end

#git_filter_branch_move(subdir) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/github_merge/local.rb', line 48

def git_filter_branch_move(subdir)
  puts "Moving repository #{subdir} to subdirectory #{subdir}..."

  sed = sed_command_to_use
  Dir.chdir subdir do
    %x[git filter-branch --index-filter \
      'git ls-files -s | #{sed} "s@\t@&#{subdir}/@" |
              GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                      git update-index --index-info &&
       mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' #{beginning_sha}..HEAD]
  end
end

#merge!Object



10
11
12
13
14
15
16
17
18
# File 'lib/github_merge/local.rb', line 10

def merge!
  FileUtils.mkdir_p OUT_DIR
  Dir.chdir OUT_DIR do
    clone_repos
    move_repos_to_subdir
    create_empty_merged_repo
    merge_repositories
  end
end

#merge_repo_nameObject



77
78
79
# File 'lib/github_merge/local.rb', line 77

def merge_repo_name
  @config["merged repository"]
end

#merge_repositoriesObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/github_merge/local.rb', line 37

def merge_repositories
  Dir.chdir merge_repo_name do
    @config["repositories"].each_with_index do |repo, index|
      repo_name = repo["sub directory"]
      `git remote add #{repo_name} ../#{repo_name}`
      `git fetch #{repo_name}`
      `git merge --no-edit #{repo_name}/master`
    end
  end
end

#merged?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'lib/github_merge/local.rb', line 81

def merged?
  merge_repo_dir = "#{OUT_DIR}/#{merge_repo_name}"
  return false unless Dir.exist? merge_repo_dir
  Dir.chdir merge_repo_dir do
    return false unless Dir.exists? '.git'
    return `git log`.split("\n").size > 0
  end
end

#move_repos_to_subdirObject



26
27
28
29
30
# File 'lib/github_merge/local.rb', line 26

def move_repos_to_subdir
  @config["repositories"].each do |repo|
    git_filter_branch_move(repo["sub directory"])
  end
end

#sed_command_to_useObject



69
70
71
72
73
74
75
# File 'lib/github_merge/local.rb', line 69

def sed_command_to_use
  if RbConfig::CONFIG['host_os'] =~ /darwin/
    'gsed'
  else
    'sed'
  end
end