Module: Autoshell::Git

Included in:
Base
Defined in:
lib/autoshell/git.rb

Overview

Git repo stuff

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#branchString

Returns (master) branch of the current repo.

Returns:

  • (String)

    (master) branch of the current repo



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/autoshell/git.rb', line 7

module Git
  def branch
    @branch ||= 'master'
  end
  attr_writer :branch

  # Set the branch from a repo url
  #
  # @param [String] Git repository URL
  # @return [string] Branch name
  def branch_from_repo_url(repo_url)
    if repo_url =~ /#\S+[^\/]/
      @branch = repo_url.split('#')[1]
    else
      @branch = 'master'
    end
  end

  def commit_hash_for_checkout
    @commit_hash_for_checkout ||= 'HEAD'
  end
  attr_writer :commit_hash_for_checkout

  # Has this repo been cloned to disk?
  #
  # @return [Boolean] True if this dir is a cloned repo
  def git?
    dir? '.git'
  end

  # Update the repo on disk
  #
  # @return [String] Output of git commands
  def update
    [
      git('checkout', '.'),
      git('clean', '-ffd'),
      git('fetch', 'origin'),
      git('checkout', branch),
      git('pull', '--recurse-submodules=yes'),
      git('checkout', commit_hash_for_checkout),
      git('submodule', 'update', '--init'),
      git('clean', '-ffd')
    ].join("\n")
  end

  # Checkout a different branch
  #
  # @param [String] Branch name
  # @return [String] Output of git commands
  def switch(new_branch)
    self.branch = new_branch
    update
  end

  # Clone a repo to disk from the url
  #
  # @param [String] Git repository URL
  # @return [String] Output of git commands
  def clone(repo_url)
    mkpdir working_dir
    run 'git', 'clone', '--recursive', repo_url.split('#')[0], working_dir
    branch_from_repo_url(repo_url)
    update
  end

  # Checkout a specific hash on the repo
  #
  # @param [String] Git version hash
  # @return [String] Output of git commands
  def checkout_version(version_hash)
    git 'checkout', version_hash || 'HEAD'
  end

  # Get the current commit hash
  #
  # @param [String] (HEAD) Branch or tag
  # @return [String] Git commit hash
  def commit_hash(branch_or_tag = nil)
    @version = git 'rev-parse', branch_or_tag || 'HEAD'
  end
  alias_method :version, :commit_hash

  # Get a tar archive of the repo as a string
  #
  # @param [String] (HEAD) Branch or tag
  # @return [String] Zip-formatted binary string
  def archive(branch_or_tag = nil)
    git 'archive', branch_or_tag || 'HEAD', binmode: true
  end

  # Run a git command
  #
  # @param [*Array<String>] Command
  # @return [String] Command output
  def git(*args)
    cd { run(*['git'] + args) }
  end
end

#commit_hash_for_checkoutObject



25
26
27
# File 'lib/autoshell/git.rb', line 25

def commit_hash_for_checkout
  @commit_hash_for_checkout ||= 'HEAD'
end

Instance Method Details

#archive(branch_or_tag = nil) ⇒ String

Get a tar archive of the repo as a string

Parameters:

  • (HEAD) (String)

    Branch or tag

Returns:

  • (String)

    Zip-formatted binary string



94
95
96
# File 'lib/autoshell/git.rb', line 94

def archive(branch_or_tag = nil)
  git 'archive', branch_or_tag || 'HEAD', binmode: true
end

#branch_from_repo_url(repo_url) ⇒ string

Set the branch from a repo url

Parameters:

  • Git (String)

    repository URL

Returns:

  • (string)

    Branch name



17
18
19
20
21
22
23
# File 'lib/autoshell/git.rb', line 17

def branch_from_repo_url(repo_url)
  if repo_url =~ /#\S+[^\/]/
    @branch = repo_url.split('#')[1]
  else
    @branch = 'master'
  end
end

#checkout_version(version_hash) ⇒ String

Checkout a specific hash on the repo

Parameters:

  • Git (String)

    version hash

Returns:

  • (String)

    Output of git commands



77
78
79
# File 'lib/autoshell/git.rb', line 77

def checkout_version(version_hash)
  git 'checkout', version_hash || 'HEAD'
end

#clone(repo_url) ⇒ String

Clone a repo to disk from the url

Parameters:

  • Git (String)

    repository URL

Returns:

  • (String)

    Output of git commands



66
67
68
69
70
71
# File 'lib/autoshell/git.rb', line 66

def clone(repo_url)
  mkpdir working_dir
  run 'git', 'clone', '--recursive', repo_url.split('#')[0], working_dir
  branch_from_repo_url(repo_url)
  update
end

#commit_hash(branch_or_tag = nil) ⇒ String Also known as: version

Get the current commit hash

Parameters:

  • (HEAD) (String)

    Branch or tag

Returns:

  • (String)

    Git commit hash



85
86
87
# File 'lib/autoshell/git.rb', line 85

def commit_hash(branch_or_tag = nil)
  @version = git 'rev-parse', branch_or_tag || 'HEAD'
end

#git(*args) ⇒ String

Run a git command

Parameters:

  • Command (*Array<String>)

Returns:

  • (String)

    Command output



102
103
104
# File 'lib/autoshell/git.rb', line 102

def git(*args)
  cd { run(*['git'] + args) }
end

#git?Boolean

Has this repo been cloned to disk?

Returns:

  • (Boolean)

    True if this dir is a cloned repo



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

def git?
  dir? '.git'
end

#switch(new_branch) ⇒ String

Checkout a different branch

Parameters:

  • Branch (String)

    name

Returns:

  • (String)

    Output of git commands



57
58
59
60
# File 'lib/autoshell/git.rb', line 57

def switch(new_branch)
  self.branch = new_branch
  update
end

#updateString

Update the repo on disk

Returns:

  • (String)

    Output of git commands



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/autoshell/git.rb', line 40

def update
  [
    git('checkout', '.'),
    git('clean', '-ffd'),
    git('fetch', 'origin'),
    git('checkout', branch),
    git('pull', '--recurse-submodules=yes'),
    git('checkout', commit_hash_for_checkout),
    git('submodule', 'update', '--init'),
    git('clean', '-ffd')
  ].join("\n")
end