Blame view

blimp.nim 16.6 KB
386557e6   Göran Krampe   Added support for...
1
  import md5, os, osproc, parseopt2, strutils, parsecfg, streams, lapp, subexes, tables
74272cdb   Göran Krampe   First commit
2
  
5fc367ad   Göran Krampe   Renamed to blimp,...
3
  # blimp is a little utility program for handling large files
74272cdb   Göran Krampe   First commit
4
  # in git repositories. Its inspired by git-fat and s3annex
aaf9bcef   Göran Krampe   Upped to version ...
5
6
  # but doesn't rely on S3 for storage - it uses rsync like git-fat.
  # It is a single binary without any dependencies.
74272cdb   Göran Krampe   First commit
7
  #
5fc367ad   Göran Krampe   Renamed to blimp,...
8
9
10
11
  # Manual use:
  #
  # Use "blimp d mybigfile" to deflate it before commit.
  # Use "blimp i mybigfile" to inflate it back to original size.
74272cdb   Göran Krampe   First commit
12
  #
aaf9bcef   Göran Krampe   Upped to version ...
13
  # When deflated the file only has:
386557e6   Göran Krampe   Added support for...
14
  #   "blimphash:" <filename> + <an md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
15
  # ...inside it.
74272cdb   Göran Krampe   First commit
16
  #
aaf9bcef   Göran Krampe   Upped to version ...
17
18
  # The file is copied over to a local dir:
  #  <blimpstore>/<filename>-<md5sum>
5fc367ad   Göran Krampe   Renamed to blimp,...
19
  #
dab0c5b0   Göran Krampe   Added --version, ...
20
  # Configuration is in these locations in order:
78569137   Göran Krampe   Added directory e...
21
  #
dab0c5b0   Göran Krampe   Added --version, ...
22
  #   ./.blimp.conf
5fc367ad   Göran Krampe   Renamed to blimp,...
23
  #   <gitroot>/.blimp.conf
aaf9bcef   Göran Krampe   Upped to version ...
24
  #   ~/<blimpstore>/.blimp.conf
dab0c5b0   Göran Krampe   Added --version, ...
25
  #   ~/.blimp.conf
aaf9bcef   Göran Krampe   Upped to version ...
26
27
28
29
  #
  # This way you can have settings per directory, per git clone,
  # per store and per user. A default blimpstore with a commented .blimp.conf
  # is created in ~/blimpstore if you run blimp and no .blimp.conf is found.
dab0c5b0   Göran Krampe   Added --version, ...
30
31
32
  
  const 
    versionMajor* = 0
386557e6   Göran Krampe   Added support for...
33
34
    versionMinor* = 3
    versionPatch* = 0
dab0c5b0   Göran Krampe   Added --version, ...
35
    versionAsString* = $versionMajor & "." & $versionMinor & "." & $versionPatch
74272cdb   Göran Krampe   First commit
36
  
b9ad52ff   Göran Krampe   Added default con...
37
  var
aaf9bcef   Göran Krampe   Upped to version ...
38
    blimpStore, remoteBlimpStore, uploadCommandFormat, downloadCommandFormat, deleteCommandFormat, rsyncPassword, blimpVersion: string = nil
78569137   Göran Krampe   Added directory e...
39
    homeDir, currentDir, gitRootDir: string
386557e6   Göran Krampe   Added support for...
40
    verbose, stdio, inflateAll: bool
aaf9bcef   Göran Krampe   Upped to version ...
41
    stdinContent: string = nil
74272cdb   Göran Krampe   First commit
42
  
