Class: Octopress::Deploy::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/octopress-deploy/git.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Git

Returns a new instance of Git.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/octopress-deploy/git.rb', line 5

def initialize(options={})
  @options     = options
  @repo        = @options[:git_url]
  @branch      = @options[:git_branch]   || 'master'
  @remote      = @options[:remote]       || 'deploy'
  @remote_path = @options[:remote_path]  || ''
  @remote_path = @remote_path.sub(/^\//,'') #remove leading slash
  @site_dir    = File.expand_path(@options[:site_dir]  || '_site')
  @deploy_dir  = File.expand_path(@options[:deploy_dir] || '.deploy')
  @pull_dir    = @options[:dir]
  abort "Deploy Failed: Configure a git_url in #{@options[:config_file]} before deploying.".red if @repo.nil?
end

Class Method Details

.default_config(options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/octopress-deploy/git.rb', line 45

def self.default_config(options={})
  config = <<-CONFIG
#{"git_url: #{options[:git_url]}".ljust(40)}  # remote repository url, e.g. [email protected]:username/repo_name

# Note on git_branch:
# If using GitHub project pages, set the branch to 'gh-pages'.
# For GitHub user/organization pages or Heroku, set the branch to 'master'.
#
#{"git_branch: #{options[:git_branch] || 'master'}".ljust(40)}  # Git branch where static site files are commited

CONFIG
  config << "\n# " unless options[:remote_path]
  config << "#{"remote_path: #{options[:remote_path]}".ljust(38)}  # Destination directory"
end

Instance Method Details

#check_repoObject

Check to see if local deployment dir is configured to deploy.



37
38
39
40
41
42
43
# File 'lib/octopress-deploy/git.rb', line 37

def check_repo
  if Dir.exist? @deploy_dir
    FileUtils.cd @deploy_dir do
      return `git remote -v`.include? @repo
    end
  end
end

#clean_deployObject

Remove files in deploy dir, ensuring a 1:1 site files deployment.



107
108
109
# File 'lib/octopress-deploy/git.rb', line 107

def clean_deploy
  FileUtils.rm_rf(Dir.glob('*'), secure: true)
end

#copy_siteObject

Copy site files into deploy dir.



113
114
115
116
117
118
119
# File 'lib/octopress-deploy/git.rb', line 113

def copy_site
  target_dir = File.join(@deploy_dir, @remote_path).sub(/\/$/,'')
  FileUtils.cp_r @site_dir + '/.', target_dir
  message = "Site updated at: #{Time.now.utc}"
  `git add --all .`
  `git commit -m \"#{message}\"`
end

#git_pullObject

Attempt to pull from the remote branch



98
99
100
101
102
103
# File 'lib/octopress-deploy/git.rb', line 98

def git_pull
  if `git branch -a` =~ /remotes\/#{@remote}\/#{@branch}/ ||
     `git ls-remote #{@remote}` =~ /refs\/heads\/#{@branch}/
       `git pull #{@remote} #{@branch}`
  end
end

#git_pushObject



92
93
94
# File 'lib/octopress-deploy/git.rb', line 92

def git_push
  `git push #{@remote} #{@branch}`
end

#init_repoObject

If necessary create deploy directory and initialize it with deployment remote.



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
# File 'lib/octopress-deploy/git.rb', line 62

def init_repo
  return if check_repo
  FileUtils.mkdir_p @deploy_dir
  FileUtils.cd @deploy_dir do
    if Dir[@deploy_dir+'/*'].empty?

      # initialize the repository and add the remote.
      #
      `git init`
      `git remote add #{@remote} #{@repo}`

      # Attempt to pull from the remote.
      #
      if git_pull
        `git branch -m #{@branch}`

      # If no branch exists on remote, create one locally.
      else
        `echo "initialize deploy repo" > _`
        `git add .`
        `git commit -m \"initial commit\"`
        `git branch -m #{@branch}`
        `git rm _`
        `git add -u`
        `git commit -m 'cleanup'`
      end
    end
  end
end

#pullObject



31
32
33
# File 'lib/octopress-deploy/git.rb', line 31

def pull
  `git clone -b #{@branch} #{@repo} #{@pull_dir}`
end

#pushObject

Initialize, pull, copy and deploy.



20
21
22
23
24
25
26
27
28
29
# File 'lib/octopress-deploy/git.rb', line 20

def push
  init_repo
  puts "Syncing #{@site_dir.sub(Dir.pwd.strip+'/', '')} files to #{@repo}."
  FileUtils.cd @deploy_dir do
    git_pull
    clean_deploy
    copy_site
    git_push
  end
end