-
Notifications
You must be signed in to change notification settings - Fork 10
/
gitcatfile
executable file
·82 lines (71 loc) · 1.88 KB
/
gitcatfile
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
#!/usr/bin/env groovy
import java.util.zip.DataFormatException
import java.util.zip.Inflater
def cli = new CliBuilder(usage: 'gitcatfile [-e <PATH>]')
cli.with {
h longOpt: 'help', 'Show usage information'
e longOpt: 'extract', args: 1, argName: 'extract', 'Extracts the object in given path'
}
extract = ""
def options = cli.parse(args)
if (!options) {
return
}
if (options.h) {
cli.usage()
return
}
if (options.e) {
extract = options.extract
} else {
cli.usage()
return
}
if (extract) {
def file = new File(extract)
if (! file.exists()) {
println "Object file does not exist in ${file.absolutePath}"
return
}
decompress(file.bytes)
}
def decompress(byte[] data) throws IOException, DataFormatException {
Inflater inflater = new Inflater()
inflater.setInput(data)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length)
byte[] buffer = new byte[1024]
while (!inflater.finished()) {
int count = inflater.inflate(buffer)
outputStream.write(buffer, 0, count)
}
outputStream.close()
byte[] output = outputStream.toByteArray()
def normalizedOutput = []
def isHex = false
def hexCount = 0
def isHeader = true
output.each {
if (it == 0 && !isHex) {
normalizedOutput << "\\0 "
if (!isHeader) {
isHex = true
} else {
normalizedOutput << "\n"
}
isHeader = false
} else if (isHex & hexCount <= 20 ) {
normalizedOutput << String.format("%02x", it&0xff)
hexCount++
if (hexCount == 20) {
hexCount = 0
isHex = false
normalizedOutput << "\n"
}
} else {
normalizedOutput << new String(it)
}
}
normalizedOutput.each {
print it
}
}