b9ad52ff   Göran Krampe   Added default con...
43
44
45
  let
    defaultConfig = """
  [rsync]
aaf9bcef   Göran Krampe   Upped to version ...
46
  # Set your local blimpstore directory. You can use %home%, %cwd% and %gitroot% in paths, works cross platform.
78569137   Göran Krampe   Added directory e...
47
48
49
50
51
52
53
54
55
56
57
  # Example:
  #   # Place it inside the git clone
  #   blimpstore = "%gitroot%/.git/blimpstore"
  #   # Place it in current working directory (not very useful)
  #   blimpstore = "%cwd%/blimpstore"
  #
  # Default:
  #   # Place it in the users home directory
  #   blimpstore = "%home%/blimpstore"
  
  # Set this to your remote rsync location
dab0c5b0   Göran Krampe   Added --version, ...
58
  remote = "blimpuser@some-rsync-server.com::blimpstore"
aaf9bcef   Göran Krampe   Upped to version ...
59
  # Set this to your rsync password, it will be written out as a password-file called .blimp.pass on every rsync.
dab0c5b0   Göran Krampe   Added --version, ...
60
  password = "some-good-rsync-password-for-blimpuser"
e914743a   Göran Krampe   Added remove comm...
61
  
aaf9bcef   Göran Krampe   Upped to version ...
62
63
64
  # The following three formats should not need editing.
  # $1 is the blimp filename, $2 is remote location and $3 is the local blimpstore directory set above.
  # NOTE: The password-file .blimp.pass will be created by blimp on every command, do not remove that option.
dab0c5b0   Göran Krampe   Added --version, ...
65
66
  upload = "rsync --password-file $3/.blimp.pass -avzP $3/$1 $2/"
  download = "rsync --password-file $3/.blimp.pass -avzP $2/$1 $3/"
78569137   Göran Krampe   Added directory e...
67
  # This deletes a single file from destination, that is already deleted in source. Yeah... insane! But it works.
dab0c5b0   Göran Krampe   Added --version, ...
68
  delete = "rsync --password-file $3/.blimp.pass -dv --delete --existing --ignore-existing --include '$1' --exclude '*' $3/ $2"
aaf9bcef   Göran Krampe   Upped to version ...
69
70
71
72
  
  [blimp]
  # Minimal version, otherwise stop
  # version = 0.2 
b9ad52ff   Göran Krampe   Added default con...
73
  """
74272cdb   Göran Krampe   First commit
74
  
386557e6   Göran Krampe   Added support for...
75
76
77
    
  
  proc cmd(cmd: string): string =
78569137   Göran Krampe   Added directory e...
78
    try:
386557e6   Göran Krampe   Added support for...
79
      let tup = execCmdEx(cmd)
78569137   Göran Krampe   Added directory e...
80
81
82
83
84
85
86
      if tup[1] == 0:
        result = strip(tup[0])
      else:
        result = nil
    except:
      result = nil
  
386557e6   Göran Krampe   Added support for...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  # Find git root dir or nil
  proc gitRoot(): string =
    cmd("git rev-parse --show-toplevel")
  
  # Git config
  proc gitConfigSet(key, val: string) =
    discard cmd("git config " & $key & " " & $val)
  
  proc gitConfigGet(key: string): string =
    cmd("git config --get " & $key)
  
  # Set blimp filter
  proc setBlimpFilter() =
    gitConfigSet("filter.blimp.clean", "\"blimp -s d %f\"")
    gitConfigSet("filter.blimp.smudge", "\"blimp -s i %f\"")
  
  
  # Ensure the filter is set
  proc ensureBlimpFilter() =
    if gitConfigGet("filter.blimp.clean") != "":
      setBlimpFilter()
  
  
78569137   Göran Krampe   Added directory e...
110
111
112
113
114
115
116
117
  # Simple expansion of %home%, %cwd% and %gitroot%
  proc expandDirs(templ: string): string =
    result = templ.replace("%home%", homeDir)
    result = result.replace("%cwd%", currentDir)
    if result.contains("%gitroot%"):
      if gitRootDir.isNil: quit("Not in a git clone, can not expand %gitroot% in '" & templ & "'") 
      result = result.replace("%gitroot%", gitRootDir)
  
dab0c5b0   Göran Krampe   Added --version, ...
118
  # Load a blimp.conf file
74272cdb   Göran Krampe   First commit
119
  proc parseConfFile(filename: string) =
74272cdb   Göran Krampe   First commit
120
121
    var f = newFileStream(filename, fmRead)
    if f != nil:
dab0c5b0   Göran Krampe   Added --version, ...
122
      if verbose: echo "Reading config: " & filename
386557e6   Göran Krampe   Added support for...
123
      var p: CfgParser  
74272cdb   Göran Krampe   First commit
124
125
126
127
128
129
130
131
132
      open(p, f, filename)
      while true:
        var e = next(p)
        case e.kind
        of cfgEof: 
          break
        of cfgSectionStart:
          continue # Ignore
        of cfgKeyValuePair:
b9ad52ff   Göran Krampe   Added default con...
133
          case e.key
dab0c5b0   Göran Krampe   Added --version, ...
134
          of "blimpstore":
