Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd.commands; 16 : 17 : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; 18 : 19 : import com.google.gerrit.common.data.GlobalCapability; 20 : import com.google.gerrit.entities.Account; 21 : import com.google.gerrit.entities.Project; 22 : import com.google.gerrit.entities.RefNames; 23 : import com.google.gerrit.exceptions.StorageException; 24 : import com.google.gerrit.extensions.annotations.RequiresCapability; 25 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 26 : import com.google.gerrit.server.account.AccountResolver; 27 : import com.google.gerrit.server.git.GitRepositoryManager; 28 : import com.google.gerrit.server.permissions.PermissionBackend; 29 : import com.google.gerrit.server.permissions.PermissionBackend.RefFilterOptions; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.gerrit.server.project.ProjectState; 32 : import com.google.gerrit.server.util.ManualRequestContext; 33 : import com.google.gerrit.server.util.OneOffRequestContext; 34 : import com.google.gerrit.sshd.CommandMetaData; 35 : import com.google.gerrit.sshd.SshCommand; 36 : import com.google.inject.Inject; 37 : import java.io.IOException; 38 : import java.util.Collection; 39 : import org.eclipse.jgit.errors.ConfigInvalidException; 40 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 41 : import org.eclipse.jgit.lib.Ref; 42 : import org.eclipse.jgit.lib.Repository; 43 : import org.kohsuke.args4j.Option; 44 : 45 : @RequiresCapability(GlobalCapability.READ_AS) 46 : @CommandMetaData( 47 : name = "ls-user-refs", 48 : description = "List refs visible to a specific user", 49 : runsAt = MASTER_OR_SLAVE) 50 1 : public class LsUserRefs extends SshCommand { 51 : @Inject private AccountResolver accountResolver; 52 : @Inject private OneOffRequestContext requestContext; 53 : @Inject private PermissionBackend permissionBackend; 54 : @Inject private GitRepositoryManager repoManager; 55 : 56 : @Option( 57 : name = "--project", 58 : aliases = {"-p"}, 59 : metaVar = "PROJECT", 60 : required = true, 61 : usage = "project for which the refs should be listed") 62 : private ProjectState projectState; 63 : 64 : @Option( 65 : name = "--user", 66 : aliases = {"-u"}, 67 : metaVar = "USER", 68 : required = true, 69 : usage = "user for which the groups should be listed") 70 : private String userName; 71 : 72 : @Option(name = "--only-refs-heads", usage = "list only refs under refs/heads") 73 : private boolean onlyRefsHeads; 74 : 75 : @Override 76 : protected void run() throws Failure { 77 0 : enableGracefulStop(); 78 : Account.Id userAccountId; 79 : try { 80 0 : userAccountId = accountResolver.resolve(userName).asUnique().account().id(); 81 0 : } catch (UnprocessableEntityException e) { 82 0 : stdout.println(e.getMessage()); 83 0 : stdout.flush(); 84 0 : return; 85 0 : } catch (StorageException | IOException | ConfigInvalidException e) { 86 0 : throw die(e); 87 0 : } 88 : 89 0 : Project.NameKey projectName = projectState.getNameKey(); 90 0 : try (Repository repo = repoManager.openRepository(projectName); 91 0 : ManualRequestContext ctx = requestContext.openAs(userAccountId)) { 92 : try { 93 0 : Collection<Ref> refsMap = 94 : permissionBackend 95 0 : .user(ctx.getUser()) 96 0 : .project(projectName) 97 0 : .filter(repo.getRefDatabase().getRefs(), repo, RefFilterOptions.defaults()); 98 : 99 0 : for (Ref ref : refsMap) { 100 0 : if (!onlyRefsHeads || ref.getName().startsWith(RefNames.REFS_HEADS)) { 101 0 : stdout.println(ref); 102 : } 103 0 : } 104 0 : } catch (IOException | PermissionBackendException e) { 105 0 : throw new Failure(1, "fatal: Error reading refs: '" + projectName, e); 106 0 : } 107 0 : } catch (RepositoryNotFoundException e) { 108 0 : throw die("'" + projectName + "': not a git archive", e); 109 0 : } catch (IOException e) { 110 0 : throw die("Error opening: '" + projectName, e); 111 0 : } 112 0 : } 113 : }