Blame view

blimp.nim 17.2 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
ec7d9613   Göran Krampe   Added option --fi...
40
    verbose, stdio, onAllDeflated, onAllFiltered: 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
  
  [blimp]
  # Minimal version, otherwise stop
ec7d9613   Göran Krampe   Added option --fi...
72
  # version = """ & versionAsString
74272cdb   Göran Krampe   First commit
73
  
386557e6   Göran Krampe   Added support for...
74
75
  
  proc cmd(cmd: string): string =
78569137   Göran Krampe   Added directory e...
76
    try:
386557e6   Göran Krampe   Added support for...
77
      let tup = execCmdEx(cmd)
78569137   Göran Krampe   Added directory e...
78
79
80
81
82
83
84
      if tup[1] == 0:
        result = strip(tup[0])
      else:
        result = nil
    except:
      result = nil
  
386557e6   Göran Krampe   Added support for...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  # 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...
108
109
110
111
112
113
114
115
  # 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, ...
116
  # Load a blimp.conf file
74272cdb   Göran Krampe   First commit
117
  proc parseConfFile(filename: string) =
74272cdb   Göran Krampe   First commit
118
119
    var f = newFileStream(filename, fmRead)
    if f != nil:
dab0c5b0   Göran Krampe   Added --version, ...
120
      if verbose: echo "Reading config: " & filename
386557e6   Göran Krampe   Added support for...
121
      var p: CfgParser  
74272cdb   Göran Krampe   First commit
122
123
124
125
126
127
128
129
130
      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...
131
          case e.key
dab0c5b0   Göran Krampe   Added --version, ...
132
          of "blimpstore":
78569137   Göran Krampe   Added directory e...
133
            if blimpStore.isNil: blimpStore = expandDirs(e.value)
b9ad52ff   Göran Krampe   Added default con...
134
          of "remote":
78569137   Göran Krampe   Added directory e...
135
            if remoteBlimpStore.isNil: remoteBlimpStore = expandDirs(e.value)
dab0c5b0   Göran Krampe   Added --version, ...
136
137
          of "password":
            if rsyncPassword.isNil: rsyncPassword = e.value
b9ad52ff   Göran Krampe   Added default con...
138
          of "upload":
78569137   Göran Krampe   Added directory e...
139
            if uploadCommandFormat.isNil: uploadCommandFormat = expandDirs(e.value)
b9ad52ff   Göran Krampe   Added default con...
140
          of "download":
78569137   Göran Krampe   Added directory e...
141
            if downloadCommandFormat.isNil: downloadCommandFormat = expandDirs(e.value)
e914743a   Göran Krampe   Added remove comm...
142
          of "delete":
78569137   Göran Krampe   Added directory e...
143
            if deleteCommandFormat.isNil: deleteCommandFormat = expandDirs(e.value)
aaf9bcef   Göran Krampe   Upped to version ...
144
145
          of "version":
            if blimpVersion.isNil: blimpVersion = e.value
74272cdb   Göran Krampe   First commit
146
147
148
149
150
151
152
153
          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...
154
155
156
157
  # Trivial helper to enable verbose
  proc run(cmd: string): auto =
    if verbose: echo(cmd)
    execCmd(cmd)
74272cdb   Göran Krampe   First commit
158
  
dab0c5b0   Göran Krampe   Added --version, ...
159
160
  # Every rsync command, make sure we have a password file
  proc rsyncRun(cmd: string): auto =
78569137   Göran Krampe   Added directory e...
161
162
163
164
    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, ...
165
166
    run(cmd)
  
5fc367ad   Göran Krampe   Renamed to blimp,...
167
168
169
  # Upload a file to the remote master blimpStore
  proc uploadFile(blimpFilename: string) =
    if remoteBlimpStore.isNil:
e914743a   Göran Krampe   Added remove comm...
170
      echo("Remote blimpstore not set in configuration file, skipping uploading content:\n\t" & blimpFilename)
74272cdb   Göran Krampe   First commit
171
      return
