1 """A Python class hierarchy wrapping the StGit on-disk metadata."""
4 from stgit import exception, utils
5 from stgit.lib import git, stackupgrade
8 """Represents an StGit patch. This class is mainly concerned with
9 reading and writing the on-disk representation of a patch."""
10 def __init__(self, stack, name):
13 name = property(lambda self: self.__name)
16 return 'refs/patches/%s/%s' % (self.__stack.name, self.__name)
19 return self.__ref + '.log'
22 return self.__stack.repository.refs.get(self.__ref)
24 def __compat_dir(self):
25 return os.path.join(self.__stack.directory, 'patches', self.__name)
26 def __write_compat_files(self, new_commit, msg):
27 """Write files used by the old infrastructure."""
28 def write(name, val, multiline = False):
29 fn = os.path.join(self.__compat_dir, name)
31 utils.write_string(fn, val, multiline)
32 elif os.path.isfile(fn):
36 old_log = [self.__stack.repository.refs.get(self.__log_ref)]
39 cd = git.CommitData(tree = new_commit.data.tree, parents = old_log,
40 message = '%s\t%s' % (msg, new_commit.sha1))
41 c = self.__stack.repository.commit(cd)
42 self.__stack.repository.refs.set(self.__log_ref, c, msg)
45 write('authname', d.author.name)
46 write('authemail', d.author.email)
47 write('authdate', d.author.date)
48 write('commname', d.committer.name)
49 write('commemail', d.committer.email)
50 write('description', d.message)
51 write('log', write_patchlog().sha1)
52 write('top', new_commit.sha1)
53 write('bottom', d.parent.sha1)
55 old_top_sha1 = self.commit.sha1
56 old_bottom_sha1 = self.commit.data.parent.sha1
59 old_bottom_sha1 = None
60 write('top.old', old_top_sha1)
61 write('bottom.old', old_bottom_sha1)
62 def __delete_compat_files(self):
63 if os.path.isdir(self.__compat_dir):
64 for f in os.listdir(self.__compat_dir):
65 os.remove(os.path.join(self.__compat_dir, f))
66 os.rmdir(self.__compat_dir)
67 self.__stack.repository.refs.delete(self.__log_ref)
68 def set_commit(self, commit, msg):
69 self.__write_compat_files(commit, msg)
70 self.__stack.repository.refs.set(self.__ref, commit, msg)
72 self.__delete_compat_files()
73 self.__stack.repository.refs.delete(self.__ref)
75 return self.name in self.__stack.patchorder.applied
77 return self.commit.data.is_nochange()
79 class PatchOrder(object):
80 """Keeps track of patch order, and which patches are applied.
81 Works with patch names, not actual patches."""
82 def __init__(self, stack):
85 def __read_file(self, fn):
86 return tuple(utils.read_strings(
87 os.path.join(self.__stack.directory, fn)))
88 def __write_file(self, fn, val):
89 utils.write_strings(os.path.join(self.__stack.directory, fn), val)
90 def __get_list(self, name):
91 if not name in self.__lists:
92 self.__lists[name] = self.__read_file(name)
93 return self.__lists[name]
94 def __set_list(self, name, val):
96 if val != self.__lists.get(name, None):
97 self.__lists[name] = val
98 self.__write_file(name, val)
99 applied = property(lambda self: self.__get_list('applied'),
100 lambda self, val: self.__set_list('applied', val))
101 unapplied = property(lambda self: self.__get_list('unapplied'),
102 lambda self, val: self.__set_list('unapplied', val))
103 hidden = property(lambda self: self.__get_list('hidden'),
104 lambda self, val: self.__set_list('hidden', val))
105 # don't return the hidden patches, these have to be returned explicitly
106 all = property(lambda self: self.applied + self.unapplied)
108 class Patches(object):
109 """Creates L{Patch} objects. Makes sure there is only one such object
111 def __init__(self, stack):
113 def create_patch(name):
114 p = Patch(self.__stack, name)
115 p.commit # raise exception if the patch doesn't exist
117 self.__patches = git.ObjectCache(create_patch) # name -> Patch
118 def exists(self, name):
125 return self.__patches[name]
126 def new(self, name, commit, msg):
127 assert not name in self.__patches
128 p = Patch(self.__stack, name)
129 p.set_commit(commit, msg)
130 self.__patches[name] = p
134 """Represents an StGit stack (that is, a git branch with some extra
136 def __init__(self, repository, name):
137 self.__repository = repository
142 raise exception.StgException('%s: no such branch' % name)
143 self.__patchorder = PatchOrder(self)
144 self.__patches = Patches(self)
145 if not stackupgrade.update_to_current_format_version(repository, name):
146 raise exception.StgException('%s: branch not initialized' % name)
147 name = property(lambda self: self.__name)
148 repository = property(lambda self: self.__repository)
149 patchorder = property(lambda self: self.__patchorder)
150 patches = property(lambda self: self.__patches)
153 return os.path.join(self.__repository.directory, 'patches', self.__name)
155 return 'refs/heads/%s' % self.__name
158 return self.__repository.refs.get(self.__ref())
159 def set_head(self, commit, msg):
160 self.__repository.refs.set(self.__ref(), commit, msg)
163 if self.patchorder.applied:
164 return self.patches.get(self.patchorder.applied[0]
168 def head_top_equal(self):
169 if not self.patchorder.applied:
171 return self.head == self.patches.get(self.patchorder.applied[-1]).commit
173 class Repository(git.Repository):
174 """A git L{Repository<git.Repository>} with some added StGit-specific
176 def __init__(self, *args, **kwargs):
177 git.Repository.__init__(self, *args, **kwargs)
178 self.__stacks = {} # name -> Stack
180 def current_branch(self):
181 return utils.strip_leading('refs/heads/', self.head)
183 def current_stack(self):
184 return self.get_stack()
185 def get_stack(self, name = None):
187 name = self.current_branch
188 if not name in self.__stacks:
189 self.__stacks[name] = Stack(self, name)
190 return self.__stacks[name]