// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.schema;

import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;

import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;

import java.io.IOException;

/** Creates the {@code All-Users} repository. */
public class AllUsersCreator {
  private final GitRepositoryManager mgr;
  private final AllUsersName allUsersName;

  @Inject
  AllUsersCreator(GitRepositoryManager mgr, AllUsersName allUsersName) {
    this.mgr = mgr;
    this.allUsersName = allUsersName;
  }

  public void create() throws IOException, ConfigInvalidException {
    Repository git = null;
    try {
      git = mgr.openRepository(allUsersName);
    } catch (RepositoryNotFoundException notFound) {
      try {
        git = mgr.createRepository(allUsersName);
      } catch (RepositoryNotFoundException err) {
        String name = allUsersName.get();
        throw new IOException("Cannot create repository " + name, err);
      }
    } finally {
      if (git != null) {
        git.close();
      }
    }
  }
}

@@ -673,92 +673,560 @@
 rit.
-server.config.AllUsersName;%0Aimport com.google.gerrit.server.git.GitRepositoryManager
+common.Version;%0Aimport com.google.gerrit.common.data.AccessSection;%0Aimport com.google.gerrit.common.data.Permission;%0Aimport com.google.gerrit.reviewdb.client.Project;%0Aimport com.google.gerrit.reviewdb.client.RefNames;%0Aimport com.google.gerrit.server.GerritPersonIdent;%0Aimport com.google.gerrit.server.config.AllUsersName;%0Aimport com.google.gerrit.server.extensions.events.GitReferenceUpdated;%0Aimport com.google.gerrit.server.git.GitRepositoryManager;%0Aimport com.google.gerrit.server.git.MetaDataUpdate;%0Aimport com.google.gerrit.server.git.ProjectConfig
 ;%0Aim
@@ -1364,32 +1364,73 @@
 FoundException;%0A
+import org.eclipse.jgit.lib.PersonIdent;%0A
 import org.eclip
@@ -1647,16 +1647,56 @@
 ersName;
+%0A  private final PersonIdent serverUser;
 %0A%0A  @Inj
@@ -1717,16 +1717,23 @@
 Creator(
+%0A      
 GitRepos
@@ -1749,16 +1749,22 @@
 ger mgr,
+%0A     
  AllUser
@@ -1781,16 +1781,65 @@
 sersName
+,%0A      @GerritPersonIdent PersonIdent serverUser
 ) %7B%0A    
@@ -1892,16 +1892,50 @@
 rsName;%0A
+    this.serverUser = serverUser;%0A
   %7D%0A%0A  p
@@ -2078,24 +2078,49 @@
 UsersName);%0A
+      initAllUsers(git);%0A
     %7D catch 
@@ -2218,24 +2218,51 @@
 UsersName);%0A
+        initAllUsers(git);%0A
       %7D catc
@@ -2500,11 +2500,750 @@
    %7D%0A  %7D
+%0A%0A  private void initAllUsers(Repository git)%0A      throws IOException, ConfigInvalidException %7B%0A    MetaDataUpdate md = new MetaDataUpdate(%0A        GitReferenceUpdated.DISABLED,%0A        allUsersName,%0A        git);%0A    md.getCommitBuilder().setAuthor(serverUser);%0A    md.getCommitBuilder().setCommitter(serverUser);%0A    md.setMessage(%22Initialized Gerrit Code Review %22 + Version.getVersion());%0A%0A    ProjectConfig config = ProjectConfig.read(md);%0A    Project project = config.getProject();%0A    project.setDescription(%22Individual user settings and preferences.%22);%0A%0A    AccessSection all = config.getAccessSection(RefNames.REFS_USER + %22*%22, true);%0A    all.getPermission(Permission.READ, true).setExclusiveGroup(true);%0A    config.commit(md);%0A  %7D
 %0A%7D%0A

Configure All-Users to protect read of the refs/users/* namespace