kch (owner)

Revisions

gist: 225509 Download_button fork
public
Description:
Assimilates git submodules' files, gets properly rid of the submodule metadata.
Public Clone URL: git://gist.github.com/225509.git
Embed All Files: show embed
modsux.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#!/usr/bin/env ruby
require 'yaml'
require 'pathname'
require 'shellwords'
 
module_url_by_path = Dir['**/.gitmodules'].inject({}) do |h, path|
  IO.read(path).scan(/^\[submodule .*\].*\n((?:\s+.*\n)+)/).flatten.each do |s_attrs|
    h_attrs = s_attrs.strip.split(/\n/).inject({}) do |h_attrs, s_attr_line|
      k, v = s_attr_line.strip.split(/\s+=\s+/, 2)
      h_attrs[k.to_sym] = v
      h_attrs
    end
    h[Pathname.new(File.dirname(path)).join(h_attrs[:path]).to_s] = h_attrs[:url]
  end
  h
end
 
submodules = `git submodule status --recursive`.scan(/^(.)(\w+) (.*) \((.*)\)$/).map do |status, commit, path, ref|
  { :status => status, :commit => commit, :path => path, :ref => ref, :url => module_url_by_path[path] }
end
 
if submodules.any? { |h| h[:status] != ' ' }
  puts "Status failed for some submodules. Fix and try again."
  exit 1
end
 
# config cleanup
f_config = ".git/config"
in_sub = false
lines = ""
IO.read(f_config).lines.each do |line|
  next in_sub = true if line =~ /^\[submodule /
  next lines << line unless in_sub
  next if line =~ /^\s/
  in_sub = false
  redo
end
open(f_config, "w") { |f| f.write(lines) }
 
# fs, index cleanup
submodules.each { |h| h.delete :status }
submodules.each do |h|
  path = Pathname.new(h[:path])
  puts `rm -rf #{path.join(".git").to_s.shellescape}`
  puts `rm -f #{path.join(".gitmodules").to_s.shellescape}`
  puts `git rm --ignore-unmatch --cached #{path.to_s.shellescape}`
end
puts `git rm .gitmodules`
 
# add it all
submodules.each do |h|
  path = Pathname.new(h[:path])
  puts `git add #{path.to_s.shellescape}`
end
 
# commit it all
msg = "Assimilated all submodules:\n\n#{submodules.to_yaml}"
puts `git commit -m #{msg.shellescape}`