dab0c5b0   Göran Krampe   Added --version, ...
172
    let errorCode = rsyncRun(format(uploadCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
b9ad52ff   Göran Krampe   Added default con...
173
174
    if errorCode != 0:
      quit("Something went wrong uploading " & blimpFilename & " to " & remoteBlimpStore, 2)
74272cdb   Göran Krampe   First commit
175
    
5fc367ad   Göran Krampe   Renamed to blimp,...
176
177
178
179
  # 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, ...
180
    let errorCode = rsyncRun(format(downloadCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
b9ad52ff   Göran Krampe   Added default con...
181
182
    if errorCode != 0:
      quit("Something went wrong downloading " & blimpFilename & " from " & remoteBlimpStore, 3)
5fc367ad   Göran Krampe   Renamed to blimp,...
183
  
e914743a   Göran Krampe   Added remove comm...
184
185
186
187
  # Delete a file from the remote master blimpStore
  proc remoteDeleteFile(blimpFilename: string) =
    if remoteBlimpStore.isNil:
      return
dab0c5b0   Göran Krampe   Added --version, ...
188
    let errorCode = rsyncRun(format(deleteCommandFormat, blimpFilename, remoteBlimpStore, blimpStore))
e914743a   Göran Krampe   Added remove comm...
189
190
    if errorCode != 0:
      quit("Something went wrong deleting " & blimpFilename & " from " & remoteBlimpStore, 3)
5fc367ad   Göran Krampe   Renamed to blimp,...
191
192
193
194
  
  # Copy content to blimpStore, no upload yet.
  proc copyToBlimpStore(filename, blimpFilename: string) =
    if not existsFile(blimpStore / blimpFilename):
aaf9bcef   Göran Krampe   Upped to version ...
195
196
197
198
199
200
201
      if stdio:
        try:
          writeFile(blimpStore / blimpFilename, stdinContent)
        except:
          quit("Failed writing file: " & blimpStore / blimpFilename & " from stdin", 1)
      else:
        copyFile(filename, blimpStore / blimpFilename)
ec7d9613   Göran Krampe   Added option --fi...
202
      uploadFile(blimpFilename)
5fc367ad   Göran Krampe   Renamed to blimp,...
203
204
  
  # Copy content from blimpStore, and downloading first if needed
e914743a   Göran Krampe   Added remove comm...
205
  proc copyFromBlimpStore(blimpFilename, filename: string) =
5fc367ad   Göran Krampe   Renamed to blimp,...
206
207
    if not existsFile(blimpStore / blimpFilename):
      downloadFile(blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
208
209
210
211
212
213
214
215
    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
216
  
e914743a   Göran Krampe   Added remove comm...
217
218
219
220
221
222
  # 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 ...
223
  proc blimpFileNameFromString(line: string): string =
386557e6   Göran Krampe   Added support for...
224
225
    let hashline = split(strip(line), ':')
    if hashline[0] == "blimphash":
e914743a   Göran Krampe   Added remove comm...
226
227
228
229
      result = hashline[1]
    else:
      result = nil
  
aaf9bcef   Göran Krampe   Upped to version ...
230
231
232
233
234
235
236
237
238
239
  # 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...
240
241
  # Get hash and compute blimpFilename
  proc computeBlimpFilename(filename: string): string =
dab0c5b0   Göran Krampe   Added --version, ...
242
243
244
245
246
247
    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...
248
    result = extractFilename(filename) & "-" & hash
e914743a   Göran Krampe   Added remove comm...
249
   
5fc367ad   Göran Krampe   Renamed to blimp,...
250
  # Copy original file to blimpStore and replace with hash stub in git.
74272cdb   Göran Krampe   First commit
251
  proc deflate(filename: string) =
dab0c5b0   Göran Krampe   Added --version, ...
252
253
254
255
256
257
258
    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 ...
259
      if stdio:
386557e6   Göran Krampe   Added support for...
260
        write(stdout, "blimphash:" & blimpFilename)
aaf9bcef   Göran Krampe   Upped to version ...
261
      else:
386557e6   Göran Krampe   Added support for...
262
        writeFile(filename, "blimphash:" & blimpFilename)
dab0c5b0   Göran Krampe   Added --version, ...
263
      if verbose: echo("\t" & filename & " deflated.")
e914743a   Göran Krampe   Added remove comm...
264
  
386557e6   Göran Krampe   Added support for...
265
266
267
268
269
270
  # 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
ec7d9613   Göran Krampe   Added option --fi...
271
272
273
274
275
276
277
278
        
  # Iterator over all files matching the blimp filter in the git clone
  iterator allFiltered() =
    let lines = cmd("git ls-files | git check-attr --stdin filter").split('\l')
    for line in lines:
      let status = line.split(':')
      if strip(status[2]) == "blimp":
        yield status[0]
386557e6   Göran Krampe   Added support for...
279
  
5fc367ad   Göran Krampe   Renamed to blimp,...
280
  # Parse out hash from hash stub and copy back original content from blimpStore.
74272cdb   Göran Krampe   First commit
281
  proc inflate(filename: string) =
dab0c5b0   Göran Krampe   Added --version, ...
282
    if verbose: echo "Inflating " & filename
e914743a   Göran Krampe   Added remove comm...
283
284
    let blimpFilename = blimpFilename(filename)
    if blimpFilename.isNil:
dab0c5b0   Göran Krampe   Added --version, ...
285
      echo("\t" & filename & " is not deflated, skipping.")
e914743a   Göran Krampe   Added remove comm...
286
287
    else:
      copyFromBlimpStore(blimpfilename, filename)
dab0c5b0   Göran Krampe   Added --version, ...
288
      if verbose: echo("\t" & filename & " inflated.")
e914743a   Göran Krampe   Added remove comm...
289
290
291
292
293
294
295
  
  # 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
296
    else:
e914743a   Göran Krampe   Added remove comm...
297
298
299
      blimpFilename = computeBlimpFilename(filename)
    deleteFromBlimpStore(blimpfilename, filename)
    echo("\t" & filename & " content removed from blimpstore locally and remotely.")
5fc367ad   Göran Krampe   Renamed to blimp,...
300
  
dab0c5b0   Göran Krampe   Added --version, ...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  
  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...
315
316
317
  proc `$`(x: string): string =
    if x.isNil: "nil" else: x
  
dab0c5b0   Göran Krampe   Added --version, ...
318
319
320
321
322
323
324
  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...
325
    echo "\trsyncPassword: " & $rsyncPassword
aaf9bcef   Göran Krampe   Upped to version ...
326
    echo "\tblimpVersion: " & $blimpVersion
dab0c5b0   Göran Krampe   Added --version, ...
327
    echo "\n"
74272cdb   Göran Krampe   First commit
328
  
386557e6   Göran Krampe   Added support for...
329
  let synopsis = """
db95b901   Göran Krampe   Various fixes, mo...
330
    blimp [options] <command> <filenames...>
dab0c5b0   Göran Krampe   Added --version, ...
331
332
      -h,--help                Show this
      --version                Show version of blimp
aaf9bcef   Göran Krampe   Upped to version ...
333
      -v,--verbose             Verbosity, only works without -s
386557e6   Göran Krampe   Added support for...
334
335
      -i,--init                Set blimp filter in git config
      ----------
dab0c5b0   Göran Krampe   Added --version, ...
336
      <command>   (string)     (d)eflate, (i)nflate, remove
386557e6   Göran Krampe   Added support for...
337
      -a,--all                 Operate on all deflated files in clone
ec7d9613   Göran Krampe   Added option --fi...
338
      -f,--filter              Operate on all files matching blimp filter
386557e6   Göran Krampe   Added support for...
339
340
341
342
343
      ----------
      -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 ...
344
345
346
    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...
347
348
    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 ...
349
350
351
  
    Manual use:
  
386557e6   Göran Krampe   Added support for...
352
353
    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 ...
354
355
  
    When deflated the file only has:
386557e6   Göran Krampe   Added support for...
356
      "blimphash:" <filename> "-" <md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
357
358
    ...inside it.
  
386557e6   Göran Krampe   Added support for...
359
    Deflate also copies the real content to your local blimpstore:
aaf9bcef   Göran Krampe   Upped to version ...
360
    
386557e6   Göran Krampe   Added support for...
361
      <blimpstore>/<filename>-<md5sum>
aaf9bcef   Göran Krampe   Upped to version ...
362
  
386557e6   Göran Krampe   Added support for...
363
    ...and if configured also uploads it to "remote", using rsync.
aaf9bcef   Göran Krampe   Upped to version ...
364
365
366
367
  
    Configuration is in these locations in order:
   
      ./.blimp.conf
386557e6   Göran Krampe   Added support for...
368
      <gitroot>/.blimp.conf
aaf9bcef   Göran Krampe   Upped to version ...
369
370
371
372
373
374
375
376
377
378
379
380
      ~/<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...
381
    your local blimpstore, and if its not there, first download from the remote.
aaf9bcef   Göran Krampe   Upped to version ...
382
383
384
385
386
    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...
387
    via a git filter (smudge/clean), see below.
aaf9bcef   Göran Krampe   Upped to version ...
388
389
390
391
392
  
    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...
393
394
395
396
    
    In order to have blimp work automatically you can:
    
    * Create a .gitattributes file with lines like:
ec7d9613   Göran Krampe   Added option --fi...
397
      *.png filter=blimp binary
386557e6   Göran Krampe   Added support for...
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
      
    * 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 ...
415
  """
db95b901   Göran Krampe   Various fixes, mo...
416
  
74272cdb   Göran Krampe   First commit
417
  ################################ main #####################################
78569137   Göran Krampe   Added directory e...
418
419
420
421
422
  # 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
423
  
dab0c5b0   Göran Krampe   Added --version, ...
424
  # Using lapp to get args, on parsing failure this will show usage automatically
386557e6   Göran Krampe   Added support for...
425
  var args = parse(synopsis)
dab0c5b0   Göran Krampe   Added --version, ...
426
  verbose = args["verbose"].asBool
aaf9bcef   Göran Krampe   Upped to version ...
427
  stdio = args["stdio"].asBool
ec7d9613   Göran Krampe   Added option --fi...
428
429
  onAllDeflated = args["all"].asBool
  onAllFiltered = args["filter"].asBool
aaf9bcef   Göran Krampe   Upped to version ...
430
431
432
433
434
435
436
437
438
439
440
  
  # 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, ...
441
442
  
  # Parse configuration files, may shadow and override each other
78569137   Göran Krampe   Added directory e...
443
  parseConfFile(currentDir / ".blimp.conf")
386557e6   Göran Krampe   Added support for...
444
  if not gitRootDir.isNil and gitRootDir != currentDir:
78569137   Göran Krampe   Added directory e...
445
    parseConfFile(gitRootDir / ".blimp.conf")
74272cdb   Göran Krampe   First commit
446
  
dab0c5b0   Göran Krampe   Added --version, ...
447
448
  # If we haven't gotten a blimpstore yet, we set a default one
  if blimpStore.isNil:
78569137   Göran Krampe   Added directory e...
449
    blimpStore = homeDir / "blimpstore"
74272cdb   Göran Krampe   First commit
450
  
dab0c5b0   Göran Krampe   Added --version, ...
451
452
  if existsDir(blimpStore):
    parseConfFile(blimpStore / ".blimp.conf")
78569137   Göran Krampe   Added directory e...
453
  parseConfFile(homeDir / ".blimp.conf")
74272cdb   Göran Krampe   First commit
454
  
386557e6   Göran Krampe   Added support for...
455
  # Let's just show what we have :)
dab0c5b0   Göran Krampe   Added --version, ...
456
  if verbose: dumpConfig()
e914743a   Göran Krampe   Added remove comm...
457
  
dab0c5b0   Göran Krampe   Added --version, ...
458
  # These two are special, they short out
386557e6   Göran Krampe   Added support for...
459
  if args.showHelp: quit(synopsis & help)
dab0c5b0   Göran Krampe   Added --version, ...
460
  if args.showVersion: quit("blimp version: " & versionAsString)
74272cdb   Göran Krampe   First commit
461
  
aaf9bcef   Göran Krampe   Upped to version ...
462
463
464
465
  # Check blimpVersion
  if not blimpVersion.isNil and blimpVersion != versionAsString:
    quit("Wrong version of blimp, configuration wants: " & blimpVersion)
  
386557e6   Göran Krampe   Added support for...
466
467
468
469
470
  # Should we install the filter?
  if args["init"].asBool:
    ensureBlimpFilter()
    if verbose: echo("Installed blimp filter")
  
db95b901   Göran Krampe   Various fixes, mo...
471
  let command = args["command"].asString
386557e6   Göran Krampe   Added support for...
472
473
474
  var filenames: seq[PValue]
  if args.hasKey("filenames"):
    filenames = args["filenames"].asSeq
dab0c5b0   Göran Krampe   Added --version, ...
475
476
477
478
  
  # Make sure the local blimpstore is setup.
  setupBlimpStore()
  
74272cdb   Göran Krampe   First commit
479
  # Do the deed
386557e6   Göran Krampe   Added support for...
480
481
  if command != "":
    if command == "d" or command == "deflate":
ec7d9613   Göran Krampe   Added option --fi...
482
483
484
485
486
487
      if onAllFiltered:
        for fn in allFiltered():
          deflate(fn)
      else:
        for fn in filenames:
          deflate(fn.asString)
386557e6   Göran Krampe   Added support for...
488
    elif command == "i" or command == "inflate":
ec7d9613   Göran Krampe   Added option --fi...
489
      if onAllDeflated:
386557e6   Göran Krampe   Added support for...
490
491
        for fn in allDeflated():
          inflate(fn)
ec7d9613   Göran Krampe   Added option --fi...
492
493
494
      elif onAllFiltered:
        for fn in allFiltered():
          inflate(fn)
386557e6   Göran Krampe   Added support for...
495
496
497
498
499
500
501
      else:
        for fn in filenames:
          inflate(fn.asString)
    elif command == "remove":
      for fn in filenames:
        remove(fn.asString)
    else:
ec7d9613   Göran Krampe   Added option --fi...
502
      quit("Unknown command, only (d)eflate, (i)inflate or remove are valid.", 6)
74272cdb   Göran Krampe   First commit
503
504
505
  
  # All good
  quit(0)