Module: Overcommit::GitRepo
- Defined in:
- lib/overcommit/git_repo.rb
Overview
Provide a set of utilities for certain interactions with ‘git`.
Defined Under Namespace
Classes: Submodule, SubmoduleStatus
Constant Summary collapse
- DIFF_HUNK_REGEX =
Regular expression used to extract diff ranges from hunks of diff output.
/ ^@@\s [^\s]+\s # Ignore old file range \+(\d+)(?:,(\d+))? # Extract range of hunk containing start line and number of lines \s@@.*$ /x
- SUBMODULE_STATUS_REGEX =
Regular expression used to extract information from lines of ‘git submodule status` output
/ ^\s*(?<prefix>[-+U]?)(?<sha1>\w+) \s(?<path>[^\s]+?) (?:\s\((?<describe>.+)\))?$ /x
Class Method Summary collapse
-
.all_files ⇒ Array<String>
Returns the names of all files that are tracked by git.
-
.branches_containing_commit(commit_ref) ⇒ Array<String>
Returns the names of all branches containing the given commit.
-
.current_branch ⇒ String
Returns the name of the currently checked out branch.
-
.extract_modified_lines(file_path, options) ⇒ Set
Extract the set of modified lines from a given file.
-
.initial_commit? ⇒ true, false
Returns whether the current git branch is empty (has no commits).
-
.list_files(paths = [], options = {}) ⇒ Array<String>
Returns the names of files in the given paths that are tracked by git.
-
.modified_files(options) ⇒ Array<String>
Returns the names of all files that have been modified compared to HEAD.
-
.restore_cherry_pick_state ⇒ Object
Restore any relevant files that were present when repo was in the middle of a cherry-pick.
-
.restore_merge_state ⇒ Object
Restore any relevant files that were present when repo was in the middle of a merge.
-
.staged_submodule_removals ⇒ Object
Returns the submodules that have been staged for removal.
-
.store_cherry_pick_state ⇒ Object
Store any relevant files that are present when repo is in the middle of a cherry-pick.
-
.store_merge_state ⇒ Object
Store any relevant files that are present when repo is in the middle of a merge.
-
.submodule_statuses(options = {}) ⇒ Array<SubmoduleStatus>
Returns a list of SubmoduleStatus objects, one for each submodule in the parent repository.
-
.submodules(options = {}) ⇒ Array<Overcommit::GitRepo::Submodule>
Returns the current set of registered submodules.
-
.tracked?(path) ⇒ true, false
Returns whether the specified file/path is tracked by this repository.
Class Method Details
.all_files ⇒ Array<String>
Returns the names of all files that are tracked by git.
126 127 128 129 130 131 |
# File 'lib/overcommit/git_repo.rb', line 126 def all_files `git ls-files`. split(/\n/). map { |relative_file| File.(relative_file) }. reject { |file| File.directory?(file) } # Exclude submodule directories end |
.branches_containing_commit(commit_ref) ⇒ Array<String>
Returns the names of all branches containing the given commit.
263 264 265 266 267 268 |
# File 'lib/overcommit/git_repo.rb', line 263 def branches_containing_commit(commit_ref) `git branch --column=dense --contains #{commit_ref}`. sub(/\((HEAD )?detached (from|at) .*?\)/, ''). # ignore detached HEAD split(/\s+/). reject { |s| s.empty? || s == '*' } end |
.current_branch ⇒ String
Returns the name of the currently checked out branch.
272 273 274 |
# File 'lib/overcommit/git_repo.rb', line 272 def current_branch `git symbolic-ref --short -q HEAD`.chomp end |
.extract_modified_lines(file_path, options) ⇒ Set
Extract the set of modified lines from a given file.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/overcommit/git_repo.rb', line 65 def extract_modified_lines(file_path, ) lines = Set.new flags = '--cached' if [:staged] refs = [:refs] subcmd = [:subcmd] || 'diff' `git #{subcmd} --no-color --no-ext-diff -U0 #{flags} #{refs} -- "#{file_path}"`. scan(DIFF_HUNK_REGEX) do |start_line, lines_added| lines_added = (lines_added || 1).to_i # When blank, one line was added cur_line = start_line.to_i lines_added.times do lines.add cur_line cur_line += 1 end end lines end |
.initial_commit? ⇒ true, false
Returns whether the current git branch is empty (has no commits).
135 136 137 |
# File 'lib/overcommit/git_repo.rb', line 135 def initial_commit? !Overcommit::Utils.execute(%w[git rev-parse HEAD]).success? end |
.list_files(paths = [], options = {}) ⇒ Array<String>
Returns the names of files in the given paths that are tracked by git.
107 108 109 110 111 112 113 |
# File 'lib/overcommit/git_repo.rb', line 107 def list_files(paths = [], = {}) ref = [:ref] || 'HEAD' `git ls-tree --name-only #{ref} "#{paths.join('" "')}"`. split(/\n/). map { |relative_file| File.(relative_file) }. reject { |file| File.directory?(file) } # Exclude submodule directories end |
.modified_files(options) ⇒ Array<String>
Returns the names of all files that have been modified compared to HEAD.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/overcommit/git_repo.rb', line 90 def modified_files() flags = '--cached' if [:staged] refs = [:refs] subcmd = [:subcmd] || 'diff' `git #{subcmd} --name-only -z --diff-filter=ACMR --ignore-submodules=all #{flags} #{refs}`. split("\0"). map(&:strip). reject(&:empty?). map { |relative_file| File.(relative_file) } end |
.restore_cherry_pick_state ⇒ Object
Restore any relevant files that were present when repo was in the middle of a cherry-pick.
192 193 194 195 196 197 198 199 200 |
# File 'lib/overcommit/git_repo.rb', line 192 def restore_cherry_pick_state if @cherry_head File.open(File.('CHERRY_PICK_HEAD', Overcommit::Utils.git_dir), 'w') do |f| f.write(@cherry_head) end @cherry_head = nil end end |
.restore_merge_state ⇒ Object
Restore any relevant files that were present when repo was in the middle of a merge.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/overcommit/git_repo.rb', line 172 def restore_merge_state if @merge_head FileUtils.touch(File.('MERGE_MODE', Overcommit::Utils.git_dir)) File.open(File.('MERGE_HEAD', Overcommit::Utils.git_dir), 'w') do |f| f.write(@merge_head) end @merge_head = nil end if @merge_msg File.open(File.('MERGE_MSG', Overcommit::Utils.git_dir), 'w') do |f| f.write("#{@merge_msg}\n") end @merge_msg = nil end end |
.staged_submodule_removals ⇒ Object
Returns the submodules that have been staged for removal.
‘git` has an unexpected behavior where removing a submodule without committing (i.e. such that the submodule directory is removed and the changes to the index are staged) and then doing a hard reset results in the index being wiped but the empty directory of the once existent submodule being restored (but with no content).
This prevents restoration of the stash of the submodule index changes, which breaks pre-commit hook restorations of the working index.
Thus we expose this helper so the restoration code can manually delete the directory.
220 221 222 223 224 225 226 227 228 |
# File 'lib/overcommit/git_repo.rb', line 220 def staged_submodule_removals # There were no submodules before, so none could have been removed return [] if `git ls-files .gitmodules`.empty? previous = submodules(ref: 'HEAD') current = submodules previous - current end |
.store_cherry_pick_state ⇒ Object
Store any relevant files that are present when repo is in the middle of a cherry-pick.
Restored via [#restore_cherry_pick_state].
160 161 162 163 164 165 166 167 168 |
# File 'lib/overcommit/git_repo.rb', line 160 def store_cherry_pick_state cherry_head = `git rev-parse CHERRY_PICK_HEAD 2> #{File::NULL}`.chomp # Store the merge state if we're in the middle of resolving a merge # conflict. This is necessary since stashing removes the merge state. if cherry_head != 'CHERRY_PICK_HEAD' @cherry_head = cherry_head end end |
.store_merge_state ⇒ Object
Store any relevant files that are present when repo is in the middle of a merge.
Restored via [#restore_merge_state].
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/overcommit/git_repo.rb', line 143 def store_merge_state merge_head = `git rev-parse MERGE_HEAD 2> #{File::NULL}`.chomp # Store the merge state if we're in the middle of resolving a merge # conflict. This is necessary since stashing removes the merge state. if merge_head != 'MERGE_HEAD' @merge_head = merge_head end merge_msg_file = File.('MERGE_MSG', Overcommit::Utils.git_dir) @merge_msg = File.open(merge_msg_file).read if File.exist?(merge_msg_file) end |
.submodule_statuses(options = {}) ⇒ Array<SubmoduleStatus>
Returns a list of SubmoduleStatus objects, one for each submodule in the parent repository.
50 51 52 53 54 55 56 57 58 |
# File 'lib/overcommit/git_repo.rb', line 50 def submodule_statuses( = {}) flags = '--recursive' if [:recursive] `git submodule status #{flags}`. scan(SUBMODULE_STATUS_REGEX). map do |prefix, sha1, path, describe| SubmoduleStatus.new(prefix, sha1, path, describe) end end |
.submodules(options = {}) ⇒ Array<Overcommit::GitRepo::Submodule>
Returns the current set of registered submodules.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/overcommit/git_repo.rb', line 234 def submodules( = {}) ref = [:ref] modules = [] IniParse.parse(`git show #{ref}:.gitmodules`).each do |section| # git < 1.8.5 does not update the .gitmodules file with submodule # changes, so when we are looking at the current state of the work tree, # we need to check if the submodule actually exists via another method, # since the .gitmodules file we parsed does not represent reality. if ref.nil? && GIT_VERSION < '1.8.5' result = Overcommit::Utils.execute(%W[ git submodule status #{section['path']} ]) next unless result.success? end modules << Submodule.new(section['path'], section['url']) end modules rescue IniParse::IniParseError => ex raise Overcommit::Exceptions::GitSubmoduleError, "Unable to read submodule information from #{ref}:.gitmodules file: #{ex.}" end |
.tracked?(path) ⇒ true, false
Returns whether the specified file/path is tracked by this repository.
119 120 121 |
# File 'lib/overcommit/git_repo.rb', line 119 def tracked?(path) Overcommit::Utils.execute(%W[git ls-files #{path} --error-unmatch]).success? end |