322N/A/*
322N/A * CDDL HEADER START
322N/A *
322N/A * The contents of this file are subject to the terms of the
322N/A * Common Development and Distribution License (the "License").
322N/A * You may not use this file except in compliance with the License.
322N/A *
322N/A * See LICENSE.txt included in this distribution for the specific
322N/A * language governing permissions and limitations under the License.
322N/A *
322N/A * When distributing Covered Code, include this CDDL HEADER in each
322N/A * file and include the License file at LICENSE.txt.
322N/A * If applicable, add the following below this CDDL HEADER, with the
322N/A * fields enclosed by brackets "[]" replaced with your own identifying
322N/A * information: Portions Copyright [yyyy] [name of copyright owner]
322N/A *
322N/A * CDDL HEADER END
322N/A */
322N/A
322N/A/*
322N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
322N/A * Use is subject to license terms.
322N/A */
322N/A
322N/Apackage org.opensolaris.opengrok.history;
322N/A
322N/Aimport java.io.File;
322N/Aimport java.io.IOException;
322N/Aimport java.io.InputStream;
416N/Aimport java.util.logging.Level;
1327N/Aimport java.util.logging.Logger;
1327N/A
459N/Aimport org.apache.commons.jrcs.diff.PatchFailedException;
324N/Aimport org.apache.commons.jrcs.rcs.Archive;
459N/Aimport org.apache.commons.jrcs.rcs.InvalidFileFormatException;
324N/Aimport org.apache.commons.jrcs.rcs.Node;
459N/Aimport org.apache.commons.jrcs.rcs.ParseException;
324N/Aimport org.apache.commons.jrcs.rcs.Version;
322N/A
322N/A/**
322N/A * Access to an RCS repository.
322N/A */
322N/Apublic class RCSRepository extends Repository {
1473N/A private static final Logger logger =
1473N/A Logger.getLogger(RCSRepository.class.getName());
815N/A private static final long serialVersionUID = 1L;
322N/A
1473N/A /**
1473N/A * Create a new instance of type {@code RCS}.
1473N/A */
664N/A public RCSRepository() {
1182N/A working = Boolean.TRUE;
664N/A type = "RCS";
664N/A }
664N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
322N/A @Override
1473N/A public boolean fileHasHistory(File file) {
323N/A return getRCSFile(file) != null;
322N/A }
322N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
322N/A @Override
1473N/A public InputStream getHistoryGet(String parent, String basename, String rev)
1473N/A {
322N/A try {
557N/A File file = new File(parent, basename);
557N/A File rcsFile = getRCSFile(file);
557N/A return new RCSget(rcsFile.getPath(), rev);
322N/A } catch (IOException ioe) {
1327N/A logger.warning("Failed to retrieve revision '" + rev + "' of '"
1327N/A + basename + "': " + ioe.getMessage());
1327N/A logger.log(Level.FINE, "getHistoryGet", ioe);
322N/A return null;
322N/A }
322N/A }
322N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
322N/A @Override
1473N/A public boolean fileHasAnnotation(File file) {
324N/A return fileHasHistory(file);
322N/A }
322N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
322N/A @Override
1473N/A protected Annotation annotate(File file, String revision) throws IOException {
325N/A File rcsFile = getRCSFile(file);
325N/A return rcsFile == null ? null : annotate(file, revision, rcsFile);
325N/A }
325N/A
325N/A static Annotation annotate(File file, String revision, File rcsFile)
459N/A throws IOException {
459N/A try {
459N/A Archive archive = new Archive(rcsFile.getPath());
459N/A // If revision is null, use current revision
1473N/A Version version = revision == null
1473N/A ? archive.getRevisionVersion()
1473N/A : archive.getRevisionVersion(revision);
459N/A // Get the revision with annotation
459N/A archive.getRevision(version, true);
459N/A Annotation a = new Annotation(file.getName());
459N/A // A comment in Archive.getRevisionNodes() says that it is not
459N/A // considered public API anymore, but it works.
459N/A for (Node n : archive.getRevisionNodes()) {
459N/A String rev = n.getVersion().toString();
459N/A String author = n.getAuthor();
1384N/A a.addLine(rev, author);
459N/A }
459N/A return a;
459N/A } catch (ParseException pe) {
1473N/A throw new IOException("Parse exception annotating RCS repository: "
1473N/A + pe.getMessage(), pe);
459N/A } catch (InvalidFileFormatException iffe) {
1473N/A throw new IOException("File format exception annotating RCS repository: "
1473N/A + iffe.getMessage(), iffe);
459N/A } catch (PatchFailedException pfe) {
1473N/A throw new IOException("Patch failed exception annotating RCS repository: "
1473N/A + pfe.getMessage(), pfe);
324N/A }
322N/A }
322N/A
322N/A @Override
643N/A void update() throws IOException {
1182N/A throw new IOException("Not supported yet.");
322N/A }
322N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
322N/A @Override
1473N/A public boolean isRepositoryFor(File file) {
322N/A File rcsDir = new File(file, "RCS");
322N/A return rcsDir.isDirectory();
322N/A }
322N/A
322N/A /**
322N/A * Get a {@code File} object that points to the file that contains
322N/A * RCS history for the specified file.
322N/A *
322N/A * @param file the file whose corresponding RCS file should be found
323N/A * @return the file which contains the RCS history, or {@code null} if it
323N/A * cannot be found
322N/A */
1473N/A @SuppressWarnings("static-method")
1473N/A protected File getRCSFile(File file) {
322N/A File dir = new File(file.getParentFile(), "RCS");
322N/A String baseName = file.getName();
323N/A File rcsFile = new File(dir, baseName + ",v");
323N/A return rcsFile.exists() ? rcsFile : null;
322N/A }
765N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
765N/A @Override
1473N/A public boolean hasHistoryForDirectories() {
765N/A return false;
765N/A }
765N/A
1473N/A /**
1473N/A * {@inheritDoc}
1473N/A */
765N/A @Override
1473N/A public History getHistory(File file) throws HistoryException {
1473N/A return new RCSHistoryParser().parse(file);
765N/A }
322N/A}