transport-helper: update ref status after push with export

Also add check_output from python 2.7. Signed-off-by: Sverre Rabbelier <srabbelier@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sverre Rabbelier committed Jul 16, 2011 at 15:03 UTC 6c8151a32e59c3109b3acc886358bfe6c14612fb
4 files changed +67 -2
git-remote-testgit.py
+5 -1
@@ -147,11 +147,15 @@ def do_export(repo, args):
147 sys.stdout.flush()
148
149 update_local_repo(repo)
150 - repo.importer.do_import(repo.gitdir)
150 + changed = repo.importer.do_import(repo.gitdir)
151
152 if not repo.local:
153 repo.non_local.push(repo.gitdir)
154
155 + for ref in changed:
156 + print "ok %s" % ref
157 + print
158 +
159
160 COMMANDS = {
161 'capabilities': do_capabilities,
git_remote_helpers/git/importer.py
+27 -1
@@ -1,7 +1,7 @@
1 import os
2 import subprocess
3
4 -from git_remote_helpers.util import check_call
4 +from git_remote_helpers.util import check_call, check_output
5
6
7 class GitImporter(object):
@@ -16,6 +16,18 @@ class GitImporter(object):
16
17 self.repo = repo
18
19 + def get_refs(self, gitdir):
20 + """Returns a dictionary with refs.
21 + """
22 + args = ["git", "--git-dir=" + gitdir, "for-each-ref", "refs/heads"]
23 + lines = check_output(args).strip().split('\n')
24 + refs = {}
25 + for line in lines:
26 + value, name = line.split(' ')
27 + name = name.strip('commit\t')
28 + refs[name] = value
29 + return refs
30 +
31 def do_import(self, base):
32 """Imports a fast-import stream to the given directory.
33
@@ -32,9 +44,23 @@ class GitImporter(object):
44 if not os.path.exists(dirname):
45 os.makedirs(dirname)
46
47 + refs_before = self.get_refs(gitdir)
48 +
49 args = ["git", "--git-dir=" + gitdir, "fast-import", "--quiet", "--export-marks=" + path]
50
51 if os.path.exists(path):
52 args.append("--import-marks=" + path)
53
54 check_call(args)
55 +
56 + refs_after = self.get_refs(gitdir)
57 +
58 + changed = {}
59 +
60 + for name, value in refs_after.iteritems():
61 + if refs_before.get(name) == value:
62 + continue
63 +
64 + changed[name] = value
65 +
66 + return changed
git_remote_helpers/util.py
+34
@@ -175,6 +175,40 @@ def check_call(*popenargs, **kwargs):
175 return 0
176
177
178 +# from python2.7:subprocess.py
179 +def check_output(*popenargs, **kwargs):
180 + r"""Run command with arguments and return its output as a byte string.
181 +
182 + If the exit code was non-zero it raises a CalledProcessError. The
183 + CalledProcessError object will have the return code in the returncode
184 + attribute and output in the output attribute.
185 +
186 + The arguments are the same as for the Popen constructor. Example:
187 +
188 + >>> check_output(["ls", "-l", "/dev/null"])
189 + 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
190 +
191 + The stdout argument is not allowed as it is used internally.
192 + To capture standard error in the result, use stderr=STDOUT.
193 +
194 + >>> check_output(["/bin/sh", "-c",
195 + ... "ls -l non_existent_file ; exit 0"],
196 + ... stderr=STDOUT)
197 + 'ls: non_existent_file: No such file or directory\n'
198 + """
199 + if 'stdout' in kwargs:
200 + raise ValueError('stdout argument not allowed, it will be overridden.')
201 + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
202 + output, unused_err = process.communicate()
203 + retcode = process.poll()
204 + if retcode:
205 + cmd = kwargs.get("args")
206 + if cmd is None:
207 + cmd = popenargs[0]
208 + raise subprocess.CalledProcessError(retcode, cmd)
209 + return output
210 +
211 +
212 def file_reader_method (missing_ok = False):
213 """Decorator for simplifying reading of files.
214
transport-helper.c
+1
@@ -764,6 +764,7 @@ static int push_refs_with_export(struct transport *transport,
764
765 if (finish_command(&exporter))
766 die("Error while running fast-export");
767 + push_update_refs_status(data, remote_refs);
768 return 0;
769 }
770