GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of jwiegley/git-issues
Description: A distributed issue tracking system based on Git repositories, written in Python
Homepage: http://www.newartisans.com/software.html
Clone URL: git://github.com/ktf/git-issues.git
git-issues / gitshelve.py
100755 516 lines (417 sloc) 16.184 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env python
# coding: utf-8
 
# gitshelve.py, version 0.1
#
# by John Wiegley <johnw@newartisans.com>
#
# This file implements a Python shelve object that uses a branch within the
# current Git repository to store its data, plus a history of all changes to
# that data. The usage is identical to shelve, with the exception that a
# repository directory or branch name must be specified, and that
# writeback=True is assumed.
#
# Example:
#
# import gitshelve
#
# data = gitshelve.open(branch = 'mydata', repository = '/tmp/foo')
# data = gitshelve.open(branch = 'mydata') # use current repo
#
# data['foo/bar/git.c'] = "This is some sample data."
#
# data.commit("Changes")
# data.sync() # same as data.commit()
#
# print data['foo/bar/git.c']
#
# data.close()
#
# If you checkout the 'mydata' branch now, you'll see the file 'git.c' in the
# directory 'foo/bar'. Running 'git log' will show the change you made.
 
import re
import os
 
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
 
from subprocess import Popen, PIPE
from string import split, join
 
######################################################################
 
verbose = False
 
######################################################################
 
# Utility function for calling out to Git (this script does not try to
# be a Git library, just an interface to the underlying commands). It
# supports a 'restart' keyword, which will cause a Python function to
# be called on failure. If that function returns True, the same
# command will be attempted again. This can avoid costly checks to
# make sure a branch exists, for example, by simply failing on the
# first attempt to use it and then allowing the restart function to
# create it.
 
class GitError(Exception):
    def __init__(self, cmd, args, kwargs, stderr = None):
        self.cmd = cmd
        self.args = args
        self.kwargs = kwargs
        self.stderr = stderr
        Exception.__init__(self)
 
    def __str__(self):
        if self.stderr:
            return "Git command failed: git %s %s: %s" % \
                (self.cmd, self.args, self.stderr)
        else:
            return "Git command failed: git %s %s" % (self.cmd, self.args)
 
def git(cmd, *args, **kwargs):
    restart = True
    while restart:
        stdin_mode = None
        if kwargs.has_key('input'):
            stdin_mode = PIPE
 
        if verbose:
            print "Command: git %s %s" % (cmd, join(args, ' '))
            if kwargs.has_key('input'):
                print "Input: <<EOF"
                print kwargs['input'],
                print "EOF"
 
        environ = None
        if kwargs.has_key('repository'):
            environ = os.environ.copy()
            environ['GIT_DIR'] = kwargs['repository']
 
            git_dir = environ['GIT_DIR']
            if not os.path.isdir(git_dir):
                proc = Popen(('git', 'init'), env = environ,
                             stdout = PIPE, stderr = PIPE)
                if proc.wait() != 0:
                    raise GitError('init', [], {}, proc.stderr.read())
 
        proc = Popen(('git', cmd) + args, env = environ,
                     stdin = stdin_mode,
                     stdout = PIPE,
                     stderr = PIPE)
 
        if kwargs.has_key('input'):
            proc.stdin.write(kwargs['input'])
            proc.stdin.close()
 
        returncode = proc.wait()
        restart = False
        if returncode != 0:
            if kwargs.has_key('restart'):
                if kwargs['restart'](cmd, args, kwargs):
                    restart = True
            else:
                raise GitError(cmd, args, kwargs, proc.stderr.read())
 
    if not kwargs.has_key('ignore_output'):
        if kwargs.has_key('keep_newline'):
            return proc.stdout.read()
        else:
            return proc.stdout.read()[:-1]
 
 
class gitbook:
    """Abstracts a reference to a data file within a Git repository. It also
maintains knowledge of whether the object has been modified or not."""
    def __init__(self, shelf, path, name = None):
        self.shelf = shelf
        self.path = path
        self.name = name
        self.data = None
        self.dirty = False
 
    def get_data(self):
        if self.data is None:
            assert self.name is not None
            self.data = self.deserialize_data(self.shelf.get_blob(self.name))
        return self.data
        
    def set_data(self, data):
        if data != self.data:
            self.name = None
            self.data = data
            self.dirty = True
 
    def serialize_data(self, data):
        return data
 
    def deserialize_data(self, data):
        return data
 
    def change_comment(self):
        return None
 
    def __getstate__(self):
        odict = self.__dict__.copy() # copy the dict since we change it
        del odict['dirty'] # remove dirty flag
        return odict
 
    def __setstate__(self, ndict):
        self.__dict__.update(ndict) # update attributes
        self.dirty = False
 
 