78569137   Göran Krampe   Added directory e...
135
            if blimpStore.isNil: blimpStore = expandDirs(e.value)
b9ad52ff   Göran Krampe   Added default con...
136
          of "remote":
78569137   Göran Krampe   Added directory e...
137
            if remoteBlimpStore.isNil: remoteBlimpStore = expandDirs(e.value)
dab0c5b0   Göran Krampe   Added --version, ...
138
139
          of "password":
            if rsyncPassword.isNil: rsyncPassword = e.value
b9ad52ff   Göran Krampe   Added default con...
140
          of "upload":
78569137   Göran Krampe   Added directory e...
141
            if uploadCommandFormat.isNil: uploadCommandFormat = expandDirs(e.value)
b9ad52ff   Göran Krampe   Added default con...
142
          of "download":
78569137   Göran Krampe   Added directory e...
143
            if downloadCommandFormat.isNil: downloadCommandFormat = expandDirs(e.value)
e914743a   Göran Krampe   Added remove comm...
144
          of "delete":
78569137   Göran Krampe   Added directory e...
145
            if deleteCommandFormat.isNil: deleteCommandFormat = expandDirs(e.value)
aaf9bcef   Göran Krampe   Upped to version ...
146
147
          of "version":
            if blimpVersion.isNil: blimpVersion = e.value
74272cdb   Göran Krampe   First commit
148
149
150
151
152
153
154
155
          else:
            quit("Unknown configuration: " & e.key)
        of cfgOption:
          quit("Unknown configuration: " & e.key)
        of cfgError:
          quit("Parsing " & filename & ": " & e.msg)
      close(p)
  
db95b901   Göran Krampe   Various fixes, mo...
156
157
158
159
  # Trivial helper to enable verbose
  proc run(cmd: string): auto =
    if verbose: echo(cmd)
    execCmd(cmd)
74272cdb   Göran Krampe   First commit
160
  
dab0c5b0   Göran Krampe   Added --version, ...
161
162
  # Every rsync command, make sure we have a password file
  proc rsyncRun(cmd: string): auto =
78569137   Göran Krampe   Added directory e...
163
164
165
166
    if not rsyncPassword.isNil:
      writeFile(blimpStore / ".blimp.pass", rsyncPassword)
      if execCmd("chmod 600 " & blimpStore / ".blimp.pass") != 0:
        quit("Failed to chmod 600 " & blimpStore / ".blimp.pass")
dab0c5b0   Göran Krampe   Added --version, ...
167
168
    run(cmd)
  
5fc367ad   Göran Krampe   Renamed to blimp,...
169
170
171
  # Upload a file to the remote master blimpStore
  proc uploadFile(blimpFilename: string) =
    if remoteBlimpStore.isNil:
e914743a   Göran Krampe   Added remove comm...
172
      echo("Remote blimpstore not set in configuration file, skipping uploading content:\n\t" & blimpFilename)
74272cdb   Göran Krampe   First commit
173
      return
