class GitHelper::LocalCode
Public Instance Methods
branch()
click to toggle source
# File lib/git_helper/local_code.rb, line 81 def branch # Get the current branch by looking in the list of branches for the * `git branch`.scan(/\*\s(\S*)/).first.first end
change_remote(remote_name, remote_url)
click to toggle source
# File lib/git_helper/local_code.rb, line 32 def change_remote(remote_name, remote_url) `git remote set-url #{remote_name} #{remote_url}` end
checkout_default()
click to toggle source
# File lib/git_helper/local_code.rb, line 5 def checkout_default system('git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@")') end
clean_branches()
click to toggle source
# File lib/git_helper/local_code.rb, line 18 def clean_branches system('git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@")') system('git pull') system('git fetch -p') system('git branch -vv | grep "origin/.*: gone]" | awk "{print \$1}" | grep -v "*" | xargs git branch -D') end
default_branch()
click to toggle source
# File lib/git_helper/local_code.rb, line 86 def default_branch `git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@" | tr -d "\n"` end
empty_commit()
click to toggle source
# File lib/git_helper/local_code.rb, line 14 def empty_commit system('git commit --allow-empty -m "Empty commit"') end
forget_local_commits()
click to toggle source
# File lib/git_helper/local_code.rb, line 9 def forget_local_commits system('git pull') system('git reset --hard origin/HEAD') end
generate_title(local_branch)
click to toggle source
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
# File lib/git_helper/local_code.rb, line 116 def generate_title(local_branch) branch_arr = local_branch.split(local_branch.include?('_') ? '_' : '-') return if branch_arr.empty? if branch_arr.length == 1 branch_arr.first.capitalize elsif branch_arr[0].scan(/(\w+)/).any? && branch_arr[1].scan(/(\d+)/).any? # branch includes jira_123 at beginning issue = "#{branch_arr[0].upcase}-#{branch_arr[1]}" description = branch_arr[2..-1].join(' ') "#{issue} #{description.capitalize}" elsif branch_arr[0].scan(/(\w+-\d+)/).any? # branch includes string jira-123 at beginning issue = branch_arr[0].upcase description = branch_arr[1..-1].join(' ') "#{issue} #{description.capitalize}" else # plain words branch_arr[0..-1].join(' ').capitalize end end
github_repo?()
click to toggle source
# File lib/git_helper/local_code.rb, line 68 def github_repo? remotes.select { |remote| remote.include?('github') }.any? end
gitlab_project?()
click to toggle source
# File lib/git_helper/local_code.rb, line 72 def gitlab_project? remotes.select { |remote| remote.include?('gitlab') }.any? end
https_remote?(remote)
click to toggle source
# File lib/git_helper/local_code.rb, line 48 def https_remote?(remote) remote.scan(%r{(https://)}).any? end
new_branch(branch_name)
click to toggle source
# File lib/git_helper/local_code.rb, line 25 def new_branch(branch_name) system('git pull') system("git branch --no-track #{branch_name}") system("git checkout #{branch_name}") system("git push --set-upstream origin #{branch_name}") end
project_name()
click to toggle source
# File lib/git_helper/local_code.rb, line 76 def project_name # Get the repo/project name by looking in the remote URLs for the full name `git remote -v`.scan(/\S\s*\S+.com\S{1}(\S*).git/).first.first end
read_template(file_name)
click to toggle source
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
# File lib/git_helper/local_code.rb, line 110 def read_template(file_name) File.open(file_name).read end
remote_name(remote)
click to toggle source
# File lib/git_helper/local_code.rb, line 40 def remote_name(remote) remote.scan(/([a-zA-z]+)/).first.first end
remote_project(remote)
click to toggle source
# File lib/git_helper/local_code.rb, line 52 def remote_project(remote) if https_remote?(remote) remote.scan(%r{https://\S+/(\S*).git}).first.first elsif ssh_remote?(remote) remote.scan(%r{/(\S*).git}).first.first end end
remote_source(remote)
click to toggle source
# File lib/git_helper/local_code.rb, line 60 def remote_source(remote) if https_remote?(remote) remote.scan(%r{https://([a-zA-z.]+)/}).first.first elsif ssh_remote?(remote) remote.scan(/git@([a-zA-z.]+):/).first.first end end
remotes()
click to toggle source
# File lib/git_helper/local_code.rb, line 36 def remotes `git remote -v`.split("\n") end
ssh_remote?(remote)
click to toggle source
# File lib/git_helper/local_code.rb, line 44 def ssh_remote?(remote) remote.scan(/(git@)/).any? end
template_options(identifiers)
click to toggle source
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
# File lib/git_helper/local_code.rb, line 92 def template_options(identifiers) nested_templates = Dir.glob( File.join("#{identifiers[:template_directory]}/#{identifiers[:nested_directory_name]}", '*.md'), File::FNM_DOTMATCH | File::FNM_CASEFOLD ) non_nested_templates = Dir.glob( File.join(identifiers[:template_directory], "#{identifiers[:non_nested_file_name]}.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD ) root_templates = Dir.glob( File.join('.', "#{identifiers[:non_nested_file_name]}.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD ) nested_templates.concat(non_nested_templates).concat(root_templates).uniq end