forked from Dieterbe/git-scripts
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-find
More file actions
Latest commit
executable file
·259 lines (220 loc) · 5.5 KB
/
Copy pathgit-find
File metadata and controls
executable file
·259 lines (220 loc) · 5.5 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env ruby
# git-find, version 1.0
#
# by John Wiegley <johnw@newartisans.com>
#
# usage: git find <commit or tag>...
#
# Performs an exhaustive search of your object database to inform you exactly
# how the given commit or tag fits in your repository. It tells you all the
# parents and children of the commit/tag, and every name it can be referenced
# by. It will tell you if it can be reached only by a reflog entry, or the
# stash, or if it's descendend from a dangling commit.
#
# TODO: Add support for searching for blobs and trees as well.
#
# NOTE: This script can be very slow.
classGitObject
attr_reader:ident
definitialize(ident)
@ident=ident
end
end
classBlob < GitObject
attr_reader:names
definitialize(ident)
superident
@names=[]
end
defadd_name(name)
@names.push(name)
end
end
classTree < GitObject
attr_reader:names
definitialize(ident)
superident
@names=[]
end
defadd_name(name)
@names.push(name)
end
end
classCommit < GitObject
attr_reader:names
attr_reader:parents
attr_reader:children
definitialize(ident, *parents)
superident
@names=[]
@parents=parents
@children=[]
end
defadd_name(name)
@names.push(name)
end
defadd_parent(parent)
unless@parents.include?(parent)
@parents.push(parent)
parent.add_child(self)
end
end
defadd_child(child)
unless@children.include?(child)
@children.push(child)
end
end
defassign_name(name)
if@names.include?(name)
return
end
add_name(name)
offset=0
@parents.eachdo |parent|
ifoffset == 0
ifname =~ /(.+?)~([0-9]+)$/
child_name= $1 + "~" + ($2.to_i + 1).to_s
else
child_name=name + "~1"
end
else
child_name=name + "^" + (offset + 1).to_s
end
parent.assign_name(child_name)
offset += 1
end
end
end
classTag < GitObject
attr_reader:name
attr_reader:commit
definitialize(ident,name,commit)
superident
@name=name
@commit=commit
end
end
classRef
attr_reader:name
attr_reader:commit
definitialize(name,commit)
@name=name
@commit=commit
end
end
$objects ={}
# Build up a full commit history for every ref, and each reflog entry for each
# ref.
defget_ref_history(ref)
first_obj=true
`git rev-list --parents #{ref}`.split("\n").eachdo |entry|
commit, *parents=entry.split(' ')
if $objects.has_key?(commit)
obj= $objects[commit]
else
obj=Commit.new(commit)
$objects[commit]=obj
end
iffirst_objandref =~ /\/tags\//
id=`git rev-parse #{ref}`.chomp
$objects[id]=Tag.new(id,ref,obj)
first_obj=false
end
ifobj.parents.size == 0
parents.eachdo |parent|
if $objects.has_key?(parent)
parent_obj= $objects[parent]
else
parent_obj=Commit.new(parent)
$objects[parent]=parent_obj
end
obj.add_parent(parent_obj)
end
end
end
end
defprocess_names(ref,name=nil,hash=nil)
# Assign symbolic names to each commit
if not hash
hash=`git rev-parse #{ref}`.chomp
end
raise"Cannot locate object #{ref} => #{hash}"if not $objects.has_key?(hash)
obj= $objects[hash]
ifobj.is_a?Tag
obj.commit.assign_name(name ? name : ref)
else
obj.assign_name(name ? name : ref)
end
end
$refs =`git rev-parse --symbolic --all`.split
$refs.push("HEAD")
puts"Processing refs history ..."
$refs.each{ |ref| get_ref_history(ref)}
$refs.each{ |ref| process_names(ref)}
$hashes =ARGV[0].map{ |ref| `git rev-parse #{ref}`.chomp}
$search_reflogs =false
$hashes.eachdo |hash|
unless $objects.has_key?(hash)
$search_reflogs =true
end
end
if $search_reflogs
puts"Processing reflogs ..."
$refs.eachdo |ref|
unlessref == "refs/stash"
# Handle each reflog entry
`git reflog show --abbrev=40 #{ref}`.split("\n").eachdo |line|
ifline =~ /^([0-9a-f]{40}) ([^@]+@\{[0-9]+\}): /
hash= $1
unless $objects.has_key?(hash)
puts" unreachable reflog #{$2}"
get_ref_history $2
process_names $2,nil,hash
end
end
end
end
end
end
$search_dangling =false
$hashes.eachdo |hash|
unless $objects.has_key?(hash)
$search_dangling =true
end
end
if $search_dangling
puts"Finding unreachable objects ..."
`git fsck --full --unreachable`.split("\n").eachdo |item|
ifitem =~ /unreachable (blob|tag|commit|tree) ([a-f0-9]{40})/
puts" " + item
if $1 == "blob"
$objects[$2]=Blob.new($2)
elsif $1 == "tag"
commit=Commit.new(`git rev-list -1 #{$2}`.chomp)
$objects[$2]=Tag.new($2,"<dangling>",commit)
get_ref_history $2
process_names $2,"dangling-tag-#{$2}"
elsif $1 == "commit"
get_ref_history $2
process_names $2,"dangling-commit-#{$2}"
elsif $1 == "tree"
$objects[$2]=Tree.new($2)
end
end
end
end
ARGV[0].eachdo |ref|
hash=`git rev-parse #{ref}`.chomp
object= $objects[hash]
puts""
ifobject.is_a?Tag
puts"Tag #{ref} => #{hash}"
object=object.commit
puts" commit #{object.ident}"
else
puts"Commit #{ref} => #{hash}"
end
object.parents.each{ |parent| puts" parent #{parent.ident}"}
object.children.each{ |child| puts" child #{child.ident}"}
object.names.each{ |name| puts" name #{name}"}
end