-
Notifications
You must be signed in to change notification settings - Fork 0
/
gf
executable file
·65 lines (54 loc) · 1.83 KB
/
gf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
# Parses the output of git status, and returns the names of files that match your query expression.
# You can use this script to pipe the name of files in your checkout into other tools. Examples:
# gc `gf .flexlibproperties` # restores all files that match fliexlibproperties
# mate `gf unmerged` # Open all unmerged files in an editor
#
# Usage:
# gf text
# which returns "abc/com/yahoo/api/controls/text.mxml" when the output of git status looks like:
# modified: abc/text.mxml
# modified: abc/form.mxml
#
# You can use many query terms after gf, and all lines that match any of them will be returned,
# separated by spaces.
#
# Add -n to have the files in the output separated by newlines instead of spaces.
#
# The output from git status looks like this:
#
# On branch adsets
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: api/controls/text.mxml
# modified: api/controls/account/adsets/form.mxml
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# ...........
lines = `git status`.split("\n")
is_file = /^#\t/
files = []
# Removes "modified" etc. from the beginning of the line
def strip(line)
line.gsub(/^#\s+(deleted|modified|new file|unmerged|typechange)?:?\s*/, "")
end
ARGV.each do |search_term|
# ignore options
next if search_term.index("-") == 0
search = /#{search_term.gsub(".", "\\.")}/i
lines.each do |line|
next unless line.match(is_file)
if search.match(line.downcase)
files.push(strip(line))
end
end
end
files.uniq!
separator = ARGV.index("-n") ? "\n" : " "
# Escape any spaces in the paths by replacing them with question marks.
# Old school file globbing hack by Darrell!
files = files.map { |filename| filename.gsub(" ", "?" ) }
puts files.join(separator)