Line data Source code
1 : // Copyright (C) 2016 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.server.patch; 16 : 17 : import com.google.gerrit.entities.Patch; 18 : import com.google.gerrit.entities.Project; 19 : import com.google.gerrit.server.patch.filediff.FileDiffOutput; 20 : import com.google.inject.Inject; 21 : import com.google.inject.assistedinject.Assisted; 22 : import java.util.ArrayList; 23 : import java.util.List; 24 : import java.util.Map; 25 : import java.util.concurrent.Callable; 26 : import org.eclipse.jgit.lib.ObjectId; 27 : 28 : public class DiffSummaryLoader implements Callable<DiffSummary> { 29 : public interface Factory { 30 : DiffSummaryLoader create(DiffSummaryKey key, Project.NameKey project); 31 : } 32 : 33 : private final DiffOperations diffOperations; 34 : private final DiffSummaryKey key; 35 : private final Project.NameKey project; 36 : 37 : @Inject 38 : DiffSummaryLoader( 39 103 : DiffOperations diffOps, @Assisted DiffSummaryKey k, @Assisted Project.NameKey p) { 40 103 : diffOperations = diffOps; 41 103 : key = k; 42 103 : project = p; 43 103 : } 44 : 45 : @Override 46 : public DiffSummary call() throws Exception { 47 103 : ObjectId oldId = key.toPatchListKey().getOldId(); 48 103 : ObjectId newId = key.toPatchListKey().getNewId(); 49 : Map<String, FileDiffOutput> diffList = 50 103 : oldId == null 51 103 : ? diffOperations.listModifiedFilesAgainstParent( 52 : project, newId, /* parentNum= */ 0, DiffOptions.DEFAULTS) 53 103 : : diffOperations.listModifiedFiles(project, oldId, newId, DiffOptions.DEFAULTS); 54 103 : return toDiffSummary(diffList); 55 : } 56 : 57 : private DiffSummary toDiffSummary(Map<String, FileDiffOutput> fileDiffs) { 58 103 : List<String> r = new ArrayList<>(fileDiffs.size()); 59 103 : int linesInserted = 0; 60 103 : int linesDeleted = 0; 61 103 : for (String path : fileDiffs.keySet()) { 62 103 : if (Patch.isMagic(path)) { 63 103 : continue; 64 : } 65 90 : FileDiffOutput fileDiff = fileDiffs.get(path); 66 90 : linesInserted += fileDiff.insertions(); 67 90 : linesDeleted += fileDiff.deletions(); 68 90 : switch (fileDiff.changeType()) { 69 : case ADDED: 70 : case MODIFIED: 71 : case DELETED: 72 : case COPIED: 73 : case REWRITE: 74 90 : r.add( 75 90 : FilePathAdapter.getNewPath( 76 90 : fileDiff.oldPath(), fileDiff.newPath(), fileDiff.changeType())); 77 90 : break; 78 : 79 : case RENAMED: 80 10 : r.add(FilePathAdapter.getOldPath(fileDiff.oldPath(), fileDiff.changeType())); 81 10 : r.add( 82 10 : FilePathAdapter.getNewPath( 83 10 : fileDiff.oldPath(), fileDiff.newPath(), fileDiff.changeType())); 84 : break; 85 : } 86 90 : } 87 103 : return new DiffSummary(r.stream().sorted().toArray(String[]::new), linesInserted, linesDeleted); 88 : } 89 : }