Class: Multipush::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/multipush/base.rb

Instance Method Summary collapse

Instance Method Details

#go!(params = {}) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/multipush/base.rb', line 25

def go!(params = {})
  # clear log file
  system("cat /dev/null > multipush.log")

  git_cmd = "git"
  remote_prefix = "multipush_"
  config_file_name = "multipush.yml"
  config = nil

  raise "Could not find config file #{config_file_name}" unless File.exists?(config_file_name)

  begin
    config = YAML::load(File.new(config_file_name))
  rescue => e
    raise "Error loading config file #{config_file_name}: #{e}"
  end

  # do some config checks
  raise "dirs must be an Array" unless config["dirs"].is_a?(Array)
  raise "remotes must be a Hash" unless config["remotes"].is_a?(Hash)

  # the remotes have been defined. good.
  # let's go.
  # apply the list of remotes to every repo.

  repo_count = 0

  for dir in config["dirs"]
    oldest_dir = Dir.pwd
    # change
    Dir.chdir dir
    # get all subdirs
    subdirs = Dir["*/"]
    # gogo
    for subdir in subdirs
      olddir = Dir.pwd
      #puts subdir
      # change to the dir
      Dir.chdir subdir

      # verbose
      log(subdir)

      repo_count += 1

      ############################################################################
      # apply the list of remotes to every repo
      ############################################################################

      config["remotes"].each do |remote, endpoint|
        the_remote = "#{remote_prefix}#{remote}"
        # remove the remote, just to make sure
        # this allows the user to change remote location in the multipush config file
        # instead of having to manage different git repo's.
        my_system("#{git_cmd} remote rm #{the_remote}")
        # now re-add it again
        my_system("#{git_cmd} remote add #{the_remote} #{endpoint}/#{subdir}")
        # let's push
        my_system("#{git_cmd} push #{the_remote} --all")
      end

      ############################################################################
      # cleanup
      ############################################################################

      Dir.chdir olddir
    end
  
    Dir.chdir oldest_dir
  end

  log("Done. Processed #{repo_count} repos.")
end

#log(str) ⇒ Object



10
11
12
13
14
15
# File 'lib/multipush/base.rb', line 10

def log(str)
  str = " [#{Time.now}] #{str}"
  
  puts str
  system("echo '#{str}' >> multipush.log")
end

#my_system(cmd) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/multipush/base.rb', line 17

def my_system(cmd)
  #puts cmd
  # Don't print stderror, log that as well.
  #output = `#{cmd} 2>&1`
  #system("echo #{output} >> multipush.log")
  system(cmd)
end