Merge pull request #259 from ghthor/add-git-grep-aliases
[PATCH] Add grep_shortcuts
This commit is contained in:
@@ -34,10 +34,10 @@ git_alias="g"
|
||||
# 1. 'SCM Breeze' functions
|
||||
git_status_shortcuts_alias="gs"
|
||||
git_add_shortcuts_alias="ga"
|
||||
git_add_patch_alias="gap"
|
||||
git_add_updated_alias="gau"
|
||||
git_show_files_alias="gsf"
|
||||
exec_scmb_expand_args_alias="ge"
|
||||
git_show_files_alias="gsf"
|
||||
git_commit_all_alias="gca"
|
||||
git_grep_shortcuts_alias="gtrep"
|
||||
# 2. Commands that handle paths (with shortcut args expanded)
|
||||
git_checkout_alias="gco"
|
||||
git_checkout_branch_alias="gcb"
|
||||
@@ -68,7 +68,8 @@ git_status_short_alias="gss"
|
||||
git_clean_alias="gce"
|
||||
git_clean_force_alias="gcef"
|
||||
git_add_all_alias="gaa"
|
||||
git_commit_all_alias="gca"
|
||||
git_add_patch_alias="gap"
|
||||
git_add_updated_alias="gau"
|
||||
git_commit_amend_alias="gcm"
|
||||
git_commit_amend_no_msg_alias="gcmh"
|
||||
git_commit_no_msg_alias="gch"
|
||||
|
||||
@@ -89,6 +89,7 @@ _alias "$git_add_shortcuts_alias" 'git_add_shortcuts'
|
||||
_alias "$exec_scmb_expand_args_alias" 'exec_scmb_expand_args'
|
||||
_alias "$git_show_files_alias" 'git_show_affected_files'
|
||||
_alias "$git_commit_all_alias" 'git_commit_all'
|
||||
_alias "$git_grep_shortcuts_alias" 'git_grep_shortcuts'
|
||||
|
||||
# Git Index alias
|
||||
_alias "$git_index_alias" 'git_index'
|
||||
|
||||
47
lib/git/grep_shortcuts.rb
Normal file
47
lib/git/grep_shortcuts.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: UTF-8
|
||||
|
||||
PROJECT_ROOT = File.exist?(".git") ? Dir.pwd : `\git rev-parse --show-toplevel 2> /dev/null`.strip
|
||||
|
||||
COLORS = {
|
||||
:rst => "\033[0m",
|
||||
:del => "\033[0;31m",
|
||||
:mod => "\033[0;32m",
|
||||
:new => "\033[0;33m",
|
||||
:ren => "\033[0;34m",
|
||||
:cpy => "\033[0;33m",
|
||||
:typ => "\033[0;35m",
|
||||
:unt => "\033[0;36m",
|
||||
:dark => "\033[2;37m",
|
||||
:branch => "\033[1m",
|
||||
:header => "\033[0m"
|
||||
}
|
||||
|
||||
COLOR_MATCH = /\e\[[0-9;]*[mK]/
|
||||
|
||||
output_files = []
|
||||
|
||||
stdin = STDIN.set_encoding(Encoding::ASCII_8BIT)
|
||||
|
||||
while stdin.gets
|
||||
if $. > 1000
|
||||
puts "Only showing first 1000 results. Please refine your search."
|
||||
break
|
||||
end
|
||||
print "#{COLORS[:dark]}[#{COLORS[:rst]}#{$.}#{COLORS[:dark]}]#{COLORS[:rst]} "
|
||||
matches = $_.match(/(^.+?)#{COLOR_MATCH}?:#{COLOR_MATCH}?(\d+)?/)
|
||||
file = matches[1]
|
||||
line = matches[2]
|
||||
output_files << "#{file}#{line ? ":#{line}" : ""}"
|
||||
puts $_
|
||||
end
|
||||
|
||||
print "@@filelist@@::"
|
||||
|
||||
output_files.each_with_index {|f,i|
|
||||
# If file starts with a '~', treat it as a relative path.
|
||||
# This is important when dealing with symlinks
|
||||
print "|" unless i == 0
|
||||
print f.start_with?("~") ? f.sub(/~/, '') : File.join(PROJECT_ROOT, f)
|
||||
}
|
||||
puts
|
||||
24
lib/git/grep_shortcuts.sh
Normal file
24
lib/git/grep_shortcuts.sh
Normal file
@@ -0,0 +1,24 @@
|
||||
git_grep_shortcuts() {
|
||||
fail_if_not_git_repo || return 1
|
||||
git_clear_vars
|
||||
# Run ruby script, store output
|
||||
tmp_grep_results="$(git rev-parse --git-dir)/tmp_grep_results_$$"
|
||||
git grep -n --color=always "$@" |
|
||||
/usr/bin/env ruby "$scmbDir/lib/git/grep_shortcuts.rb" >"$tmp_grep_results"
|
||||
|
||||
# Fetch list of files from last line of script output
|
||||
files="$(tail -1 "$tmp_grep_results" | sed 's%@@filelist@@::%%g')"
|
||||
|
||||
# Export numbered env variables for each file
|
||||
IFS="|"
|
||||
local e=1
|
||||
for file in ${=files}; do
|
||||
export $git_env_char$e="$file"
|
||||
let e++
|
||||
done
|
||||
IFS=$' \t\n'
|
||||
|
||||
# Print status
|
||||
cat "$tmp_grep_results" | sed '$d' | less -SfRMXFi
|
||||
rm -f "$tmp_grep_results"
|
||||
}
|
||||
@@ -22,6 +22,7 @@ if [[ -s "$HOME/.git.scmbrc" ]]; then
|
||||
source "$scmbDir/lib/git/keybindings.sh"
|
||||
source "$scmbDir/lib/git/status_shortcuts.sh"
|
||||
source "$scmbDir/lib/git/branch_shortcuts.sh"
|
||||
source "$scmbDir/lib/git/grep_shortcuts.sh"
|
||||
source "$scmbDir/lib/git/shell_shortcuts.sh"
|
||||
source "$scmbDir/lib/git/repo_index.sh"
|
||||
source "$scmbDir/lib/git/tools.sh"
|
||||
|
||||
Reference in New Issue
Block a user