From cba32b71bd27b747f5a667ec6ba11ff5e21362ef Mon Sep 17 00:00:00 2001
From: Sean McGivern <sean@gitlab.com>
Date: Wed, 8 Jun 2016 11:34:24 +0100
Subject: [PATCH] Allow clones from /namespace/project

Redirect /namespace/project/info/refs to
/namespace/project.git/info/refs, so `git clone` works on either
namespace/project.git or namespace/project.
---
 CHANGELOG        |  1 +
 config/routes.rb | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/CHANGELOG b/CHANGELOG
index 5136756079d..419ea3d1b50 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -17,6 +17,7 @@ v 8.9.0 (unreleased)
   - Redesign navigation for project pages
   - Fix groups API to list only user's accessible projects
   - Redesign account and email confirmation emails
+  - `git clone https://host/namespace/project` now works, in addition to using the `.git` suffix
   - Bump nokogiri to 1.6.8
   - Use gitlab-shell v3.0.0
   - Use Knapsack to evenly distribute tests across multiple nodes
diff --git a/config/routes.rb b/config/routes.rb
index 240dcc74b06..f1a43cbbd26 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -441,6 +441,23 @@ Rails.application.routes.draw do
   resources :namespaces, path: '/', constraints: { id: /[a-zA-Z.0-9_\-]+/ }, only: [] do
     resources(:projects, constraints: { id: /[a-zA-Z.0-9_\-]+(?<!\.atom)/ }, except:
               [:new, :create, :index], path: "/") do
+
+      # Allow /info/refs, /info/refs?service=git-upload-pack, and
+      # /info/refs?service=git-receive-pack, but nothing else.
+      #
+      git_http_handshake = lambda do |request|
+        request.query_string.blank? ||
+          request.query_string.match(/\Aservice=git-(upload|receive)-pack\z/)
+      end
+
+      ref_redirect = redirect do |params, request|
+        path = "#{params[:namespace_id]}/#{params[:project_id]}.git/info/refs"
+        path << "?#{request.query_string}" unless request.query_string.blank?
+        path
+      end
+
+      get '/info/refs', constraints: git_http_handshake, to: ref_redirect
+
       member do
         put :transfer
         delete :remove_fork
-- 
2.30.9