contrib / fast-import / import-zips.pyon commit Merge branch 'maint' (c4a01a3)
   1#!/usr/bin/env python
   2
   3## zip archive frontend for git-fast-import
   4##
   5## For example:
   6##
   7##  mkdir project; cd project; git init
   8##  python import-zips.py *.zip
   9##  git log --stat import-zips
  10
  11from os import popen, path
  12from sys import argv, exit
  13from time import mktime
  14from zipfile import ZipFile
  15
  16if len(argv) < 2:
  17        print 'Usage:', argv[0], '<zipfile>...'
  18        exit(1)
  19
  20branch_ref = 'refs/heads/import-zips'
  21committer_name = 'Z Ip Creator'
  22committer_email = 'zip@example.com'
  23
  24fast_import = popen('git fast-import --quiet', 'w')
  25def printlines(list):
  26        for str in list:
  27                fast_import.write(str + "\n")
  28
  29for zipfile in argv[1:]:
  30        commit_time = 0
  31        next_mark = 1
  32        common_prefix = None
  33        mark = dict()
  34
  35        zip = ZipFile(zipfile, 'r')
  36        for name in zip.namelist():
  37                if name.endswith('/'):
  38                        continue
  39                info = zip.getinfo(name)
  40
  41                if commit_time < info.date_time:
  42                        commit_time = info.date_time
  43                if common_prefix == None:
  44                        common_prefix = name[:name.rfind('/') + 1]
  45                else:
  46                        while not name.startswith(common_prefix):
  47                                last_slash = common_prefix[:-1].rfind('/') + 1
  48                                common_prefix = common_prefix[:last_slash]
  49
  50                mark[name] = ':' + str(next_mark)
  51                next_mark += 1
  52
  53                printlines(('blob', 'mark ' + mark[name], \
  54                                        'data ' + str(info.file_size)))
  55                fast_import.write(zip.read(name) + "\n")
  56
  57        committer = committer_name + ' <' + committer_email + '> %d +0000' % \
  58                mktime(commit_time + (0, 0, 0))
  59
  60        printlines(('commit ' + branch_ref, 'committer ' + committer, \
  61                'data <<EOM', 'Imported from ' + zipfile + '.', 'EOM', \
  62                '', 'deleteall'))
  63
  64        for name in mark.keys():
  65                fast_import.write('M 100644 ' + mark[name] + ' ' +
  66                        name[len(common_prefix):] + "\n")
  67
  68        printlines(('',  'tag ' + path.basename(zipfile), \
  69                'from ' + branch_ref, 'tagger ' + committer, \
  70                'data <<EOM', 'Package ' + zipfile, 'EOM', ''))
  71
  72if fast_import.close():
  73        exit(1)