Class: Munkey

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

Constant Summary collapse

DEFAULT_BRANCH =
'munkey'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gitpath, options) ⇒ Munkey

Returns a new instance of Munkey.



37
38
39
40
41
42
# File 'lib/munkey.rb', line 37

def initialize(gitpath, options)
  @gitpath = gitpath
  munkey_file = File.join(gitpath, '.git', 'munkey.yml')
  @ftpdetails = YAML.load_file(munkey_file) if File.exist?(munkey_file)
  @verbose = options[:verbose] || false
end

Class Method Details

.clone(ftpsrc, repo_path, options = {}) ⇒ Object

Raises:

  • (InvalidSource)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/munkey.rb', line 12

def clone(ftpsrc, repo_path, options = {})
  src = URI::parse(ftpsrc)
  raise InvalidSource unless src.is_a?(URI::FTP)
  
  repo = create_repo(repo_path, options)
  repo.save_ftp_details(src)
  repo.pull_ftp_files
  repo.commit_changes
  repo.create_branch
  repo
end

Instance Method Details

#clone_to_tmp(branch = DEFAULT_BRANCH) ⇒ Object



103
104
105
106
107
# File 'lib/munkey.rb', line 103

def clone_to_tmp(branch = DEFAULT_BRANCH)
  tmp_repo = File.join ENV['TMPDIR'], create_tmpname
  system("git clone#{git_quiet} -b #{branch} #{@gitpath} #{tmp_repo}")
  tmp_repo
end

#commit_changes(dst = nil) ⇒ Object



91
92
93
94
95
# File 'lib/munkey.rb', line 91

def commit_changes(dst = nil)
  Dir.chdir(dst || @gitpath) do
    system("git add .") && system("git commit -m 'Pull from ftp://#{@ftpdetails[:host]}#{@ftpdetails[:path]} at #{Time.now.to_s}'")
  end
end

#create_branch(branch_name = DEFAULT_BRANCH) ⇒ Object



97
98
99
100
101
# File 'lib/munkey.rb', line 97

def create_branch(branch_name = DEFAULT_BRANCH)
  Dir.chdir(@gitpath) do
    system("git branch #{branch_name}")
  end
end

#files_changed_between_branches(branch = DEFAULT_BRANCH) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/munkey.rb', line 127

def files_changed_between_branches(branch = DEFAULT_BRANCH)
  changes = { :changed => [], :removed => [] }
  Dir.chdir(@gitpath) do
    `git diff --name-status #{branch} master`.strip.split("\n").each do |f|
      status, name = f.split(/\s+/, 2)
      if status == "D"
        changes[:removed] << name
      else
        changes[:changed] << name unless name == '.gitignore'
      end
    end
  end
  
  changes
end

#last_pull_date(branch = DEFAULT_BRANCH) ⇒ Object



160
161
162
163
164
165
# File 'lib/munkey.rb', line 160

def last_pull_date(branch = DEFAULT_BRANCH)
  Dir.chdir(@gitpath) do
    commits = `git log --format=oneline #{branch}`.strip.split("\n")
    commits.find {|c| c =~ /^[a-f\d]{40} Pull from ftp:\/\/.* at ([\w +:]+)$/ } ? Time.parse($1) : nil      
  end
end

#list_ftp_changes(changes) ⇒ Object



155
156
157
158
# File 'lib/munkey.rb', line 155

def list_ftp_changes(changes)
  changes[:changed].each {|f| puts "WILL UPLOAD #{f}" }
  changes[:removed].each {|f| puts "WILL REMOVE #{f}" }
end

#merge_foreign_changes(branch = DEFAULT_BRANCH) ⇒ Object



115
116
117
118
119
# File 'lib/munkey.rb', line 115

def merge_foreign_changes(branch = DEFAULT_BRANCH)
  Dir.chdir(@gitpath) do
    system("git merge #{branch}")
  end
end

#merge_pushed_changes(tmp_repo) ⇒ Object



121
122
123
124
125
# File 'lib/munkey.rb', line 121

def merge_pushed_changes(tmp_repo)
  Dir.chdir(tmp_repo) do
    system("git pull#{git_quiet} origin master")
  end
end

#pull(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/munkey.rb', line 44

def pull(options = {})
  default_options = { :merge => true, :quick => false }
  default_options.merge! options
  
  tmp_repo = clone_to_tmp
  pull_ftp_files(tmp_repo, default_options[:quick])
  commit_changes(tmp_repo)
  push_into_base_repo(tmp_repo)
  FileUtils.rm_rf(tmp_repo)
  merge_foreign_changes if default_options[:merge]
end

#pull_ftp_files(dst = nil, quick = false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/munkey.rb', line 75

def pull_ftp_files(dst = nil, quick = false)
  if quick
    since = last_pull_date
  end
  
  dst ||= @gitpath
  gitignore = GitignoreParser.parse(dst)
  ftp = FtpSync.new(@ftpdetails[:host], @ftpdetails[:user], @ftpdetails[:password], :ignore => gitignore, :verbose => @verbose)
  ftp.pull_dir(dst, @ftpdetails[:path], :delete => true, :since => (since || nil), :skip_errors => true) do |p|
    Dir.chdir(dst) do
      relpath = p.gsub %r{^#{Regexp.escape(dst)}\/}, ''
      system("git rm -r#{git_quiet} '#{relpath}'")
    end
  end  
end

#push(dryrun = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/munkey.rb', line 56

def push(dryrun = false)
  changes = files_changed_between_branches
  
  list_ftp_changes(changes) && return if dryrun
  
  update_ftp_server(changes)
  tmp_repo = clone_to_tmp
  merge_pushed_changes(tmp_repo)
  push_into_base_repo(tmp_repo)
  FileUtils.rm_rf(tmp_repo)
end

#push_into_base_repo(tmp_repo, branch = DEFAULT_BRANCH) ⇒ Object



109
110
111
112
113
# File 'lib/munkey.rb', line 109

def push_into_base_repo(tmp_repo, branch = DEFAULT_BRANCH)
  Dir.chdir(tmp_repo) do
    system("git push#{git_quiet} origin #{branch}:#{branch}")
  end
end

#save_ftp_details(ftp_uri) ⇒ Object



68
69
70
71
72
73
# File 'lib/munkey.rb', line 68

def save_ftp_details(ftp_uri)
  @ftpdetails = { :host => ftp_uri.host, :path => "/#{ftp_uri.path}", :user => ftp_uri.user, :password => ftp_uri.password }
  File.open File.join(@gitpath, '.git', 'munkey.yml'), 'w' do |f|
    f.write @ftpdetails.to_yaml
  end
end

#update_ftp_server(changes) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/munkey.rb', line 143

def update_ftp_server(changes)
  ftp = FtpSync.new(@ftpdetails[:host], @ftpdetails[:user], @ftpdetails[:password], :verbose => @verbose)

  unless changes[:changed].size == 0
    ftp.push_files @gitpath, @ftpdetails[:path], changes[:changed]
  end
  
  unless changes[:removed].size == 0
    ftp.remove_files @ftpdetails[:path], changes[:removed]
  end
end