Skip to content

Commit

Permalink
fix up amend alias detection
Browse files Browse the repository at this point in the history
  • Loading branch information
benmelz committed Feb 22, 2024
1 parent 036a2fb commit 4969f26
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/overcommit/hook_context/helpers/file_modifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def amendment?
cmd = Overcommit::Utils.parent_command
return unless cmd

amend_pattern = 'commit(\s.*)?\s--amend(\s|$)'
amend_pattern = /commit(\s.*)?\s--amend/

# Since the ps command can return invalid byte sequences for commands
# containing unicode characters, we replace the offending characters,
Expand All @@ -24,18 +24,11 @@ def amendment?
encode('UTF-8')
end

return @amendment if
# True if the command is a commit with the --amend flag
@amendment = !(/\s#{amend_pattern}/ =~ cmd).nil?
# True if the command is a commit with the --amend flag
return @amendment if @amendment = cmd.match?(amend_pattern)

# Check for git aliases that call `commit --amend`
`git config --get-regexp "^alias\\." "#{amend_pattern}"`.
scan(/alias\.([-\w]+)/). # Extract the alias
each do |match|
return @amendment if
# True if the command uses a git alias for `commit --amend`
@amendment = !(/git(\.exe)?\s+#{match[0]}/ =~ cmd).nil?
end
return @amendment if @amendment = command_is_amend_alias?(cmd, amend_pattern)

@amendment
end
Expand Down Expand Up @@ -74,6 +67,22 @@ def modified_lines_in_file(file)
end
@modified_lines[file]
end

private

def command_is_amend_alias?(cmd, amend_pattern)
`git config --get-regexp "^alias"`.split("\n").each do |alias_def|
alias_map = alias_def.match /alias\.(?<to>[-\w]+)\s+(?<from>.+)/
next unless alias_map

alias_from_match = alias_map[:from].match? amend_pattern
alias_to_match = cmd.match? /git(\.exe)?\s+#{alias_map[:to]}/

# True if the command uses a git alias for `commit --amend`
return true if @amendment = alias_from_match && alias_to_match
end
false
end
end
end
end

0 comments on commit 4969f26

Please sign in to comment.