Skip to content

Instantly share code, notes, and snippets.

@venkatd
Created May 30, 2012 22:42
Show Gist options
  • Save venkatd/2839388 to your computer and use it in GitHub Desktop.
Save venkatd/2839388 to your computer and use it in GitHub Desktop.
require 'net/ftp'
class FtpStorage
def initialize(host, username, password)
connect(host, username, password)
end
def connect(host, username, password)
@host, @username, @password = host, username, password
@ftp = Net::FTP.new(@host)
@ftp.passive = true
@ftp.debug_mode = true
@ftp.login(@username, @password)
end
def download(filepath)
@ftp.getbinaryfile(filepath, filepath)
end
def close
@ftp.close
end
end
class GitChange
attr_accessor :operation
attr_accessor :filepath
def self.from_line(line)
change_names = {'M' => :modified, 'C' => :copied, 'R' => :renamed, 'A' => :added, 'D' => :deleted, 'U' => :unmerged}
operation = change_names[ line[0] ]
filepath = line[1..-1].strip
GitChange.new(operation, filepath)
end
def initialize(operation, filepath)
self.operation = operation
self.filepath = filepath
end
def modified?
operation == :modified
end
alias_method :changed?, :modified?
def copied?
operation == :copied
end
def renamed?
operation == :renamed
end
def added?
operation == :added
end
alias_method :created?, :added?
def deleted?
operation == :deleted
end
def unmerged?
operation == :unmerged
end
def to_s
"#{operation}: #{filepath}"
end
end
class GitChangeset < Array
def self.of_cwd(a, b)
changes = GitChangeset.new
command = "git diff --name-status #{a} #{b}"
output = IO.popen(command)
output.readlines.each do |line|
changes << GitChange.from_line(line)
end
return changes
end
def with_operation(op)
op = op.to_sym
keep_if { |change| change.operation == op }
end
def modified
with_operation(:modified)
end
def added
with_operation(:added)
end
def deleted
with_operation(:deleted)
end
end
#storage = FtpStorage.new('50.63.212.1', 'venkatdinavahi', 'Dinavahi1979')
#storage.download('2012/rotem/contact.php')
#storage.close
changes = GitChangeset.of_cwd('HEAD~1', 'HEAD~3')
changes.added.each do |change|
puts change
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment