annotate rhodecode/lib/middleware/simplegit.py @ 3135:e7ba69286276 beta

logging: include more info in action logging These messages are very frequent and thus shows what is going on on the server - and it is nice to have some extra information to give some context for other messages that might be logged.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 02 Jan 2013 13:54:26 +0100
parents 9b92cf5a0cca
children cd50d1b5f35b 5440ea1d0628
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
1 # -*- coding: utf-8 -*-
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
2 """
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
3 rhodecode.lib.middleware.simplegit
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
5
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
6 SimpleGit middleware for handling git protocol request (push/clone etc.)
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
7 It's implemented with basic auth function
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
8
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
9 :created_on: Apr 28, 2010
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
10 :author: marcink
1824
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
11 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com>
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
12 :license: GPLv3, see COPYING for more details.
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
13 """
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
14 # This program is free software: you can redistribute it and/or modify
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
15 # it under the terms of the GNU General Public License as published by
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
16 # the Free Software Foundation, either version 3 of the License, or
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
17 # (at your option) any later version.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
18 #
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # This program is distributed in the hope that it will be useful,
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 # GNU General Public License for more details.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
23 #
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 # You should have received a copy of the GNU General Public License
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
635
fd63782c4426 Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents: 625
diff changeset
26
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
27 import os
2052
320806ff6be2 fixes git-protocol with
Marcin Kuzminski <marcin@python-works.com>
parents: 2026
diff changeset
28 import re
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
29 import logging
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
30 import traceback
635
fd63782c4426 Fixed age, for new vcs implementation. Removed all obsolete date formatters
Marcin Kuzminski <marcin@python-works.com>
parents: 625
diff changeset
31
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
32 from dulwich import server as dulserver
2578
d24c70ec9312 Fixed issue with gzipped streams for large git pushes.
Marcin Kuzminski <marcin@python-works.com>
parents: 2500
diff changeset
33 from dulwich.web import LimitedInputFilter, GunzipFilter
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
34 from rhodecode.lib.exceptions import HTTPLockedRC
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
35 from rhodecode.lib.hooks import pre_pull
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
36
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
37
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
38 class SimpleGitUploadPackHandler(dulserver.UploadPackHandler):
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
39
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
40 def handle(self):
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
41 write = lambda x: self.proto.write_sideband(1, x)
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
42
1275
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
43 graph_walker = dulserver.ProtocolGraphWalker(self,
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
44 self.repo.object_store,
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
45 self.repo.get_peeled)
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
46 objects_iter = self.repo.fetch_objects(
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
47 graph_walker.determine_wants, graph_walker, self.progress,
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
48 get_tagged=self.get_tagged)
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
49
2197
b14d8bd96144 Synced SimpleGitUploadPackHandler with latest dulwich code
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
50 # Did the process short-circuit (e.g. in a stateless RPC call)? Note
b14d8bd96144 Synced SimpleGitUploadPackHandler with latest dulwich code
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
51 # that the client still expects a 0-object pack in most cases.
b14d8bd96144 Synced SimpleGitUploadPackHandler with latest dulwich code
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
52 if objects_iter is None:
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
53 return
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
54
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
55 self.progress("counting objects: %d, done.\n" % len(objects_iter))
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
56 dulserver.write_pack_objects(dulserver.ProtocolFile(None, write),
2197
b14d8bd96144 Synced SimpleGitUploadPackHandler with latest dulwich code
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
57 objects_iter)
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
58 messages = []
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
59 messages.append('thank you for using rhodecode')
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
60
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
61 for msg in messages:
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
62 self.progress(msg + "\n")
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
63 # we are done
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
64 self.proto.write("0000")
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
65
2197
b14d8bd96144 Synced SimpleGitUploadPackHandler with latest dulwich code
Marcin Kuzminski <marcin@python-works.com>
parents: 2184
diff changeset
66
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
67 dulserver.DEFAULT_HANDLERS = {
2322
6ea36346590a comments on git-operations mapp
Marcin Kuzminski <marcin@python-works.com>
parents: 2236
diff changeset
68 #git-ls-remote, git-clone, git-fetch and git-pull
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
69 'git-upload-pack': SimpleGitUploadPackHandler,
2322
6ea36346590a comments on git-operations mapp
Marcin Kuzminski <marcin@python-works.com>
parents: 2236
diff changeset
70 #git-push
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
71 'git-receive-pack': dulserver.ReceivePackHandler,
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
72 }
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
73
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
74 # not used for now until dulwich get's fixed
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
75 #from dulwich.repo import Repo
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
76 #from dulwich.web import make_wsgi_chain
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
77
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 from paste.httpheaders import REMOTE_USER, AUTH_TYPE
2668
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2578
diff changeset
79 from webob.exc import HTTPNotFound, HTTPForbidden, HTTPInternalServerError, \
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2578
diff changeset
80 HTTPBadRequest, HTTPNotAcceptable
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
81
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
82 from rhodecode.lib.utils2 import safe_str, fix_PATH, get_server_url
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
83 from rhodecode.lib.base import BaseVCSController
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
84 from rhodecode.lib.auth import get_container_username
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
85 from rhodecode.lib.utils import is_valid_repo, make_ui
2716
4c71667160e5 use os.environ as a fallback for getting special info from hooks, this will allow
Marcin Kuzminski <marcin@python-works.com>
parents: 2668
diff changeset
86 from rhodecode.lib.compat import json
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
87 from rhodecode.model.db import User, RhodeCodeUi
903
04c9bb9ca6d6 code docs, updates
Marcin Kuzminski <marcin@python-works.com>
parents: 902
diff changeset
88
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 log = logging.getLogger(__name__)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90
1275
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
91
2061
9f0fe6777833 merge pull request #32 from codingtony
Marcin Kuzminski <marcin@python-works.com>
parents: 2060
diff changeset
92 GIT_PROTO_PAT = re.compile(r'^/(.+)/(info/refs|git-upload-pack|git-receive-pack)')
2052
320806ff6be2 fixes git-protocol with
Marcin Kuzminski <marcin@python-works.com>
parents: 2026
diff changeset
93
2058
fb51a6fc10ae updated CONTRIBUTORS
Marcin Kuzminski <marcin@python-works.com>
parents: 2057
diff changeset
94
2060
572855f7a392 reverted git fix as it broke pushing
Marcin Kuzminski <marcin@python-works.com>
parents: 2058
diff changeset
95 def is_git(environ):
2061
9f0fe6777833 merge pull request #32 from codingtony
Marcin Kuzminski <marcin@python-works.com>
parents: 2060
diff changeset
96 path_info = environ['PATH_INFO']
9f0fe6777833 merge pull request #32 from codingtony
Marcin Kuzminski <marcin@python-works.com>
parents: 2060
diff changeset
97 isgit_path = GIT_PROTO_PAT.match(path_info)
2100
f0649c7cf94a fixed some unicode problems with waitress
Marcin Kuzminski <marcin@python-works.com>
parents: 2090
diff changeset
98 log.debug('pathinfo: %s detected as GIT %s' % (
f0649c7cf94a fixed some unicode problems with waitress
Marcin Kuzminski <marcin@python-works.com>
parents: 2090
diff changeset
99 path_info, isgit_path != None)
f0649c7cf94a fixed some unicode problems with waitress
Marcin Kuzminski <marcin@python-works.com>
parents: 2090
diff changeset
100 )
2061
9f0fe6777833 merge pull request #32 from codingtony
Marcin Kuzminski <marcin@python-works.com>
parents: 2060
diff changeset
101 return isgit_path
2052
320806ff6be2 fixes git-protocol with
Marcin Kuzminski <marcin@python-works.com>
parents: 2026
diff changeset
102
2058
fb51a6fc10ae updated CONTRIBUTORS
Marcin Kuzminski <marcin@python-works.com>
parents: 2057
diff changeset
103
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
104 class SimpleGit(BaseVCSController):
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
106 def _handle_request(self, environ, start_response):
2060
572855f7a392 reverted git fix as it broke pushing
Marcin Kuzminski <marcin@python-works.com>
parents: 2058
diff changeset
107 if not is_git(environ):
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 return self.application(environ, start_response)
2668
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2578
diff changeset
109 if not self._check_ssl(environ, start_response):
f0851f37d6be Implementes #509 require SSL flag now works for both git and mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents: 2578
diff changeset
110 return HTTPNotAcceptable('SSL REQUIRED !')(environ, start_response)
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
111
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
112 ip_addr = self._get_ip_addr(environ)
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
113 username = None
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
114 self._git_first_op = False
898
9c4851dce8e6 fixed error propagation when using git/mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 756
diff changeset
115 # skip passing error to error controller
9c4851dce8e6 fixed error propagation when using git/mercurial requests
Marcin Kuzminski <marcin@python-works.com>
parents: 756
diff changeset
116 environ['pylons.status_code_redirect'] = True
665
070f32743632 Moved out reposcan into hg Model.
Marcin Kuzminski <marcin@python-works.com>
parents: 655
diff changeset
117
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
118 #======================================================================
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
119 # EXTRACT REPOSITORY NAME FROM ENV
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
120 #======================================================================
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
121 try:
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
122 repo_name = self.__get_repository(environ)
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
123 log.debug('Extracted repo name is %s' % repo_name)
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
124 except:
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
125 return HTTPInternalServerError()(environ, start_response)
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
126
2122
c137b8a81f8e git+hg middleware do repo verification at earliest possible state, giving 404 as fast as possible. If repo is not found.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
127 # quick check if that dir exists...
2716
4c71667160e5 use os.environ as a fallback for getting special info from hooks, this will allow
Marcin Kuzminski <marcin@python-works.com>
parents: 2668
diff changeset
128 if is_valid_repo(repo_name, self.basepath, 'git') is False:
2122
c137b8a81f8e git+hg middleware do repo verification at earliest possible state, giving 404 as fast as possible. If repo is not found.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
129 return HTTPNotFound()(environ, start_response)
c137b8a81f8e git+hg middleware do repo verification at earliest possible state, giving 404 as fast as possible. If repo is not found.
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
130
2060
572855f7a392 reverted git fix as it broke pushing
Marcin Kuzminski <marcin@python-works.com>
parents: 2058
diff changeset
131 #======================================================================
572855f7a392 reverted git fix as it broke pushing
Marcin Kuzminski <marcin@python-works.com>
parents: 2058
diff changeset
132 # GET ACTION PULL or PUSH
572855f7a392 reverted git fix as it broke pushing
Marcin Kuzminski <marcin@python-works.com>
parents: 2058
diff changeset
133 #======================================================================
572855f7a392 reverted git fix as it broke pushing
Marcin Kuzminski <marcin@python-works.com>
parents: 2058
diff changeset
134 action = self.__get_action(environ)
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
136 #======================================================================
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
137 # CHECK ANONYMOUS PERMISSION
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
138 #======================================================================
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
139 if action in ['pull', 'push']:
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
140 anonymous_user = self.__get_user('default')
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
141 username = anonymous_user.username
2026
c6e288dcb4a2 improved logging in git/hg middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 2021
diff changeset
142 anonymous_perm = self._check_permission(action, anonymous_user,
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
143 repo_name, ip_addr)
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
144
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
145 if anonymous_perm is not True or anonymous_user.active is False:
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
146 if anonymous_perm is not True:
1275
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
147 log.debug('Not enough credentials to access this '
2723276285ae pep8ify middlewares
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
148 'repository as anonymous user')
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
149 if anonymous_user.active is False:
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
150 log.debug('Anonymous access is disabled, running '
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
151 'authentication')
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
152 #==============================================================
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
153 # DEFAULT PERM FAILED OR ANONYMOUS ACCESS IS DISABLED SO WE
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
154 # NEED TO AUTHENTICATE AND ASK FOR AUTH USER PERMISSIONS
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
155 #==============================================================
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156
1630
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
157 # Attempting to retrieve username from the container
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
158 username = get_container_username(environ, self.config)
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
159
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
160 # If not authenticated by the container, running basic auth
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
161 if not username:
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1293
diff changeset
162 self.authenticate.realm = \
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1293
diff changeset
163 safe_str(self.config['rhodecode_realm'])
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
164 result = self.authenticate(environ)
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
165 if isinstance(result, str):
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
166 AUTH_TYPE.update(environ, 'basic')
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
167 REMOTE_USER.update(environ, result)
1630
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
168 username = result
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
169 else:
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
170 return result.wsgi_application(environ, start_response)
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
171
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
172 #==============================================================
1630
25d8e4836bc2 Improved container-based auth support for middleware
Liad Shani <liadff@gmail.com>
parents: 1620
diff changeset
173 # CHECK PERMISSIONS FOR THIS REQUEST USING GIVEN USERNAME
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
174 #==============================================================
2500
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
175 try:
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
176 user = self.__get_user(username)
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
177 if user is None or not user.active:
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
178 return HTTPForbidden()(environ, start_response)
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
179 username = user.username
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
180 except:
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
181 log.error(traceback.format_exc())
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
182 return HTTPInternalServerError()(environ, start_response)
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
183
2500
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
184 #check permissions for this repository
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
185 perm = self._check_permission(action, user, repo_name, ip_addr)
2500
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
186 if perm is not True:
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
187 return HTTPForbidden()(environ, start_response)
94ef0b609d17 possible fix for #486 undefined variable username
Marcin Kuzminski <marcin@python-works.com>
parents: 2468
diff changeset
188
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
189 # extras are injected into UI object and later available
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
190 # in hooks executed by rhodecode
2858
dd2d5b65cae9 Also export config filename via extras.config in simplegit middleware (juste like for the simplehg one)
Vincent Caron <vcaron@bearstech.com>
parents: 2726
diff changeset
191 from rhodecode import CONFIG
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
192 server_url = get_server_url(environ)
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
193 extras = {
3125
9b92cf5a0cca Added UserIpMap interface for allowed IP addresses and IP restriction access
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
194 'ip': ip_addr,
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
195 'username': username,
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
196 'action': action,
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
197 'repository': repo_name,
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
198 'scm': 'git',
2858
dd2d5b65cae9 Also export config filename via extras.config in simplegit middleware (juste like for the simplehg one)
Vincent Caron <vcaron@bearstech.com>
parents: 2726
diff changeset
199 'config': CONFIG['__file__'],
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
200 'server_url': server_url,
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
201 'make_lock': None,
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
202 'locked_by': [None, None]
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
203 }
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
204
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 #===================================================================
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 # GIT REQUEST HANDLING
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 #===================================================================
2100
f0649c7cf94a fixed some unicode problems with waitress
Marcin Kuzminski <marcin@python-works.com>
parents: 2090
diff changeset
208 repo_path = os.path.join(safe_str(self.basepath), safe_str(repo_name))
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
209 log.debug('Repository path is %s' % repo_path)
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
210
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
211 # CHECK LOCKING only if it's not ANONYMOUS USER
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
212 if username != User.DEFAULT_USER:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
213 log.debug('Checking locking on repository')
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
214 (make_lock,
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
215 locked,
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
216 locked_by) = self._check_locking_state(
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
217 environ=environ, action=action,
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
218 repo=repo_name, user_id=user.user_id
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
219 )
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
220 # store the make_lock for later evaluation in hooks
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
221 extras.update({'make_lock': make_lock,
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
222 'locked_by': locked_by})
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
223 # set the environ variables for this request
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
224 os.environ['RC_SCM_DATA'] = json.dumps(extras)
2869
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2858
diff changeset
225 fix_PATH()
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
226 log.debug('HOOKS extras is %s' % extras)
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
227 baseui = make_ui('db')
2209
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
228 self.__inject_extras(repo_path, baseui, extras)
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
229
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 try:
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
231 # invalidate cache on push
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
232 if action == 'push':
1761
b265be1c6093 Wrapped calls for git and hg middleware in extra block that clears db Session.
Marcin Kuzminski <marcin@python-works.com>
parents: 1630
diff changeset
233 self._invalidate_cache(repo_name)
2236
37c143aa8616 fixes issue #436 git push error
Marcin Kuzminski <marcin@python-works.com>
parents: 2209
diff changeset
234 self._handle_githooks(repo_name, action, baseui, environ)
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
235
3135
e7ba69286276 logging: include more info in action logging
Mads Kiilerich <madski@unity3d.com>
parents: 3125
diff changeset
236 log.info('%s action on GIT repo "%s" by "%s" from %s' %
e7ba69286276 logging: include more info in action logging
Mads Kiilerich <madski@unity3d.com>
parents: 3125
diff changeset
237 (action, repo_name, username, ip_addr))
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
238 app = self.__make_app(repo_name, repo_path, extras)
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
239 return app(environ, start_response)
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
240 except HTTPLockedRC, e:
3135
e7ba69286276 logging: include more info in action logging
Mads Kiilerich <madski@unity3d.com>
parents: 3125
diff changeset
241 log.debug('Repository LOCKED ret code 423!')
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
242 return e(environ, start_response)
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
243 except Exception:
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 log.error(traceback.format_exc())
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 return HTTPInternalServerError()(environ, start_response)
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
247 def __make_app(self, repo_name, repo_path, extras):
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
248 """
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
249 Make an wsgi application using dulserver
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1813
diff changeset
250
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
251 :param repo_name: name of the repository
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
252 :param repo_path: full path to the repository
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
253 """
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents: 2322
diff changeset
255 from rhodecode.lib.middleware.pygrack import make_wsgi_app
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents: 2322
diff changeset
256 app = make_wsgi_app(
2468
6af1e0d5ff9d Fix repo_root for grouped repos
hppj <hppj@postmage.biz>
parents: 2407
diff changeset
257 repo_root=safe_str(self.basepath),
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents: 2322
diff changeset
258 repo_name=repo_name,
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
259 extras=extras,
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents: 2322
diff changeset
260 )
2578
d24c70ec9312 Fixed issue with gzipped streams for large git pushes.
Marcin Kuzminski <marcin@python-works.com>
parents: 2500
diff changeset
261 app = GunzipFilter(LimitedInputFilter(app))
2382
034e4fe1ebb2 changed dulwich git interface to gitweb + subprocessio
Marcin Kuzminski <marcin@python-works.com>
parents: 2322
diff changeset
262 return app
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
264 def __get_repository(self, environ):
1496
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
265 """
f4fed0b32103 Rewrote git middleware with the same pattern as recent fix for #176
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
266 Get's repository name out of PATH_INFO header
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
267
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
268 :param environ: environ where PATH_INFO is stored
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
269 """
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
270 try:
1813
a8c66e870bd0 implements #285: Implemented non changeable urls for clone url, and web views
Marcin Kuzminski <marcin@python-works.com>
parents: 1761
diff changeset
271 environ['PATH_INFO'] = self._get_by_id(environ['PATH_INFO'])
2061
9f0fe6777833 merge pull request #32 from codingtony
Marcin Kuzminski <marcin@python-works.com>
parents: 2060
diff changeset
272 repo_name = GIT_PROTO_PAT.match(environ['PATH_INFO']).group(1)
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
273 except:
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
274 log.error(traceback.format_exc())
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
275 raise
2052
320806ff6be2 fixes git-protocol with
Marcin Kuzminski <marcin@python-works.com>
parents: 2026
diff changeset
276
918
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
277 return repo_name
b2d5868cc4b8 fixes #97 in simplehg and simplegit, force casting to headers
Marcin Kuzminski <marcin@python-works.com>
parents: 903
diff changeset
278
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 def __get_user(self, username):
1530
04027bdb876c Refactoring of model get functions
Marcin Kuzminski <marcin@python-works.com>
parents: 1507
diff changeset
280 return User.get_by_username(username)
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 def __get_action(self, environ):
2090
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
283 """
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
284 Maps git request commands into a pull or push command.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 989
diff changeset
285
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286 :param environ:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287 """
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 service = environ['QUERY_STRING'].split('=')
2090
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
289
620
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 if len(service) > 1:
19a62a5490fe added base simple git middleware, for future usage
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291 service_cmd = service[1]
2058
fb51a6fc10ae updated CONTRIBUTORS
Marcin Kuzminski <marcin@python-works.com>
parents: 2057
diff changeset
292 mapping = {
fb51a6fc10ae updated CONTRIBUTORS
Marcin Kuzminski <marcin@python-works.com>
parents: 2057
diff changeset
293 'git-receive-pack': 'push',
fb51a6fc10ae updated CONTRIBUTORS
Marcin Kuzminski <marcin@python-works.com>
parents: 2057
diff changeset
294 'git-upload-pack': 'pull',
fb51a6fc10ae updated CONTRIBUTORS
Marcin Kuzminski <marcin@python-works.com>
parents: 2057
diff changeset
295 }
2090
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
296 op = mapping[service_cmd]
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
297 self._git_stored_op = op
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
298 return op
625
d5372213db98 some hacking on simplegit middleware
Marcin Kuzminski <marcin@python-works.com>
parents: 620
diff changeset
299 else:
2090
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
300 # try to fallback to stored variable as we don't know if the last
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
301 # operation is pull/push
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
302 op = getattr(self, '_git_stored_op', 'pull')
2632a49cb402 fixes issue #372
Marcin Kuzminski <marcin@python-works.com>
parents: 2061
diff changeset
303 return op
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
304
2236
37c143aa8616 fixes issue #436 git push error
Marcin Kuzminski <marcin@python-works.com>
parents: 2209
diff changeset
305 def _handle_githooks(self, repo_name, action, baseui, environ):
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
306 """
2407
8a68e0292232 Change git & hg hooks to post. They shouldn't block as they are used just for logging actions. Futhermore post hooks have access to changesets, so it's much better flexible
Marcin Kuzminski <marcin@python-works.com>
parents: 2402
diff changeset
307 Handles pull action, push is handled by post-receive hook
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
308 """
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
309 from rhodecode.lib.hooks import log_pull_action
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
310 service = environ['QUERY_STRING'].split('=')
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
311
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
312 if len(service) < 2:
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
313 return
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
314
2209
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
315 from rhodecode.model.db import Repository
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
316 _repo = Repository.get_by_repo_name(repo_name)
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
317 _repo = _repo.scm_instance
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
318 _repo._repo.ui = baseui
2203
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
319
d9972f76322e added emulation of pull hook for git-backend, and dummy git-push hook
Marcin Kuzminski <marcin@python-works.com>
parents: 2197
diff changeset
320 _hooks = dict(baseui.configitems('hooks')) or {}
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
321 if action == 'pull':
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
322 # stupid git, emulate pre-pull hook !
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2716
diff changeset
323 pre_pull(ui=baseui, repo=_repo._repo)
2402
2eeb2ed72e55 Added handling of git hooks, extract pushed revisions and store them inside
Marcin Kuzminski <marcin@python-works.com>
parents: 2382
diff changeset
324 if action == 'pull' and _hooks.get(RhodeCodeUi.HOOK_PULL):
2236
37c143aa8616 fixes issue #436 git push error
Marcin Kuzminski <marcin@python-works.com>
parents: 2209
diff changeset
325 log_pull_action(ui=baseui, repo=_repo._repo)
2209
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
326
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
327 def __inject_extras(self, repo_path, baseui, extras={}):
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
328 """
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
329 Injects some extra params into baseui instance
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
330
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
331 :param baseui: baseui instance
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
332 :param extras: dict with extra params to put into baseui
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
333 """
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
334
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
335 # make our hgweb quiet so it doesn't print output
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
336 baseui.setconfig('ui', 'quiet', 'true')
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
337
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
338 #inject some additional parameters that will be available in ui
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
339 #for hooks
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
340 for k, v in extras.items():
19a6c23af14b Implemented pull command for remote repos for git
Marcin Kuzminski <marcin@python-works.com>
parents: 2207
diff changeset
341 baseui.setconfig('rhodecode_extras', k, v)