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
73 return self.commit.data.is_nochange()
75 class PatchOrder(object):
76 """Keeps track of patch order, and which patches are applied.
77 Works with patch names, not actual patches."""
78 __list_order = [ 'applied', 'unapplied' ]
79 def __init__(self, stack):
82 def __read_file(self, fn):
83 return tuple(utils.read_strings(
84 os.path.join(self.__stack.directory, fn)))
85 def __write_file(self, fn, val):
86 utils.write_strings(os.path.join(self.__stack.directory, fn), val)
87 def __get_list(self, name):
88 if not name in self.__lists:
89 self.__lists[name] = self.__read_file(name)
90 return self.__lists[name]
91 def __set_list(self, name, val):
93 if val != self.__lists.get(name, None):
94 self.__lists[name] = val
95 self.__write_file(name, val)
96 applied = property(lambda self: self.__get_list('applied'),
97 lambda self, val: self.__set_list('applied', val))
98 unapplied = property(lambda self: self.__get_list('unapplied'),
99 lambda self, val: self.__set_list('unapplied', val))
101 class Patches(object):
102 """Creates Patch objects."""
103 def __init__(self, stack):
105 def create_patch(name):
106 p = Patch(self.__stack, name)
107 p.commit # raise exception if the patch doesn't exist
109 self.__patches = git.ObjectCache(create_patch) # name -> Patch
110 def exists(self, name):
117 return self.__patches[name]
118 def new(self, name, commit, msg):
119 assert not name in self.__patches
120 p = Patch(self.__stack, name)
121 p.set_commit(commit, msg)
122 self.__patches[name] = p
126 def __init__(self, repository, name):
127 self.__repository = repository
132 raise exception.StgException('%s: no such branch' % name)
133 self.__patchorder = PatchOrder(self)
134 self.__patches = Patches(self)
135 if not stackupgrade.update_to_current_format_version(repository, name):
136 raise exception.StgException('%s: branch not initialized' % 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]
158 def head_top_equal(self):
159 if not self.patchorder.applied:
161 return self.head == self.patches.get(self.patchorder.applied[-1]).commit
163 class Repository(git.Repository):
164 def __init__(self, *args, **kwargs):
165 git.Repository.__init__(self, *args, **kwargs)
166 self.__stacks = {} # name -> Stack
168 def current_branch(self):
169 return utils.strip_leading('refs/heads/', self.head)
171 def current_stack(self):
172 return self.get_stack(self.current_branch)
173 def get_stack(self, name):
174 if not name in self.__stacks:
175 self.__stacks[name] = Stack(self, name)
176 return self.__stacks[name]