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

Parameters:

  • path (String) (defaults to: ".")

    Path to git repository



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

Parameters:

  • remote (String)

    The remote repository url

Returns:

  • (String)

    Branch name



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.

Parameters:

  • base (String) (defaults to: nil)

    The base commit to compare against

  • sha (String) (defaults to: "HEAD")

    The commit to compare

  • include_uncommitted (Boolean) (defaults to: true)

    Whether to include uncommitted changes

  • include_untracked (Boolean) (defaults to: false)

    Whether to include untracked files

Returns:

  • (Hash)

    A hash of file paths and the files changed in those files as a Set



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`.

Parameters:

  • sha (String)

    The git SHA of the commit

  • comparison_branch (String)

    The comparison branch

Returns:

  • (String)

    The nearest common ancestor (SHA)



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

Returns:

  • (String)

    Branch name



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