Mercurial > kallithea
annotate rhodecode/lib/middleware/pygrack.py @ 2965:d0553004e73d beta
fixed #623: Lang meta-tag doesn't work with C#/C++
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 27 Oct 2012 00:09:07 +0200 |
parents | f68522e3df79 |
children | e67b2ef07a8e |
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 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 from rhodecode.lib import subprocessio |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 log = logging.getLogger(__name__) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 |
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 class FileWrapper(object): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 def __init__(self, fd, content_length): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 self.fd = fd |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 self.content_length = content_length |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 self.remain = content_length |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 def read(self, size): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 if size <= self.remain: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 try: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 data = self.fd.read(size) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 except socket.error: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 raise IOError(self) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 self.remain -= size |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 elif self.remain: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 data = self.fd.read(self.remain) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 self.remain = 0 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 else: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 data = None |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 return data |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 def __repr__(self): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 return '<FileWrapper %s len: %s, read: %s>' % ( |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 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
|
38 ) |
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 class GitRepository(object): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 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
|
43 commands = ['git-upload-pack', 'git-receive-pack'] |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 == self.git_folder_signature): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 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
|
50 self.content_path = content_path |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 self.valid_accepts = ['application/x-%s-result' % |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 c for c in self.commands] |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 self.repo_name = repo_name |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
54 self.extras = extras |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 def _get_fixedpath(self, path): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 """ |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 Small fix for repo_path |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
59 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 :param path: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 :type path: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 """ |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 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
|
64 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 def inforefs(self, request, environ): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 """ |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 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
|
68 HTTP /info/refs request. |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 """ |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
71 git_command = request.GET.get('service') |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 if git_command not in self.commands: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 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
|
74 return exc.HTTPMethodNotAllowed() |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 # note to self: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 # 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
|
78 # line count by 1. |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 # 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
|
80 # 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
|
81 # 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
|
82 # 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
|
83 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
|
84 packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower() |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 try: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 out = subprocessio.SubprocessIOChunker( |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 r'git %s --stateless-rpc --advertise-refs "%s"' % ( |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 git_command[4:], self.content_path), |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 starting_values=[ |
2579
c344a7fca2a7
reset response charset to fix Egit/JGit issues
Marcin Kuzminski <marcin@python-works.com>
parents:
2502
diff
changeset
|
90 packet_len + server_advert + '0000' |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 ] |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 ) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 except EnvironmentError, e: |
2875
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
94 log.error(traceback.format_exc()) |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 raise exc.HTTPExpectationFailed() |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 resp = Response() |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 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
|
98 resp.charset = None |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 resp.app_iter = out |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 return resp |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 def backend(self, request, environ): |
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 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
|
105 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
|
106 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
|
107 response to stdout |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 """ |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 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
|
110 if git_command not in self.commands: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 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
|
112 return exc.HTTPMethodNotAllowed() |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 if 'CONTENT_LENGTH' in environ: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 inputstream = FileWrapper(environ['wsgi.input'], |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 request.content_length) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 else: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 inputstream = environ['wsgi.input'] |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 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
|
121 gitenv = os.environ |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
122 from rhodecode.lib.compat import json |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
123 gitenv['RHODECODE_EXTRAS'] = json.dumps(self.extras) |
2402
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. |
2875
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
145 cmd = (u'git --git-dir "%s" ' |
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
146 'update-server-info' % self.content_path) |
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
147 log.debug('handling cmd %s' % cmd) |
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
148 subprocess.call(cmd, shell=True) |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
150 resp = Response() |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 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
|
152 resp.charset = None |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
153 resp.app_iter = out |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 return resp |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
155 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 def __call__(self, environ, start_response): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
157 request = Request(environ) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
158 _path = self._get_fixedpath(request.path_info) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
159 if _path.startswith('info/refs'): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
160 app = self.inforefs |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
161 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
|
162 app = self.backend |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 try: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
164 resp = app(request, environ) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
165 except exc.HTTPException, e: |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
166 resp = e |
2875
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
167 log.error(traceback.format_exc()) |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
168 except Exception, e: |
2875
f68522e3df79
more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2870
diff
changeset
|
169 log.error(traceback.format_exc()) |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 resp = exc.HTTPInternalServerError() |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 return resp(environ, start_response) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 |
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 class GitDirectory(object): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
176 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
|
177 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
|
178 if not os.path.isdir(repo_location): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
179 raise OSError(repo_location) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
180 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
181 self.content_path = repo_location |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
182 self.repo_name = repo_name |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
183 self.repo_location = repo_location |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
184 self.extras = extras |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
185 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
186 def __call__(self, environ, start_response): |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 content_path = self.content_path |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
188 try: |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
189 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
|
190 except (AssertionError, OSError): |
2764
c7a27b04ce58
fixed #545, exception during cloning of non-bare repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
2726
diff
changeset
|
191 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
|
192 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
|
193 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
|
194 else: |
2594
3a54e1e121d5
fix http NotFound call in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents:
2581
diff
changeset
|
195 return exc.HTTPNotFound()(environ, start_response) |
2382
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
196 return app(environ, start_response) |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
197 |
034e4fe1ebb2
changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
198 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
199 def make_wsgi_app(repo_name, repo_root, extras): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2594
diff
changeset
|
200 return GitDirectory(repo_root, repo_name, extras) |