annotate rhodecode/lib/middleware/pygrack.py @ 3840:dc4644865e8b beta

Implemented simple gist functionality ref #530. - creation of public/private gists with given lifetime - rhodecode-gist CLI for quick gist creation
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 11 May 2013 20:24:02 +0200
parents d7488551578e
children ffd45b185016
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 """
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()
3376
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
85 _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
86 try:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 out = subprocessio.SubprocessIOChunker(
3376
e67b2ef07a8e git executable is now configurable via .ini files
Marcin Kuzminski <marcin@python-works.com>
parents: 2875
diff changeset
88 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
89 _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
90 starting_values=[
2579
c344a7fca2a7 reset response charset to fix Egit/JGit issues
Marcin Kuzminski <marcin@python-works.com>
parents: 2502
diff changeset
91 packet_len + server_advert + '0000'
2382
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 )
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 except EnvironmentError, e:
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
95 log.error(traceback.format_exc())
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 raise exc.HTTPExpectationFailed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 resp = Response()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 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
99 resp.charset = None
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 resp.app_iter = out
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 return resp
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 def backend(self, request, environ):
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 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
106 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
107 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
108 response to stdout
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 """
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 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
111 if git_command not in self.commands:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 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
113 return exc.HTTPMethodNotAllowed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 if 'CONTENT_LENGTH' in environ:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 inputstream = FileWrapper(environ['wsgi.input'],
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 request.content_length)
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 else:
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 inputstream = environ['wsgi.input']
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 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
122 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
123 # 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
124 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
125 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
126 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
127 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
128 )
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
129 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
130 self.content_path),
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
131 log.debug('handling cmd %s' % cmd)
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 out = subprocessio.SubprocessIOChunker(
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
133 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
134 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
135 **opts
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2398
diff changeset
136 )
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 except EnvironmentError, e:
2875
f68522e3df79 more logging in pygrack
Marcin Kuzminski <marcin@python-works.com>
parents: 2870
diff changeset
138 log.error(traceback.format_exc())
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 raise exc.HTTPExpectationFailed()
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 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
142 # updating refs manually after each push.
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 # 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
144 _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
145 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
146 'update-server-info' % (_git_path, self.content_path))
2875
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)