Class: GitHelper::LocalCode

Inherits:
Object
  • Object
show all
Defined in:
lib/git_helper/local_code.rb

Instance Method Summary collapse

Instance Method Details

#branchObject



79
80
81
82
# File 'lib/git_helper/local_code.rb', line 79

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



30
31
32
# File 'lib/git_helper/local_code.rb', line 30

def change_remote(remote_name, remote_url)
  `git remote set-url #{remote_name} #{remote_url}`
end

#checkout_defaultObject



3
4
5
# File 'lib/git_helper/local_code.rb', line 3

def checkout_default
  system('git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@")')
end

#clean_branchesObject



16
17
18
19
20
21
# File 'lib/git_helper/local_code.rb', line 16

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_branchObject



84
85
86
# File 'lib/git_helper/local_code.rb', line 84

def default_branch
  `git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@" | tr -d "\n"`
end

#empty_commitObject



12
13
14
# File 'lib/git_helper/local_code.rb', line 12

def empty_commit
  system('git commit --allow-empty -m \"Empty commit\"')
end

#forget_local_commitsObject



7
8
9
10
# File 'lib/git_helper/local_code.rb', line 7

def forget_local_commits
  system('git pull')
  system('git reset --hard origin/HEAD')
end

#generate_title(local_branch) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/git_helper/local_code.rb', line 108

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/git_helper/local_code.rb', line 66

def github_repo?
  remotes.select { |remote| remote.include?('github') }.any?
end

#gitlab_project?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/git_helper/local_code.rb', line 70

def gitlab_project?
  remotes.select { |remote| remote.include?('gitlab') }.any?
end

#https_remote?(remote) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/git_helper/local_code.rb', line 46

def https_remote?(remote)
  remote.scan(/(https:\/\/)/).any?
end

#new_branch(branch_name) ⇒ Object



23
24
25
26
27
28
# File 'lib/git_helper/local_code.rb', line 23

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_nameObject



74
75
76
77
# File 'lib/git_helper/local_code.rb', line 74

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



104
105
106
# File 'lib/git_helper/local_code.rb', line 104

def read_template(file_name)
  File.open(file_name).read
end

#remote_name(remote) ⇒ Object



38
39
40
# File 'lib/git_helper/local_code.rb', line 38

def remote_name(remote)
  remote.scan(/([a-zA-z]+)/).first.first
end

#remote_project(remote) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/git_helper/local_code.rb', line 50

def remote_project(remote)
  if https_remote?(remote)
    remote.scan(/https:\/\/[\S]+\/([\S]*).git/).first.first
  elsif ssh_remote?(remote)
    remote.scan(/\/([\S]*).git/).first.first
  end
end

#remote_source(remote) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/git_helper/local_code.rb', line 58

def remote_source(remote)
  if https_remote?(remote)
    remote.scan(/https:\/\/([a-zA-z.]+)\//).first.first
  elsif ssh_remote?(remote)
    remote.scan(/git@([a-zA-z.]+):/).first.first
  end
end

#remotesObject



34
35
36
# File 'lib/git_helper/local_code.rb', line 34

def remotes
  `git remote -v`.split("\n")
end

#ssh_remote?(remote) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/git_helper/local_code.rb', line 42

def ssh_remote?(remote)
  remote.scan(/(git@)/).any?
end

#template_options(identifiers) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/git_helper/local_code.rb', line 88

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