annotate rhodecode/lib/middleware/pygrack.py @ 3797:d7488551578e beta

synced vcs with upstream - moved subprocessio module to VCS - many small changes to make embedded vcs as similar to to external lib - use only absolute imports - patch vcs config during load pylons env
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 27 Apr 2013 11:24:25 +0200
parents 238486bb71ab
children dc4644865e8b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 import os
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 import socket
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 import logging
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 import subprocess
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
5 import traceback
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 from webob import Request, Response, exc
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8
3376
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
9 import rhodecode
3797
d7488551578e synced vcs with upstream
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
10 from rhodecode.lib.vcs import subprocessio
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 log = logging.getLogger(__name__)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 class FileWrapper(object):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 def __init__(self, fd, content_length):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 self.fd = fd
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 self.content_length = content_length
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 self.remain = content_length
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 def read(self, size):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 if size <= self.remain:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 try:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 data = self.fd.read(size)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 except socket.error:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 raise IOError(self)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 self.remain -= size
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 elif self.remain:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 data = self.fd.read(self.remain)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 self.remain = 0
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 else:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 data = None
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 return data
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 def __repr__(self):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 return '<FileWrapper %s len: %s, read: %s>' % (
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 self.fd, self.content_length, self.content_length - self.remain
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 )
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 class GitRepository(object):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 git_folder_signature = set(['config', 'head', 'info', 'objects', 'refs'])
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 commands = ['git-upload-pack', 'git-receive-pack']
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
46 def __init__(self, repo_name, content_path, extras):
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 files = set([f.lower() for f in os.listdir(content_path)])
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 if not (self.git_folder_signature.intersection(files)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 == self.git_folder_signature):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 raise OSError('%s missing git signature' % content_path)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 self.content_path = content_path
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 self.valid_accepts = ['application/x-%s-result' %
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 c for c in self.commands]
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 self.repo_name = repo_name
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
55 self.extras = extras
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 def _get_fixedpath(self, path):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 Small fix for repo_path
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 :param path:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 :type path:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 return path.split(self.repo_name, 1)[-1].strip('/')
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 def inforefs(self, request, environ):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 WSGI Response producer for HTTP GET Git Smart
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 HTTP /info/refs request.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
72 git_command = request.GET.get('service')
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 if git_command not in self.commands:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 log.debug('command %s not allowed' % git_command)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 return exc.HTTPMethodNotAllowed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 # note to self:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 # please, resist the urge to add '\n' to git capture and increment
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 # line count by 1.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 # The code in Git client not only does NOT need '\n', but actually
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 # blows up if you sprinkle "flush" (0000) as "0001\n".
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 # It reads binary, per number of bytes specified.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 # if you do add '\n' as part of data, count it.
2579
c344a7fca2a7 reset response charset to fix Egit/JGit issues
Marcin Kuzminski <marcin@python-works.com>
parents: 2502
diff changeset
84 server_advert = '# service=%s' % git_command
c344a7fca2a7 reset response charset to fix Egit/JGit issues
Marcin Kuzminski <marcin@python-works.com>
parents: 2502
diff changeset
85 packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower()
3376
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
86 _git_path = rhodecode.CONFIG.get('git_path', 'git')
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 try:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 out = subprocessio.SubprocessIOChunker(
3376
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
89 r'%s %s --stateless-rpc --advertise-refs "%s"' % (
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
90 _git_path, git_command[4:], self.content_path),
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 starting_values=[
2579
c344a7fca2a7 reset response charset to fix Egit/JGit issues
Marcin Kuzminski <marcin@python-works.com>
parents: 2502
diff changeset
92 packet_len + server_advert + '0000'
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 ]
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 )
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 except EnvironmentError, e:
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
96 log.error(traceback.format_exc())
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 raise exc.HTTPExpectationFailed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 resp = Response()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 resp.content_type = 'application/x-%s-advertisement' % str(git_command)
2579
c344a7fca2a7 reset response charset to fix Egit/JGit issues
Marcin Kuzminski <marcin@python-works.com>
parents: 2502
diff changeset
100 resp.charset = None
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 resp.app_iter = out
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 return resp
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 def backend(self, request, environ):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 WSGI Response producer for HTTP POST Git Smart HTTP requests.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 Reads commands and data from HTTP POST's body.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 returns an iterator obj with contents of git command's
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 response to stdout
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 git_command = self._get_fixedpath(request.path_info)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 if git_command not in self.commands:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 log.debug('command %s not allowed' % git_command)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 return exc.HTTPMethodNotAllowed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 if 'CONTENT_LENGTH' in environ:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 inputstream = FileWrapper(environ['wsgi.input'],
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 request.content_length)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 else:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 inputstream = environ['wsgi.input']
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 try:
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
123 gitenv = os.environ
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
124 # forget all configs
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
125 gitenv['GIT_CONFIG_NOGLOBAL'] = '1'
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
126 opts = dict(
2406
7be31af5bc78 changed scope of calling EXTENSIONS from rhodecode for githooks to be able to execute them
Marcin Kuzminski <marcin@python-works.com>
parents: 2402
diff changeset
127 env=gitenv,
7be31af5bc78 changed scope of calling EXTENSIONS from rhodecode for githooks to be able to execute them
Marcin Kuzminski <marcin@python-works.com>
parents: 2402
diff changeset
128 cwd=os.getcwd()
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
129 )
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
130 cmd = r'git %s --stateless-rpc "%s"' % (git_command[4:],
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
131 self.content_path),
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
132 log.debug('handling cmd %s' % cmd)
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 out = subprocessio.SubprocessIOChunker(
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
134 cmd,
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
135 inputstream=inputstream,
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
136 **opts
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
137 )
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 except EnvironmentError, e:
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
139 log.error(traceback.format_exc())
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 raise exc.HTTPExpectationFailed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 if git_command in [u'git-receive-pack']:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 # updating refs manually after each push.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 # Needed for pre-1.7.0.4 git clients using regular HTTP mode.
3376
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
145 _git_path = rhodecode.CONFIG.get('git_path', 'git')
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
146 cmd = (u'%s --git-dir "%s" '
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
147 'update-server-info' % (_git_path, self.content_path))
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
148 log.debug('handling cmd %s' % cmd)
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
149 subprocess.call(cmd, shell=True)
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 resp = Response()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 resp.content_type = 'application/x-%s-result' % git_command.encode('utf8')
2581
ee980eadc4b1 reset charset for git rpc cals also
Marcin Kuzminski <marcin@python-works.com>
parents: 2579
diff changeset
153 resp.charset = None
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 resp.app_iter = out
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 return resp
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 def __call__(self, environ, start_response):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 request = Request(environ)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 _path = self._get_fixedpath(request.path_info)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 if _path.startswith('info/refs'):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 app = self.inforefs
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 elif [a for a in self.valid_accepts if a in request.accept]:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 app = self.backend
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 try:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 resp = app(request, environ)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 except exc.HTTPException, e:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 resp = e
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
168 log.error(traceback.format_exc())
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 except Exception, e:
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
170 log.error(traceback.format_exc())
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 resp = exc.HTTPInternalServerError()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 return resp(environ, start_response)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 class GitDirectory(object):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
177 def __init__(self, repo_root, repo_name, extras):
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 repo_location = os.path.join(repo_root, repo_name)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 if not os.path.isdir(repo_location):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 raise OSError(repo_location)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 self.content_path = repo_location
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 self.repo_name = repo_name
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 self.repo_location = repo_location
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
185 self.extras = extras
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 def __call__(self, environ, start_response):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 content_path = self.content_path
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 try:
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
190 app = GitRepository(self.repo_name, content_path, self.extras)
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 except (AssertionError, OSError):
2764
c7a27b04ce58 fixed #545, exception during cloning of non-bare repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2726
diff changeset
192 content_path = os.path.join(content_path, '.git')
c7a27b04ce58 fixed #545, exception during cloning of non-bare repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2726
diff changeset
193 if os.path.isdir(content_path):
c7a27b04ce58 fixed #545, exception during cloning of non-bare repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2726
diff changeset
194 app = GitRepository(self.repo_name, content_path, self.extras)
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 else:
2594
3a54e1e121d5 fix http NotFound call in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2581
diff changeset
196 return exc.HTTPNotFound()(environ, start_response)
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 return app(environ, start_response)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
200 def make_wsgi_app(repo_name, repo_root, extras):
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2594
diff changeset
201 return GitDirectory(repo_root, repo_name, extras)