dab0c5b0   Göran Krampe   Added --version, ...
174
    let errorCode = rsyncRun(format(uploadCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
b9ad52ff   Göran Krampe   Added default con...
175
176
    if errorCode != 0:
      quit("Something went wrong uploading " & blimpFilename & " to " & remoteBlimpStore, 2)
74272cdb   Göran Krampe   First commit
177
    
5fc367ad   Göran Krampe   Renamed to blimp,...
178
179
180
181
  # Download a file to the remote master blimpStore
  proc downloadFile(blimpFilename: string) =
    if remoteBlimpStore.isNil:
      quit("Remote blimpstore not set in configuration file, can not download content:\n\t" & blimpFilename)
dab0c5b0   Göran Krampe   Added --version, ...
182
    let errorCode = rsyncRun(format(downloadCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
b9ad52ff   Göran Krampe   Added default con...
183
184
    if errorCode != 0:
      quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3)
5fc367ad   Göran Krampe   Renamed to blimp,...
185
  
e914743a   Göran Krampe   Added remove comm...
186
187
188
189
  # Delete a file from the remote master blimpStore
  proc remoteDeleteFile(blimpFilename: string) =
    if remoteBlimpStore.isNil:
      return
dab0c5b0   Göran Krampe   Added --version, ...
190
    let errorCode = rsyncRun(format(deleteCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
e914743a   Göran Krampe   Added remove comm...
191
192
    if errorCode != 0:
      quit("Something went wrong deleting " & blimpFilename & " from " & remoteBlimpStore, 3)
5fc367ad   Göran Krampe   Renamed to blimp,...
193
194
195
196
  
  # Copy content to blimpStore, no upload yet.
  proc copyToBlimpStore(filename, blimpFilename: string) =
    if not existsFile(blimpStore / blimpFilename):
aaf9bcef   Göran Krampe   Upped to version ...
197
198
199
200
201
202
203
204
      if stdio:
        try:
          writeFile(blimpStore / blimpFilename, stdinContent)
        except:
          quit("Failed writing file: " & blimpStore / blimpFilename & " from stdin", 1)
      else:
        copyFile(filename, blimpStore / blimpFilename)
        uploadFile(blimpFilename)
5fc367ad   Göran Krampe   Renamed to blimp,...
205
206
  
  # Copy content from blimpStore, and downloading first if needed
e914743a   Göran Krampe   Added remove comm...
207
  proc copyFromBlimpStore(blimpFilename, filename: string) =
5fc367ad   Göran Krampe   Renamed to blimp,...
208
209
    if not existsFile(blimpStore / blimpFilename):
      downloadFile(blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
210
211
212
213
214
215
216
217
    if stdio:
      try:
        var content = readFile(blimpStore / blimpFilename)
        write(stdout, content)
      except:
        quit("Failed reading file: " & blimpStore / blimpFilename & " to stdout", 1)
    else:
      copyFile(blimpStore / blimpFilename, filename)
74272cdb   Göran Krampe   First commit
218
  
e914743a   Göran Krampe   Added remove comm...
219
220
221
222
223
224
  # Delete from blimpStore and remote.
  proc deleteFromBlimpStore(blimpFilename, filename: string) =
    if existsFile(blimpStore / blimpFilename):
      removeFile(blimpStore / blimpFilename)
    remoteDeleteFile(blimpFilename)
  
aaf9bcef   Göran Krampe   Upped to version ...
225
  proc blimpFileNameFromString(line: string): string =
386557e6   Göran Krampe   Added support for...
226
227
    let hashline = split(strip(line), ':')
    if hashline[0] == "blimphash":
e914743a   Göran Krampe   Added remove comm...
228
229
230
231
      result = hashline[1]
    else:
      result = nil
  
aaf9bcef   Göran Krampe   Upped to version ...
232
233
234
235
236
237
238
239
240
241
  # Pick out blimpFilename (filename & "-" & hash)
  proc blimpFileName(filename: string): string =
    if stdio:
      blimpFileNameFromString(stdinContent)
    else:
      var hashfile: File
      if not open(hashfile, filename):
        quit("Failed opening file: " & filename, 4)
      blimpFileNameFromString(string(readLine(hashfile)))  
  
e914743a   Göran Krampe   Added remove comm...
242
243
  # Get hash and compute blimpFilename
  proc computeBlimpFilename(filename: string): string =
dab0c5b0   Göran Krampe   Added --version, ...
244
245
246
247
248
249
    var content: string
    try:
      content = readFile(filename)
    except:
      quit("Failed opening file: " & filename, 1)
    let hash = getMD5(content)
14d16aa7   Göran Krampe   Now the git filte...
250
    result = extractFilename(filename) & "-" & hash
e914743a   Göran Krampe   Added remove comm...
251
   
5fc367ad   Göran Krampe   Renamed to blimp,...
252
  # Copy original file to blimpStore and replace with hash stub in git.
74272cdb   Göran Krampe   First commit
253
  proc deflate(filename: string) =
dab0c5b0   Göran Krampe   Added --version, ...
254
255
256
257
258
259
260
    if verbose: echo "Deflating " & filename
    var blimpFilename = blimpFilename(filename)
    if not blimpFilename.isNil:
      echo("\t" & filename & " is already deflated, skipping.")
    else:
      blimpFilename = computeBlimpFilename(filename)
      copyToBlimpStore(filename, blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
261
      if stdio:
386557e6   Göran Krampe   Added support for...
262
        write(stdout, "blimphash:" & blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
263
      else:
386557e6   Göran Krampe   Added support for...
264
        writeFile(filename, "blimphash:" & blimpFilename)
dab0c5b0   Göran Krampe   Added --version, ...
265
      if verbose: echo("\t" & filename & " deflated.")
e914743a   Göran Krampe   Added remove comm...
266
  
386557e6   Göran Krampe   Added support for...
267
268
269
270
271
272
273
  # Iterator over all deflated files in the git clone
  iterator allDeflated() =
    let filenames = cmd("git ls-files " & gitRootDir).split('\l')
    for fn in filenames:
      if not blimpFilename(fn).isNil:
        yield fn
  
5fc367ad   Göran Krampe   Renamed to blimp,...
274
  # Parse out hash from hash stub and copy back original content from blimpStore.
74272cdb   Göran Krampe   First commit
275
  proc inflate(filename: string) =
dab0c5b0   Göran Krampe   Added --version, ...
276
    if verbose: echo "Inflating " & filename
e914743a   Göran Krampe   Added remove comm...
277
278
    let blimpFilename = blimpFilename(filename)
    if blimpFilename.isNil:
dab0c5b0   Göran Krampe   Added --version, ...
279
      echo("\t" & filename & " is not deflated, skipping.")
e914743a   Göran Krampe   Added remove comm...
280
281
    else:
      copyFromBlimpStore(blimpfilename, filename)
dab0c5b0   Göran Krampe   Added --version, ...
282
      if verbose: echo("\t" & filename & " inflated.")
e914743a   Göran Krampe   Added remove comm...
283
284
285
286
287
288
289
  
  # Inflates file first (if deflated) and then removes current content for it,
  # both locally and in remote.
  proc remove(filename: string) =
    var blimpFilename = blimpFilename(filename)
    if not blimpFilename.isNil:
      copyFromBlimpStore(blimpfilename, filename)
74272cdb   Göran Krampe   First commit
290
    else:
e914743a   Göran Krampe   Added remove comm...
291
292
293
      blimpFilename = computeBlimpFilename(filename)
    deleteFromBlimpStore(blimpfilename, filename)
    echo("\t" & filename & " content removed from blimpstore locally and remotely.")
5fc367ad   Göran Krampe   Renamed to blimp,...
294
  
dab0c5b0   Göran Krampe   Added --version, ...
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  
  proc setupBlimpStore() =
    try:
      if not existsDir(blimpStore):
        createDir(blimpStore)
    except:
      quit("Could not create " & blimpStore & " directory.", 1)
  
    try:
      if not existsFile(blimpStore / ".blimp.conf"):
        writeFile(blimpStore / ".blimp.conf", defaultConfig)
    except:
      quit("Could not create .blimp.conf config file in " & blimpStore & " directory.", 1)
  
78569137   Göran Krampe   Added directory e...
309
310
311
  proc `$`(x: string): string =
    if x.isNil: "nil" else: x
  
dab0c5b0   Göran Krampe   Added --version, ...
312
313
314
315
316
317
318
  proc dumpConfig() =
    echo "\nDump of configuration:"
    echo "\tblimpStore: " & blimpStore
    echo "\tremoteBlimpStore: " & remoteBlimpStore
    echo "\tuploadCommandFormat: " & uploadCommandFormat
    echo "\tdownloadCommandFormat: " & downloadCommandFormat
    echo "\tdeleteCommandFormat: " & deleteCommandFormat
78569137   Göran Krampe   Added directory e...
319
    echo "\trsyncPassword: " & $rsyncPassword
aaf9bcef   Göran Krampe   Upped to version ...
320
    echo "\tblimpVersion: " & $blimpVersion
dab0c5b0   Göran Krampe   Added --version, ...
321
    echo "\n"
74272cdb   Göran Krampe   First commit
322
  
386557e6   Göran Krampe   Added support for...
323
  let synopsis = """
db95b901   Göran Krampe   Various fixes, mo...
324
    blimp [options] <command> <filenames...>
dab0c5b0   Göran Krampe   Added --version, ...
325
326
      -h,--help                Show this
      --version                Show version of blimp
aaf9bcef   Göran Krampe   Upped to version ...
327
      -v,--verbose             Verbosity, only works without -s
386557e6   Göran Krampe   Added support for...
328
329
      -i,--init                Set blimp filter in git config
      ----------
dab0c5b0   Göran Krampe   Added --version, ...
330
      <command>   (string)     (d)eflate, (i)nflate, remove
386557e6   Göran Krampe   Added support for...
331
332
333
334
335
336
      -a,--all                 Operate on all deflated files in clone
      ----------
      -s,--stdio               If given, use stdin/stdout for content.
      <filenames> (string...)  One or more filepaths to inflate/deflate
  """
  let help = """
aaf9bcef   Göran Krampe   Upped to version ...
337
338
339
    blimp is a little utility program for handling large files
    in git repositories. Its inspired by git-fat and s3annex
    but doesn't rely on S3 for storage - it uses rsync like git-fat.
386557e6   Göran Krampe   Added support for...
340
341
    It is a single binary without any dependencies. Its not as advanced
    as git-fat but basically does the same thing.
aaf9bcef   Göran Krampe   Upped to version ...
342
343
344
  
    Manual use:
  
386557e6   Göran Krampe   Added support for...
345
346
    Use "blimp d mybigfile" to deflate a file, typically before commit.
    Use "blimp i mybigfile" to inflate it back to original content.
aaf9bcef   Göran Krampe   Upped to version ...
347
348
  
    When deflated the file only has:
386557e6   Göran Krampe   Added support for...
349
      "blimphash:" <filename> "-" <md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
350
351
    ...inside it.
  
386557e6   Göran Krampe   Added support for...
352
    Deflate also copies the real content to your local blimpstore:
aaf9bcef   Göran Krampe   Upped to version ...
353
    
386557e6   Göran Krampe   Added support for...
354
      <blimpstore>/<filename>-<md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
355
  
386557e6   Göran Krampe   Added support for...
356
    ...and if configured also uploads it to "remote", using rsync.
aaf9bcef   Göran Krampe   Upped to version ...
357
358
359
360
  
    Configuration is in these locations in order:
   
      ./.blimp.conf
386557e6   Göran Krampe   Added support for...
361
      <gitroot>/.blimp.conf
aaf9bcef   Göran Krampe   Upped to version ...
362
363
364
365
366
367
368
369
370
371
372
373
      ~/<blimpstore>/.blimp.conf
      ~/.blimp.conf
  
    This way you can have settings per directory, per git clone,
    per store and per user. A default blimpstore with a commented .blimp.conf
    is created in ~/blimpstore if you run blimp and no .blimp.conf is found.
  
    Edit ~/blimpstore/.blimp.conf (or in another location) and set a proper
    remote and the proper rsync password. This ensures that its also properly
    synced with a master rsync repository that is typically shared.
  
    Inflate will bring back the original content by copying from
386557e6   Göran Krampe   Added support for...
374
    your local blimpstore, and if its not there, first download from the remote.
aaf9bcef   Göran Krampe   Upped to version ...
375
376
377
378
379
    Use this whenever you need to work/edit the big file - in order to get
    its real content.
  
    The filenames given are all processed. If -s is used content is processed via
    stdin/stdout and only one filename can be passed. This is used when running blimp
386557e6   Göran Krampe   Added support for...
380
    via a git filter (smudge/clean), see below.
aaf9bcef   Göran Krampe   Upped to version ...
381
382
383
384
385
  
    The remove command (no single character shortcut) will remove the file(s) content
    both from the local blimpstore and from the remote. This only removes
    the current content version, not older versions. The file itself is first
    inflated, if needed, and not deleted. This only "unblimps" the file.
386557e6   Göran Krampe   Added support for...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
    
    In order to have blimp work automatically you can:
    
    * Create a .gitattributes file with lines like:
      *.png filter=blimp -text
      
    * Configure blimp as a filter by running:
      git config filter.blimp.clean "blimp -s d %f"
      git config filter.blimp.smudge "blimp -s i %f"
  
    The above git config can be done by running "blimp --init".
  
    When the above is done (per clone) git will automatically run blimp deflate
    just before committing and blimp inflate when operations are done.
    
    This means that if you clone a git repository that already has a .gitattributes
    file in it that uses the blimp filter, then you should do:
    
      blimp --init inflate --all
    
    This will configure the blimp filter and also find and inflate all deflated
    files throughout the clone.
aaf9bcef   Göran Krampe   Upped to version ...
408
  """
db95b901   Göran Krampe   Various fixes, mo...
409
  
74272cdb   Göran Krampe   First commit
410
  ################################ main #####################################
78569137   Göran Krampe   Added directory e...
411
412
413
414
415
  # Set some dirs
  homeDir = getHomeDir()
  homeDir = homeDir[0.. -2] # Not sure why it keeps a trailing "/" on Linux
  currentDir = getCurrentDir()
  gitRootDir = gitRoot()
74272cdb   Göran Krampe   First commit
416
  
dab0c5b0   Göran Krampe   Added --version, ...
417
  # Using lapp to get args, on parsing failure this will show usage automatically
386557e6   Göran Krampe   Added support for...
418
  var args = parse(synopsis)
dab0c5b0   Göran Krampe   Added --version, ...
419
  verbose = args["verbose"].asBool
aaf9bcef   Göran Krampe   Upped to version ...
420
  stdio = args["stdio"].asBool
386557e6   Göran Krampe   Added support for...
421
  inflateAll = args["all"].asBool
aaf9bcef   Göran Krampe   Upped to version ...
422
423
424
425
426
427
428
429
430
431
432
  
  # Can't do verbose with -s, that messes up stdout,
  # read in all of stdin once and for all
  if stdio:
    verbose = false
    try:
      stdinContent = readAll(stdin)
      close(stdin)
    except:
      quit("Failed reading stdin", 1)
  
dab0c5b0   Göran Krampe   Added --version, ...
433
434
  
  # Parse configuration files, may shadow and override each other
78569137   Göran Krampe   Added directory e...
435
  parseConfFile(currentDir / ".blimp.conf")
386557e6   Göran Krampe   Added support for...
436
  if not gitRootDir.isNil and gitRootDir != currentDir:
78569137   Göran Krampe   Added directory e...
437
    parseConfFile(gitRootDir / ".blimp.conf")
74272cdb   Göran Krampe   First commit
438
  
dab0c5b0   Göran Krampe   Added --version, ...
439
440
  # If we haven't gotten a blimpstore yet, we set a default one
  if blimpStore.isNil:
78569137   Göran Krampe   Added directory e...
441
    blimpStore = homeDir / "blimpstore"
74272cdb   Göran Krampe   First commit
442
  
dab0c5b0   Göran Krampe   Added --version, ...
443
444
  if existsDir(blimpStore):
    parseConfFile(blimpStore / ".blimp.conf")
78569137   Göran Krampe   Added directory e...
445
  parseConfFile(homeDir / ".blimp.conf")
74272cdb   Göran Krampe   First commit
446
  
386557e6   Göran Krampe   Added support for...
447
  # Let's just show what we have :)
dab0c5b0   Göran Krampe   Added --version, ...
448
  if verbose: dumpConfig()
e914743a   Göran Krampe   Added remove comm...
449
  
dab0c5b0   Göran Krampe   Added --version, ...
450
  # These two are special, they short out
386557e6   Göran Krampe   Added support for...
451
  if args.showHelp: quit(synopsis & help)
dab0c5b0   Göran Krampe   Added --version, ...
452
  if args.showVersion: quit("blimp version: " & versionAsString)
74272cdb   Göran Krampe   First commit
453
  
aaf9bcef   Göran Krampe   Upped to version ...
454
455
456
457
  # Check blimpVersion
  if not blimpVersion.isNil and blimpVersion != versionAsString:
    quit("Wrong version of blimp, configuration wants: " & blimpVersion)
  
386557e6   Göran Krampe   Added support for...
458
459
460
461
462
  # Should we install the filter?
  if args["init"].asBool:
    ensureBlimpFilter()
    if verbose: echo("Installed blimp filter")
  
db95b901   Göran Krampe   Various fixes, mo...
463
  let command = args["command"].asString
386557e6   Göran Krampe   Added support for...
464
465
466
  var filenames: seq[PValue]
  if args.hasKey("filenames"):
    filenames = args["filenames"].asSeq
dab0c5b0   Göran Krampe   Added --version, ...
467
468
469
470
  
  # Make sure the local blimpstore is setup.
  setupBlimpStore()
  
74272cdb   Göran Krampe   First commit
471
  # Do the deed
386557e6   Göran Krampe   Added support for...
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  if command != "":
    if command == "d" or command == "deflate":
      for fn in filenames:
        deflate(fn.asString)
    elif command == "i" or command == "inflate":
      if inflateAll:
        for fn in allDeflated():
          inflate(fn)
      else:
        for fn in filenames:
          inflate(fn.asString)
    elif command == "remove":
      for fn in filenames:
        remove(fn.asString)
    else:
      quit("Unknown command, only (d)eflate or (i)inflate are valid.", 6)
74272cdb   Göran Krampe   First commit
488
489
490
  
  # All good
  quit(0)