Module: Git::Helpers

Defined in:
lib/git/helpers/utils.rb,
lib/git/helpers/browse.rb,
lib/git/helpers/update.rb,
lib/git/helpers/version.rb,
lib/git/helpers/pull_request.rb,
lib/git/helpers/create_upstream.rb

Overview

Provides a set of helper functions to make Git life easier

Defined Under Namespace

Modules: Utils

Constant Summary collapse

VERSION =
'0.5.2'.freeze

Class Method Summary collapse

Class Method Details

.browse(repo_dir = Dir.pwd, remote_name = 'origin', tree = nil) ⇒ Object

Opens the default web browser to the specified repositorys page based on

the the specified remote, and tree

Parameters:

  • repo_dir (String) (defaults to: Dir.pwd)

    Directory of the git repo to browse, defaults to current working dir

  • remote_name (String) (defaults to: 'origin')

    Name of the remote to browse, defaults to origin

  • tree (String) (defaults to: nil)

    Tree to browse, can be a branch name, tag, or commit hash



13
14
15
16
17
18
19
20
21
22
# File 'lib/git/helpers/browse.rb', line 13

def self.browse(repo_dir = Dir.pwd, remote_name = 'origin', tree = nil)
  repo = Git.open(repo_dir)
  raise "#{remote_name} is not a known remote" unless Utils.remote? repo, remote_name
  remote = Utils.remotes_hash(repo)[remote_name]
  tree ||= repo.current_branch
  url = remote.url.chomp('.git')
  url = Utils.transform_url(url)
  url << "/tree/#{tree}"
  Launchy.open url
end

.create_upstream(upstream_user, repo_dir = Dir.pwd, print_details = true) ⇒ Object

Updates a repositories current branch from upstream, and pushes those

to origin

Parameters:

  • upstream_user (String)

    The user of the upstream repo

  • repo_dir (String) (defaults to: Dir.pwd)

    The directory of the repo to update

  • print_details (Boolean) (defaults to: true)

    Print messages for each action or not



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/git/helpers/create_upstream.rb', line 12

def self.create_upstream(upstream_user, repo_dir = Dir.pwd, print_details = true)
  repo = Git.open(repo_dir)
  if Utils.remote?(repo, 'upstream')
    current = Utils.remotes_hash(repo)['upstream']
    puts "Upstream already exists with url: #{current.url}" if print_details
    return
  end
  remote = Utils.remotes_hash(repo)['origin']
  user = Utils.true_name(remote)
  url = Utils.transform_url(remote.url, 'git')
  url.sub!(user, upstream_user)
  puts "Adding upstream with URL #{url}" if print_details
  repo.add_remote('upstream', url)
end

.pull_request(repo_dir = Dir.pwd, target = 'upstream', target_branch = nil, source = 'origin', source_branch = nil) ⇒ Object

Opens the default web browser to an comparison page to open a pull request

Parameters:

  • repo_dir (String) (defaults to: Dir.pwd)

    Directory of the git repo to browse, defaults to current working dir

  • target_remote (String)

    Name of the target remote, default upstream

  • target_branch (String) (defaults to: nil)

    Name of the branch on the target remote to compare, defaults to current branch

  • source_remote (String)

    Name of the source remote, default origin

  • source_branch (String) (defaults to: nil)

    Name of the branch on the source remote to compare, defaults to current branch



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/git/helpers/pull_request.rb', line 14

def self.pull_request(repo_dir = Dir.pwd, target = 'upstream', target_branch = nil, source = 'origin', source_branch = nil)
  repo = Git.open(repo_dir)
  raise "#{target} is not a known remote" unless Utils.remote? repo, target
  raise "#{source} is not a known remote" unless Utils.remote? repo, source
  target_remote = Utils.remotes_hash(repo)[target]
  source_remote = Utils.remotes_hash(repo)[source]
  target_branch ||= repo.current_branch
  source_branch ||= repo.current_branch
  url = target_remote.url.chomp('.git')
  url = Utils.transform_url(url)
  url << "/compare/#{target_branch}...#{Utils.true_name(source_remote)}:#{source_branch}?expand=1"
  Launchy.open url
end

.update_repository(repo_dir = Dir.pwd, print_details = true) ⇒ Object

Updates a repositories current branch from upstream, and pushes those

to origin

Parameters:

  • repo_dir (String) (defaults to: Dir.pwd)

    The directory of the repo to update

  • print_details (Boolean) (defaults to: true)

    Print messages for each action or not



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/git/helpers/update.rb', line 11

def self.update_repository(repo_dir = Dir.pwd, print_details = true)
  repo = Git.open(repo_dir)
  remote = Utils.remote?(repo, 'upstream') ? 'upstream' : 'origin'
  current_branch = repo.current_branch
  print "Pulling changes from #{remote}/#{current_branch}..." if print_details
  repo.pull(remote, current_branch)
  puts "\tSUCCESS" if print_details
  if remote != 'origin'
    print "Pushing changes to origin/#{current_branch}..." if print_details
    repo.push('origin', current_branch)
    puts "\tSUCCESS" if print_details
  end
end