Module: Overcommit::GitRepo
- Defined in:
- lib/overcommit/git_repo.rb
Overview
Provide a set of utilities for certain interactions with ‘git`.
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
Class Method Summary collapse
-
.all_files ⇒ Array<String>
Returns the names of all files that are tracked by git.
-
.extract_modified_lines(file_path, options) ⇒ Set
Extract the set of modified lines from a given file.
-
.extract_modified_lines_last_commit(file_path) ⇒ 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).
-
.modified_files(options) ⇒ Array<String>
Returns the names of all files that have been modified from compared to HEAD.
-
.modified_files_last_commit ⇒ Array<String>
Returns the names of all files that were modified in the last commit.
-
.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.
-
.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.
Class Method Details
.all_files ⇒ Array<String>
Returns the names of all files that are tracked by git.
72 73 74 75 76 |
# File 'lib/overcommit/git_repo.rb', line 72 def all_files `git ls-files`. split(/\n/). map { |relative_file| File.(relative_file) } end |
.extract_modified_lines(file_path, options) ⇒ Set
Extract the set of modified lines from a given file.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/overcommit/git_repo.rb', line 19 def extract_modified_lines(file_path, ) lines = Set.new flags = '--cached' if [:staged] refs = 'HEAD~ HEAD' if [:last_commit] `git diff --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 |
.extract_modified_lines_last_commit(file_path) ⇒ Set
Extract the set of modified lines from a given file.
44 45 46 |
# File 'lib/overcommit/git_repo.rb', line 44 def extract_modified_lines_last_commit(file_path) extract_modified_lines(file_path, last_commit: true) end |
.initial_commit? ⇒ true, false
Returns whether the current git branch is empty (has no commits).
80 81 82 |
# File 'lib/overcommit/git_repo.rb', line 80 def initial_commit? !Overcommit::Utils.execute(%w[git rev-parse HEAD]).success? end |
.modified_files(options) ⇒ Array<String>
Returns the names of all files that have been modified from compared to HEAD.
53 54 55 56 57 58 59 60 |
# File 'lib/overcommit/git_repo.rb', line 53 def modified_files() flags = '--cached' if [:staged] refs = 'HEAD~ HEAD' if [:last_commit] `git diff --name-only -z --diff-filter=ACM --ignore-submodules=all #{flags} #{refs}`. split("\0"). map { |relative_file| File.(relative_file) } end |
.modified_files_last_commit ⇒ Array<String>
Returns the names of all files that were modified in the last commit.
65 66 67 |
# File 'lib/overcommit/git_repo.rb', line 65 def modified_files_last_commit modified_files(last_commit: true) end |
.restore_cherry_pick_state ⇒ Object
Restore any relevant files that were present when repo was in the middle of a cherry-pick.
137 138 139 140 141 142 143 144 145 |
# File 'lib/overcommit/git_repo.rb', line 137 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}\n") 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/overcommit/git_repo.rb', line 117 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}\n") 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 |
.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].
105 106 107 108 109 110 111 112 113 |
# File 'lib/overcommit/git_repo.rb', line 105 def store_cherry_pick_state cherry_head = `git rev-parse CHERRY_PICK_HEAD 2> /dev/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].
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/overcommit/git_repo.rb', line 88 def store_merge_state merge_head = `git rev-parse MERGE_HEAD 2> /dev/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 |