Class: GitHelper::LocalCode
- Inherits:
-
Object
- Object
- GitHelper::LocalCode
- Defined in:
- lib/git_helper/local_code.rb
Instance Method Summary collapse
- #branch ⇒ Object
- #change_remote(remote_name, remote_url) ⇒ Object
- #checkout_default ⇒ Object
- #clean_branches ⇒ Object
- #default_branch ⇒ Object
- #empty_commit ⇒ Object
- #forget_local_commits ⇒ Object
-
#generate_title(local_branch) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
- #github_repo? ⇒ Boolean
- #gitlab_project? ⇒ Boolean
- #https_remote?(remote) ⇒ Boolean
- #new_branch(branch_name) ⇒ Object
- #project_name ⇒ Object
-
#read_template(file_name) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
- #remote_name(remote) ⇒ Object
- #remote_project(remote) ⇒ Object
- #remote_source(remote) ⇒ Object
- #remotes ⇒ Object
- #ssh_remote?(remote) ⇒ Boolean
-
#template_options(identifiers) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
Instance Method Details
#branch ⇒ Object
81 82 83 84 |
# 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) ⇒ Object
32 33 34 |
# 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 ⇒ Object
5 6 7 |
# 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 ⇒ Object
18 19 20 21 22 23 |
# 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 ⇒ Object
86 87 88 |
# 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 ⇒ Object
14 15 16 |
# File 'lib/git_helper/local_code.rb', line 14 def empty_commit system('git commit --allow-empty -m "Empty commit"') end |
#forget_local_commits ⇒ Object
9 10 11 12 |
# 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) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# 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? ⇒ Boolean
68 69 70 |
# File 'lib/git_helper/local_code.rb', line 68 def github_repo? remotes.select { |remote| remote.include?('github') }.any? end |
#gitlab_project? ⇒ Boolean
72 73 74 |
# File 'lib/git_helper/local_code.rb', line 72 def gitlab_project? remotes.select { |remote| remote.include?('gitlab') }.any? end |
#https_remote?(remote) ⇒ Boolean
48 49 50 |
# File 'lib/git_helper/local_code.rb', line 48 def https_remote?(remote) remote.scan(%r{(https://)}).any? end |
#new_branch(branch_name) ⇒ Object
25 26 27 28 29 30 |
# 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 ⇒ Object
76 77 78 79 |
# 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) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
110 111 112 |
# File 'lib/git_helper/local_code.rb', line 110 def read_template(file_name) File.open(file_name).read end |
#remote_name(remote) ⇒ Object
40 41 42 |
# File 'lib/git_helper/local_code.rb', line 40 def remote_name(remote) remote.scan(/([a-zA-z]+)/).first.first end |
#remote_project(remote) ⇒ Object
52 53 54 55 56 57 58 |
# 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) ⇒ Object
60 61 62 63 64 65 66 |
# 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 ⇒ Object
36 37 38 |
# File 'lib/git_helper/local_code.rb', line 36 def remotes `git remote -v`.split("\n") end |
#ssh_remote?(remote) ⇒ Boolean
44 45 46 |
# File 'lib/git_helper/local_code.rb', line 44 def ssh_remote?(remote) remote.scan(/(git@)/).any? end |
#template_options(identifiers) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/git_helper/local_code.rb', line 92 def (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 |