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
  @local       = 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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/octopress-deploy/git.rb', line 70

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_branchObject

Ensure that the deploy branch is not that same as the current working branch



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

def check_branch
  same_branch = `git branch -a` =~ /\* #{@branch}/

  if current_remote = `git remote -v`.match(/\s\S+/)
    same_remote = current_remote[0].match(/#{@repo}/)
  end

  if same_remote && same_branch
    puts "Deploy to #{@branch} canceled:".red
    puts "You cannot deploy to the same branch you are working in. This will overwrite the source for your site.\n"
    puts "First, back up your site's source to a branch:"
    puts "\n  git checkout -b source".yellow
    puts "  git push origin source".yellow
    puts "\nWith that, you'll work in the #{"source".bold} branch and deploy to the #{@branch.bold} branch."
    abort
  end
end

#check_deploy_dirObject

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



62
63
64
65
66
67
68
# File 'lib/octopress-deploy/git.rb', line 62

def check_deploy_dir
  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.



147
148
149
# File 'lib/octopress-deploy/git.rb', line 147

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

#copy_siteObject

Copy site files into deploy dir.



153
154
155
156
157
158
159
# File 'lib/octopress-deploy/git.rb', line 153

def copy_site
  target_dir = File.join(@deploy_dir, @remote_path).sub(/\/$/,'')
  FileUtils.cp_r @local + '/.', 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



138
139
140
141
142
143
# File 'lib/octopress-deploy/git.rb', line 138

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/octopress-deploy/git.rb', line 117

def git_push
  if `git remote -v` =~ /#{@remote}\s+#{Regexp.quote(@repo)}.+\(push\)/
    `git push #{@remote} #{@branch}`
  else
    remotes = `git remote -v`
    push_remote = remotes.match(/^origin\s+(.+)\s+\(push\)/)
    if push_remote
      abort %Q{Deployment remote #{@remote} is pointing to "#{push_remote[1]}" but configuration points to #{@remote}
  To reset your deployment, run:
  rm -rf #{@deploy_dir}
  octopress deploy}
    else
      abort %Q{Deployment remote configured improperly. To reset your deployment run:
  rm -rf #{@deploy_dir}
  octopress deploy}
    end
  end
end

#init_repoObject

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/octopress-deploy/git.rb', line 87

def init_repo
  return if check_deploy_dir
  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



36
37
38
# File 'lib/octopress-deploy/git.rb', line 36

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
30
31
32
33
34
# File 'lib/octopress-deploy/git.rb', line 20

def push
  if File.exist?(@local)
    check_branch
    init_repo
    puts "Syncing #{@local.sub(Dir.pwd.strip+'/', '')} files to #{@repo}."
    FileUtils.cd @deploy_dir do
      git_pull
      clean_deploy
      copy_site
      git_push
    end
  else
    abort "Cannot find site build at #{@local}. Be sure to build your site first."
  end
end