Created
April 2, 2018 20:17
-
-
Save lee-dohm/b882305665229f51d16a542953420255 to your computer and use it in GitHub Desktop.
Sample script to copy templates from a given location into a repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
# Use: atom-templates [repo_name] | |
# | |
# 1. Clone the repository if it doesn't already exist; fetch `master` if it does | |
# 2. Clean up old `template-update` branch if it exists | |
# 3. Check out new branch `template-update` based on `origin/master` | |
# 4. Copy template files over | |
# 5. If there have been changes commit them, push the branch and open the repo in a browser window | |
BRANCH_NAME = 'template-update' | |
REPO_HOME = File.expand_path(ENV['ATOM_REPOS_HOME']) || File.expand_path('../..', File.dirname(__FILE__)) | |
TRIAGE_HOME = File.expand_path('..', File.dirname(__FILE__)) | |
def clone(name) | |
git("clone [email protected]:atom/#{name}.git #{File.join(REPO_HOME, name)}") | |
end | |
def current_branch | |
`git rev-parse --abbrev-ref HEAD`.strip | |
end | |
def git(command, options = {}) | |
puts "git #{command}" | |
result = system("git #{command}") | |
unless result || options[:ignore_failures] | |
raise StandardError, "Git command 'git #{command}' failed with error code #{$?}" | |
end | |
end | |
def repo_exists?(name) | |
repo_dir = File.join(REPO_HOME, name) | |
git_dir = File.join(repo_dir, '.git') | |
Dir.exist?(repo_dir) && Dir.exist?(git_dir) | |
end | |
def working_tree_clean? | |
system('git diff-index --quiet HEAD --') | |
end | |
repo_name = ARGV[0] | |
repo_dir = File.join(REPO_HOME, repo_name) | |
if repo_exists?(repo_name) | |
Dir.chdir(repo_dir) do | |
git('fetch origin master') | |
end | |
else | |
clone(repo_name) | |
end | |
Dir.chdir(repo_dir) do | |
git('checkout master') if current_branch == BRANCH_NAME | |
git("branch -df #{BRANCH_NAME}", ignore_failures: true) | |
git("checkout -b #{BRANCH_NAME} origin/master") | |
FileUtils.cp(File.join(TRIAGE_HOME, 'issue-template.md'), File.join(repo_dir, 'ISSUE_TEMPLATE.md')) | |
if repo_name == 'atom' | |
FileUtils.cp(File.join(TRIAGE_HOME, 'pull-request-template-atom.md'), File.join(repo_dir, 'PULL_REQUEST_TEMPLATE.md')) | |
else | |
FileUtils.cp(File.join(TRIAGE_HOME, 'pull-request-template.md'), File.join(repo_dir, 'PULL_REQUEST_TEMPLATE.md')) | |
end | |
if working_tree_clean? | |
puts | |
puts('No changes') | |
else | |
git('add ISSUE_TEMPLATE.md PULL_REQUEST_TEMPLATE.md') | |
git('commit -m ":memo: Update issue and PR templates"') | |
git("push -u origin #{BRANCH_NAME}") | |
system("open https://github.com/atom/#{repo_name}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intended to be called in a loop from a list of repositories to copy templates to.