class gitshelve(dict):
    """This class implements a Python "shelf" using a branch within a Git
repository. There is no "writeback" argument, meaning changes are only
written upon calling close or sync.
 
This implementation uses a dictionary of gitbook objects, since we don't
really want to use Pickling within a Git repository (it's not friendly to
other Git users, nor does it support merging)."""
    ls_tree_pat = re.compile('((\d{6}) (tree|blob)) ([0-9a-f]{40})\t(start|(.+))$')
 
    head = None
    dirty = False
    objects = None
 
    def __init__(self, branch = 'master', repository = None,
                 keep_history = True, book_type = gitbook):
        self.branch = branch
        self.repository = repository
        self.keep_history = keep_history
        self.book_type = book_type
        self.init_data()
        dict.__init__(self)
 
    def init_data(self):
        self.head = None
        self.dirty = False
        self.objects = {}
 
    def git(self, *args, **kwargs):
        if self.repository:
            kwargs['repository'] = self.repository
        return apply(git, args, kwargs)
 
    def current_head(self):
        return self.git('rev-parse', self.branch)
 
    def update_head(self, new_head):
        if self.head:
            self.git('update-ref', 'refs/heads/%s' % self.branch, new_head,
                     self.head)
        else:
            self.git('update-ref', 'refs/heads/%s' % self.branch, new_head)
        self.head = new_head
 
    def read_repository(self):
        self.init_data()
        try:
            self.head = self.current_head()
        except:
            self.head = None
 
        if not self.head:
            return
 
        ls_tree = split(self.git('ls-tree', '-r', '-t', '-z', self.head),
                        '\0')
        for line in ls_tree:
            if not line:
                continue
            match = self.ls_tree_pat.match(line)
            assert match
 
            treep = match.group(1) == "040000 tree"
            perm = match.group(2)
            name = match.group(4)
            path = match.group(5)
 
            parts = split(path, os.sep)
            d = self.objects
            for part in parts:
                if not d.has_key(part):
                    d[part] = {}
                d = d[part]
 
            if treep:
                if perm == '040000' :
                    d['__root__'] = name
                else :
                    raise GitError('read_repository', [], {},
                                   'Invalid mode for %s : 040000 required, %s found' %(path, perm))
            else:
                if perm == '100644' :
                    d['__book__'] = self.book_type(self, path, name)
                else :
                    raise GitError('read_repository', [], {},
                                   'Invalid mode for %s : 100644 required, %s found' %(path, perm))
 
    def open(cls, branch = 'master', repository = None,
             keep_history = True, book_type = gitbook):
        shelf = gitshelve(branch, repository, keep_history, book_type)
        shelf.read_repository()
        return shelf
 
    open = classmethod(open)
 
    def get_blob(self, name):
        return self.git('cat-file', 'blob', name, keep_newline = True)
 
    def hash_blob(self, data):
        return self.git('hash-object', '--stdin', input = data)
 
    def make_blob(self, data):
        return self.git('hash-object', '-w', '--stdin', input = data)
 
    def make_tree(self, objects, comment_accumulator = None):
        buf = StringIO()
 
        root = None
        if objects.has_key('__root__'):
            root = objects['__root__']
 
        for path in objects.keys():
            if path == '__root__': continue
 
            obj = objects[path]
            assert isinstance(obj, dict)
 
            if len(obj.keys()) == 1 and obj.has_key('__book__'):
                book = obj['__book__']
                if book.dirty:
                    if comment_accumulator:
                        comment = book.change_comment()
                        if comment:
                            comment_accumulator.write(comment)
 
                    book.name = self.make_blob(book.serialize_data(book.data))
                    book.dirty = False
                    root = None
 
                buf.write("100644 blob %s\t%s\0" % (book.name, path))
 
            else:
                tree_root = None
                if obj.has_key('__root__'):
                    tree_root = obj['__root__']
 
                tree_name = self.make_tree(obj, comment_accumulator)
                if tree_name != tree_root:
                    root = None
 
                buf.write("040000 tree %s\t%s\0" % (tree_name, path))
 
        if root is None:
            name = self.git('mktree', '-z', input = buf.getvalue())
            objects['__root__'] = name
            return name
        else:
            return root
 
    def make_commit(self, tree_name, comment):
        if not comment: comment = ""
        if self.head and self.keep_history:
            name = self.git('commit-tree', tree_name, '-p', self.head,
                            input = comment)
        else:
            name = self.git('commit-tree', tree_name, input = comment)
 
        self.update_head(name)
        return name
 
    def commit(self, comment = None):
        if not self.dirty:
            return self.head
 
        accumulator = None
        if comment is None:
            accumulator = StringIO()
        
        # Walk the objects now, creating and nesting trees until we end up
        # with a top-level tree. We then create a commit out of this tree.
        tree = self.make_tree(self.objects, accumulator)
        if accumulator:
            comment = accumulator.getvalue()
        name = self.make_commit(tree, comment)
 
        self.dirty = False
        return name
 
    def sync(self):
        self.commit()
 
    def close(self):
        if self.dirty:
            self.sync()
        del self.objects # free it up right away
 
    def dump_objects(self, fd, indent = 0, objects = None):
        if objects is None:
            objects = self.objects
 
        if objects.has_key('__root__') and indent == 0:
            fd.write('%stree %s\n' % (" " * indent, objects['__root__']))
            indent += 2
 
        keys = objects.keys()
        keys.sort()
        for key in keys:
            if key == '__root__': continue
            assert isinstance(objects[key], dict)
 
            if objects[key].has_key('__book__'):
                book = objects[key]['__book__']
                if book.name:
                    kind = 'blob ' + book.name
                else:
                    kind = 'blob'
            else:
                if objects[key].has_key('__root__'):
                    kind = 'tree ' + objects[key]['__root__']
                else:
                    kind = 'tree'
 
            fd.write('%s%s: %s\n' % (" " * indent, kind, key))
 
            if kind[:4] == 'tree':
                self.dump_objects(fd, indent + 2, objects[key])
 
    def get_tree(self, path, make_dirs = False):
        parts = split(path, os.sep)
        d = self.objects
        for part in parts:
            if make_dirs and not d.has_key(part):
                d[part] = {}
            d = d[part]
        return d
 
    def get(self, key):
        path = '%s/%s' % (key[:2], key[2:])
        d = None
        try:
            d = self.get_tree(path)
        except KeyError:
            raise KeyError(key)
        if not d or not d.has_key('__book__'):
            raise KeyError(key)
        return d['__book__'].get_data()
 
    def put(self, data):
        book = self.book_type(self, '__unknown__')
        book.data = data
        book.name = self.make_blob(book.serialize_data(book.data))
        book.dirty = False # the blob was just written!
        book.path = '%s/%s' % (book.name[:2], book.name[2:])
 
        d = self.get_tree(book.path, make_dirs = True)
        d.clear()
        d['__book__'] = book
        self.dirty = True
 
        return book.name
 
    def __getitem__(self, path):
        d = None
        try:
            d = self.get_tree(path)
        except KeyError:
            raise KeyError(path)
 
        if d and d.has_key('__book__'):
            return d['__book__'].get_data()
        else:
            raise KeyError(path)
 
    def __setitem__(self, path, data):
        d = self.get_tree(path, make_dirs = True)
        if not d.has_key('__book__'):
            d.clear()
            d['__book__'] = self.book_type(self, path)
        d['__book__'].set_data(data)
        self.dirty = True
 
    def prune_tree(self, objects, paths):
        if len(paths) > 1:
            self.prune_tree(objects[paths[0]], paths[1:])
        del objects[paths[0]]
 
    def __delitem__(self, path):
        try:
            self.prune_tree(self.objects, split(path, os.sep))
        except KeyError:
            raise KeyError(path)
 
    def __contains__(self, path):
        d = self.get_tree(path)
        return len(d.keys()) == 1 and d.has_key('__book__')
 
    def walker(self, kind, objects, path = ''):
        for item in objects.items():
            if item[0] == '__root__': continue
            assert isinstance(item[1], dict)
 
            if path:
                key = join((path, item[0]), os.sep)
            else:
                key