Class: Multipush::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logfileObject

Returns the value of attribute logfile.



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

def logfile
  @logfile
end

Instance Method Details

#go!(params = {}) ⇒ Object



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
98
99
100
# File 'lib/multipush/base.rb', line 27

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

  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



12
13
14
15
16
17
# File 'lib/multipush/base.rb', line 12

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

#my_system(cmd) ⇒ Object



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

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