1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.indexer.history; 24 25 import org.opengrok.indexer.util.Executor; 26 27 import java.io.BufferedReader; 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.io.InputStreamReader; 32 import java.util.function.Consumer; 33 34 class MercurialHistoryParserRevisionsOnly implements Executor.StreamHandler { 35 private final MercurialRepository repository; 36 private final Consumer<String> visitor; 37 MercurialHistoryParserRevisionsOnly(MercurialRepository repository, Consumer<String> visitor)38 MercurialHistoryParserRevisionsOnly(MercurialRepository repository, Consumer<String> visitor) { 39 this.repository = repository; 40 this.visitor = visitor; 41 } 42 parse(File file, String sinceRevision)43 void parse(File file, String sinceRevision) throws HistoryException { 44 try { 45 Executor executor = repository.getHistoryLogExecutor(file, sinceRevision, null, true); 46 int status = executor.exec(true, this); 47 48 if (status != 0) { 49 throw new HistoryException( 50 String.format("Failed to get revisions for: \"%s\" since revision %s Exit code: %d", 51 file.getAbsolutePath(), sinceRevision, status)); 52 } 53 } catch (IOException e) { 54 throw new HistoryException("Failed to get history for: \"" + 55 file.getAbsolutePath() + "\"", e); 56 } 57 } 58 59 @Override processStream(InputStream input)60 public void processStream(InputStream input) throws IOException { 61 try (BufferedReader in = new BufferedReader(new InputStreamReader(input))) { 62 String s; 63 while ((s = in.readLine()) != null) { 64 visitor.accept(s); 65 } 66 } 67 } 68 } 69