Class: GitXplorer
- Inherits:
-
Object
show all
- Defined in:
- lib/git_xplorer.rb
Defined Under Namespace
Classes: Error, GitObject
Instance Method Summary
collapse
Constructor Details
Returns a new instance of GitXplorer.
82
83
84
85
86
|
# File 'lib/git_xplorer.rb', line 82
def initialize
@cwd = Array.new
@repo = %x(git rev-parse --show-toplevel).split("/")[-1].strip
@tree = refresh_tree
end
|
Instance Method Details
#cd(path = nil) ⇒ Object
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
|
# File 'lib/git_xplorer.rb', line 11
def cd(path = nil)
path ||= ""
@cwd.clear if (path.empty?)
target = @tree.get(absolute_path(path))
if (target.nil?)
raise GitXplorer::Error::NoFileOrDirectory.new(path)
elsif (!target.is_a?(GitXplorer::GitObject::Directory))
raise GitXplorer::Error::NotDirectory.new(path)
end
path.split("/"). each do |dir|
case dir
when "."
when ".."
@cwd.delete_at(-1)
else
@cwd.push(dir)
end
end
return target
end
|
#exist?(path) ⇒ Boolean
39
40
41
|
# File 'lib/git_xplorer.rb', line 39
def exist?(path)
return @tree.exist?(absolute_path(path))
end
|
#get_completions(input) ⇒ Object
43
44
45
|
# File 'lib/git_xplorer.rb', line 43
def get_completions(input)
return @tree.get_completions(absolute_path(input))
end
|
#get_filenames(dir = "", pattern = ".") ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/git_xplorer.rb', line 47
def get_filenames(dir = "", pattern = ".")
dir ||= ""
pattern ||= "."
found = %x(
git log --diff-filter=ACR --name-only --pretty=tformat: \
-- #{dir}
).split("\n").map do |file|
file.gsub(/^#{dir}/, "")
end.select do |file|
file.match(/#{pattern}/)
end
return found
end
|
#git(args, refresh = true) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/git_xplorer.rb', line 63
def git(args, refresh = true)
if (args.match(/.*;.*/))
raise GitXplorer::Error::NoSemicolonsAllowed.new
end
if (args.match(/(\$|<)\(.+\)/))
raise GitXplorer::Error::NoSubshellsAllowed.new
end
if (args.match(/\&\&|\|\|/))
raise GitXplorer::Error::NoChainingAllowed.new
end
out = %x(git #{args})
refresh = true if (refresh.nil?)
@tree = refresh_tree if (refresh)
return out
end
|
#ls(path = nil) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/git_xplorer.rb', line 88
def ls(path = nil)
path = absolute_path(path)
target = @tree.get(path)
if (target.nil?)
raise GitXplorer::Error::NoFileOrDirectory.new(path)
end
return target.children
end
|
#pwd ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/git_xplorer.rb', line 100
def pwd
return @repo if (@cwd.empty?)
return "#{@repo}/#{@cwd.join("/")}#{"/" if ([email protected]?)}"
end
|
#pwd_short ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/git_xplorer.rb', line 108
def pwd_short
return @repo if (@cwd.empty?)
return @cwd[-3..-1].join("/") if (@cwd.length >= 3)
return "#{@repo}/#{@cwd.join("/")}#{"/" if ([email protected]?)}"
end
|
#search(dir = "", pattern = ".") ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/git_xplorer.rb', line 135
def search(dir = "", pattern = ".")
dir ||= ""
pattern ||= "."
found = %x(
git grep -HIinP "#{pattern}" $(git rev-list --all) -- \
#{dir}
).split("\n").map do |line|
line.gsub(
/^([^:]+):([^:]+):(\d+):(.*)$/,
"\\2:\\1:\\3:\\4"
)
end
return found
end
|
#show(file) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/git_xplorer.rb', line 152
def show(file)
if (!exist?(file))
raise GitXplorer::Error::NoFileOrDirectory.new(file)
end
file, _, rev = absolute_path(file).partition(":")
target = @tree.get(file)
if (!target.is_a?(GitXplorer::GitObject::File))
raise GitXplorer::Error::NotFile.new(file)
end
if (!rev.empty? && !target.has_child?(rev))
raise GitXplorer::Error::InvalidRevision.new(rev)
end
rev = target.newest.name if (rev.empty?)
system("git --no-pager show #{rev}:#{file}")
end
|
#vim(file, readonly = false) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/git_xplorer.rb', line 177
def vim(file, readonly = false)
if (!exist?(file))
raise GitXplorer::Error::NoFileOrDirectory.new(file)
end
file, _, rev = absolute_path(file).partition(":")
target = @tree.get(file)
if (!target.is_a?(GitXplorer::GitObject::File))
raise GitXplorer::Error::NotFile.new(file)
end
if (!rev.empty? && !target.has_child?(rev))
raise GitXplorer::Error::InvalidRevision.new(rev)
end
rev = target.newest.name if (rev.empty?)
op = nil
op = "vi" if (ScoobyDoo.where_are_you("vi"))
op = "vim" if (ScoobyDoo.where_are_you("vim"))
readonly ||= false
if (readonly && op.nil?)
system("git show #{rev}:#{file}")
else
name = "/tmp/gitXplorer_"
name += File.read("/dev/urandom", 4).unpack("H*").join
File.open(name, "w") do |f|
f.write(%x(git show #{rev}:#{file}))
end
system("#{op}#{" -R" if (readonly)} #{name}")
FileUtils.rm_f(name)
end
end
|