2 from stgit import exception, utils
3 from stgit.lib import git, stackupgrade
6 def __init__(self, stack, name):
9 name = property(lambda self: self.__name)
12 return 'refs/patches/%s/%s' % (self.__stack.name, self.__name)
15 return self.__ref + '.log'
18 return self.__stack.repository.refs.get(self.__ref)
20 def __compat_dir(self):
21 return os.path.join(self.__stack.directory, 'patches', self.__name)
22 def __write_compat_files(self, new_commit, msg):
23 """Write files used by the old infrastructure."""
24 def write(name, val, multiline = False):
25 fn = os.path.join(self.__compat_dir, name)
27 utils.write_string(fn, val, multiline)
28 elif os.path.isfile(fn):
32 old_log = [self.__stack.repository.refs.get(self.__log_ref)]
35 cd = git.Commitdata(tree = new_commit.data.tree, parents = old_log,
36 message = '%s\t%s' % (msg, new_commit.sha1))
37 c = self.__stack.repository.commit(cd)
38 self.__stack.repository.refs.set(self.__log_ref, c, msg)
41 write('authname', d.author.name)
42 write('authemail', d.author.email)
43 write('authdate', d.author.date)
44 write('commname', d.committer.name)
45 write('commemail', d.committer.email)
46 write('description', d.message)
47 write('log', write_patchlog().sha1)
48 write('top', new_commit.sha1)
49 write('bottom', d.parent.sha1)
51 old_top_sha1 = self.commit.sha1
52 old_bottom_sha1 = self.commit.data.parent.sha1
55 old_bottom_sha1 = None
56 write('top.old', old_top_sha1)
57 write('bottom.old', old_bottom_sha1)
58 def __delete_compat_files(self):
59 if os.path.isdir(self.__compat_dir):
60 for f in os.listdir(self.__compat_dir):
61 os.remove(os.path.join(self.__compat_dir, f))
62 os.rmdir(self.__compat_dir)
63 self.__stack.repository.refs.delete(self.__log_ref)
64 def set_commit(self, commit, msg):
65 self.__write_compat_files(commit, msg)
66 self.__stack.repository.refs.set(self.__ref, commit, msg)
68 self.__delete_compat_files()
69 self.__stack.repository.refs.delete(self.__ref)
71 return self.name in self.__stack.patchorder.applied
74 return c.data.tree == c.data.parent.data.tree
76 class PatchOrder(object):
77 """Keeps track of patch order, and which patches are applied.
78 Works with patch names, not actual patches."""
79 __list_order = [ 'applied', 'unapplied' ]
80 def __init__(self, stack):
83 def __read_file(self, fn):
84 return tuple(utils.read_strings(
85 os.path.join(self.__stack.directory, fn)))
86 def __write_file(self, fn, val):
87 utils.write_strings(os.path.join(self.__stack.directory, fn), val)
88 def __get_list(self, name):
89 if not name in self.__lists:
90 self.__lists[name] = self.__read_file(name)
91 return self.__lists[name]
92 def __set_list(self, name, val):
94 if val != self.__lists.get(name, None):
95 self.__lists[name] = val
96 self.__write_file(name, val)
97 applied = property(lambda self: self.__get_list('applied'),
98 lambda self, val: self.__set_list('applied', val))
99 unapplied = property(lambda self: self.__get_list('unapplied'),
100 lambda self, val: self.__set_list('unapplied', val))
102 class Patches(object):
103 """Creates Patch objects."""
104 def __init__(self, stack):
106 def create_patch(name):
107 p = Patch(self.__stack, name)
108 p.commit # raise exception if the patch doesn't exist
110 self.__patches = git.ObjectCache(create_patch) # name -> Patch
111 def exists(self, name):
118 return self.__patches[name]
119 def new(self, name, commit, msg):
120 assert not name in self.__patches
121 p = Patch(self.__stack, name)
122 p.set_commit(commit, msg)
123 self.__patches[name] = p
127 def __init__(self, repository, name):
128 self.__repository = repository
133 raise exception.StgException('%s: no such branch' % name)
134 self.__patchorder = PatchOrder(self)
135 self.__patches = Patches(self)
136 stackupgrade.update_to_current_format_version(repository, name)
137 name = property(lambda self: self.__name)
138 repository = property(lambda self: self.__repository)
139 patchorder = property(lambda self: self.__patchorder)
140 patches = property(lambda self: self.__patches)
143 return os.path.join(self.__repository.directory, 'patches', self.__name)
145 return 'refs/heads/%s' % self.__name
148 return self.__repository.refs.get(self.__ref())
149 def set_head(self, commit, msg):
150 self.__repository.refs.set(self.__ref(), commit, msg)
153 if self.patchorder.applied:
154 return self.patches.get(self.patchorder.applied[0]
159 class Repository(git.Repository):
160 def __init__(self, *args, **kwargs):
161 git.Repository.__init__(self, *args, **kwargs)
162 self.__stacks = {} # name -> Stack
164 def current_branch(self):
165 return utils.strip_leading('refs/heads/', self.head)
167 def current_stack(self):
168 return self.get_stack(self.current_branch)
169 def get_stack(self, name):
170 if not name in self.__stacks:
171 self.__stacks[name] = Stack(self, name)
172 return self.__stacks[name]