Skip to content

Commit

Permalink
Allowing to specify which folder of a repo to deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
7twelve committed Mar 7, 2013
1 parent 173c5ed commit 30c4e04
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 55 deletions.
12 changes: 6 additions & 6 deletions lib/dandelion/backend/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module Dandelion
module Backend
class FTP < Backend::Base
scheme 'ftp'

def initialize(config)
require 'net/ftp'
@config = config
@ftp = Net::FTP.new
@ftp.connect(@config['host'], @config['port'] || Net::FTP::FTP_PORT)
@ftp.login(@config['username'], @config['password'])
@ftp.passive = @config['passive'].nil? ? true : to_b(@config['passive'])
@ftp.chdir(@config['path']) if @config['path']
@ftp.chdir(@config['remote_path']) if @config['remote_path']
end

def read(file)
Expand Down Expand Up @@ -47,9 +47,9 @@ def delete(file)
rescue Net::FTPPermError => e
end
end

def to_s
"ftp://#{@config['username']}@#{@config['host']}/#{@config['path']}"
"ftp://#{@config['username']}@#{@config['host']}/#{@config['remote_path']}"
end

private
Expand Down Expand Up @@ -77,10 +77,10 @@ def mkdir_p(dir)
end
end
end

def to_b(value)
return [true, 'true', 1, '1', 'T', 't'].include?(value.class == String ? value.downcase : value)
end
end
end
end
end
24 changes: 12 additions & 12 deletions lib/dandelion/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
module Dandelion
module Command
class InvalidCommandError < StandardError; end

class Base
class << self
@@commands = {}
Expand All @@ -23,11 +23,11 @@ def create(name)
raise InvalidCommandError unless @@commands.include?(name)
@@commands[name]
end

def commands
@@commands.keys
end

def require_commands
Dir.glob(File.join(File.dirname(__FILE__), 'command', '*.rb')) { |file| require file }
end
Expand Down Expand Up @@ -65,16 +65,16 @@ def parser(options)
opts.on('--repo=[REPO]', 'Use the given repository') do |repo|
options[:repo] = File.expand_path(repo)
end

options[:config] = nil
opts.on('--config=[CONFIG]', 'Use the given configuration file') do |config|
options[:config] = File.expand_path(config)
end
end
end

private

def closest_repo(dir)
if File.exists?(File.join(dir, '.git'))
dir
Expand All @@ -85,15 +85,15 @@ def closest_repo(dir)
end

def initialize(options)
@options = options
@options = options
@config = YAML.load_file(@options[:config])
@repo = Git::Repo.new(@options[:repo])

yield(self) if block_given?
end

protected

def log
Dandelion.logger
end
Expand All @@ -113,12 +113,12 @@ def backend
exit 1
end
end

def deployment(revision, backend = nil)
begin
backend ||= backend()
revision_file = @config['revision_file'].nil? ? '.revision' : @config['revision_file']
options = { :exclude => @config['exclude'], :revision => revision, :revision_file => revision_file, :dry => @options[:dry] }
options = { :exclude => @config['exclude'], :local_path => @config['local_path'], :revision => revision, :revision_file => revision_file, :dry => @options[:dry] }
Deployment::Deployment.create(@repo, backend, options)
rescue Git::DiffError
log.fatal('Error: could not generate diff')
Expand Down
50 changes: 25 additions & 25 deletions lib/dandelion/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Dandelion
module Deployment
class RemoteRevisionError < StandardError; end
class FastForwardError < StandardError; end

class Deployment
class << self
def create(repo, backend, options)
Expand All @@ -15,66 +15,66 @@ def create(repo, backend, options)
end
end
end

def initialize(repo, backend, options = {})
@repo = repo
@backend = backend
@options = { :exclude => [], :revision => 'HEAD', :revision_file => '.revision' }.merge(options)
@tree = Git::Tree.new(@repo, @options[:revision])
@options = { :exclude => [], :local_path => '/', :revision => 'HEAD', :revision_file => '.revision' }.merge(options)
@tree = Git::Tree.new(@repo, @options)

if @options[:dry]
# Stub out the destructive backend methods
def @backend.write(file, data); end
def @backend.delete(file); end
end
end

def local_revision
@tree.revision
end

def remote_revision
nil
end

def write_revision
@backend.write(@options[:revision_file], local_revision)
end

def validate
begin
raise FastForwardError if fast_forwardable
rescue Grit::Git::CommandFailed
end
end

def log
Dandelion.logger
end

protected

def exclude_file?(file)
@options[:exclude].map { |e| file.start_with?(e) }.any? unless @options[:exclude].nil?
end

private

def fast_forwardable
!@repo.git.native(:cherry, {:raise => true, :timeout => false}).empty?
end
end

class DiffDeployment < Deployment
def initialize(repo, backend, options = {})
super(repo, backend, options)
@diff = Git::Diff.new(@repo, read_remote_revision, @options[:revision])
@diff = Git::Diff.new(@repo, read_remote_revision, @options)
end

def remote_revision
@diff.from_revision
end

def deploy
if !revisions_match? && any?
deploy_changed
Expand All @@ -86,7 +86,7 @@ def deploy
write_revision
end
end

def deploy_changed
@diff.changed.each do |file|
if exclude_file?(file)
Expand All @@ -97,7 +97,7 @@ def deploy_changed
end
end
end

def deploy_deleted
@diff.deleted.each do |file|
if exclude_file?(file)
Expand All @@ -108,17 +108,17 @@ def deploy_deleted
end
end
end

def any?
@diff.changed.any? || @diff.deleted.any?
end

def revisions_match?
remote_revision == local_revision
end

private

def read_remote_revision
begin
@backend.read(@options[:revision_file]).chomp
Expand All @@ -127,7 +127,7 @@ def read_remote_revision
end
end
end

class FullDeployment < Deployment
def deploy
@tree.files.each do |file|
Expand Down
25 changes: 13 additions & 12 deletions lib/dandelion/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ module Dandelion
module Git
class DiffError < StandardError; end
class RevisionError < StandardError; end

class Repo < Grit::Repo
def initialize(dir)
super(dir)
end
end

class Diff
attr_reader :from_revision, :to_revision

@files = nil

def initialize(repo, from_revision, to_revision)
@repo = repo
@from_revision = from_revision
Expand All @@ -36,11 +36,11 @@ def deleted
end

private

def diff
@repo.git.native(:diff, {:name_status => true, :raise => true}, from_revision, to_revision)
end

def parse(diff)
files = {}
diff.split("\n").each do |line|
Expand All @@ -52,24 +52,25 @@ def parse(diff)
end

class Tree
def initialize(repo, revision)
def initialize(repo, options = {})
@repo = repo
@commit = @repo.commit(revision)
@options = options
@commit = @repo.commit(@options[:revision])
raise RevisionError if @commit.nil?
@tree = @commit.tree
end

def files
@repo.git.native(:ls_tree, {:name_only => true, :r => true}, revision).split("\n")
@repo.git.native(:ls_tree, {:name_only => true, :r => true}, @options[:revision], @options[:local_path]).split("\n")
end

def show(file)
(@tree / file).data
end

def revision
@commit.sha
end
end
end
end
end

0 comments on commit 30c4e04

Please sign in to comment.