Class: Loca::Git

Inherits:
Object
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/loca/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, remote = nil) ⇒ Git

Returns a new instance of Git.



7
8
9
10
11
12
13
14
# File 'lib/loca/git.rb', line 7

def initialize(url, remote = nil)
  @url = Loca::URL.new(url)
  @branch_name = @url.branch_name

  ensure_git_repo
  ensure_no_unstashed_files
  @remote_name = remote || extract_remote_name
end

Instance Attribute Details

#branch_nameObject (readonly)

Returns the value of attribute branch_name.



5
6
7
# File 'lib/loca/git.rb', line 5

def branch_name
  @branch_name
end

Instance Method Details

#checkoutObject



22
23
24
# File 'lib/loca/git.rb', line 22

def checkout
  git "checkout #{@branch_name}", false # prints to stderr for some reason
end

#deleteObject



16
17
18
19
20
# File 'lib/loca/git.rb', line 16

def delete
  # Cannot delete a branch you are currently on:
  checkout_another_branch if @branch_name == current_branch
  git "branch -D #{@branch_name}"
end

#fetchObject



26
27
28
29
30
31
# File 'lib/loca/git.rb', line 26

def fetch
  # To avoid fatal error: Refusing to fetch into current branch
  delete unless first_time_creating?
  # Performs `git fetch upstream pull/PR_NUMBER/head:BRANCH_NAME`
  git "fetch #{@remote_name} pull/#{@url.pr_num}/head:#{@branch_name}", false # shellout has stderr for some reason
end

#first_time_creating?Boolean

Keep this a public method so we can prompt the user for overwrite

Returns:

  • (Boolean)


33
34
35
# File 'lib/loca/git.rb', line 33

def first_time_creating? # Keep this a public method so we can prompt the user for overwrite
  branches.include?(@branch_name) ? false : true
end

#git_match_http?(git, http) ⇒ Boolean

Example

git_match_http?(“github.com/smoll/loca.git”, “github.com/smoll/loca/pull/1”)

> true

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/loca/git.rb', line 41

def git_match_http?(git, http)
  format = lambda do |uri| # Strip off uri scheme & trailing '.git'
    uri.sub('https://', '')
    .sub('http://', '')
    .sub('git://', '')
    .sub(/.git$/, '')
  end
  format.call(http).start_with?(format.call(git))
end