Line data Source code
1 : // Copyright (C) 2018 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.testing; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.collect.ImmutableList; 20 : import com.google.common.collect.Streams; 21 : import com.google.gerrit.extensions.common.CommitInfo; 22 : import com.google.gerrit.server.git.CommitUtil; 23 : import java.io.IOException; 24 : import org.eclipse.jgit.lib.Ref; 25 : import org.eclipse.jgit.lib.Repository; 26 : import org.eclipse.jgit.revwalk.RevSort; 27 : import org.eclipse.jgit.revwalk.RevWalk; 28 : 29 0 : public class GitTestUtil { 30 : public static ImmutableList<CommitInfo> log(Repository repo, String refName) throws Exception { 31 1 : try (RevWalk rw = new RevWalk(repo)) { 32 1 : Ref ref = repo.exactRef(refName); 33 1 : if (ref != null) { 34 1 : rw.sort(RevSort.REVERSE); 35 1 : rw.markStart(rw.parseCommit(ref.getObjectId())); 36 1 : return Streams.stream(rw) 37 1 : .map( 38 : c -> { 39 : try { 40 1 : return CommitUtil.toCommitInfo(c); 41 0 : } catch (IOException e) { 42 0 : throw new IllegalStateException( 43 0 : "unexpected state when converting commit " + c.getName(), e); 44 : } 45 : }) 46 1 : .collect(toImmutableList()); 47 : } 48 1 : } 49 0 : return ImmutableList.of(); 50 : } 51 : }