Class: QuietQuality::VersionControlSystems::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/quiet_quality/version_control_systems/git.rb

Constant Summary collapse

Error =
Class.new(VersionControlSystems::Error)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = ".") ⇒ Git

Initializer



13
14
15
16
# File 'lib/quiet_quality/version_control_systems/git.rb', line 13

def initialize(path = ".")
  @path = path
  @git = ::Git.open(path)
end

Instance Attribute Details

#gitObject (readonly)

Returns the value of attribute git.



6
7
8
# File 'lib/quiet_quality/version_control_systems/git.rb', line 6

def git
  @git
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/quiet_quality/version_control_systems/git.rb', line 6

def path
  @path
end

Class Method Details

.default_branch(remote:) ⇒ String

The default branch for the given remote



51
52
53
# File 'lib/quiet_quality/version_control_systems/git.rb', line 51

def self.default_branch(remote:)
  ::Git.default_branch(remote)
end

Instance Method Details

#changed_files(base: nil, sha: "HEAD", include_uncommitted: true, include_untracked: false) ⇒ Hash

Retrieves the files changed in the given commit compared to the base. When no base is given, the default branch is used as the base. When no sha is given, the HEAD commit is used. Optionally, uncommitted changes can be included in the result, as well as untracked files.



28
29
30
31
32
33
34
35
# File 'lib/quiet_quality/version_control_systems/git.rb', line 28

def changed_files(base: nil, sha: "HEAD", include_uncommitted: true, include_untracked: false)
  base_commit = comparison_base(sha: sha, comparison_branch: base || default_branch)
  [
    committed_changed_files(base_commit, sha),
    include_uncommitted ? uncommitted_changed_files : nil,
    include_untracked ? untracked_changed_files : nil
  ].compact.reduce(&:merge)
end

#comparison_base(sha:, comparison_branch:) ⇒ String

Determines the nearest common ancestor for the given ‘sha` compared to the `branch`.



63
64
65
# File 'lib/quiet_quality/version_control_systems/git.rb', line 63

def comparison_base(sha:, comparison_branch:)
  git.merge_base(comparison_branch, sha).first.sha
end

#default_branchString

The default branch for the default remote for the local git repository



41
42
43
# File 'lib/quiet_quality/version_control_systems/git.rb', line 41

def default_branch
  self.class.default_branch(remote: git.remote.url)
end