1376N/A/*
1376N/A * CDDL HEADER START
1376N/A *
1376N/A * The contents of this file are subject to the terms of the
1376N/A * Common Development and Distribution License (the "License").
1376N/A * You may not use this file except in compliance with the License.
1376N/A *
1376N/A * See LICENSE.txt included in this distribution for the specific
1376N/A * language governing permissions and limitations under the License.
1376N/A *
1376N/A * When distributing Covered Code, include this CDDL HEADER in each
1376N/A * file and include the License file at LICENSE.txt.
1376N/A * If applicable, add the following below this CDDL HEADER, with the
1376N/A * fields enclosed by brackets "[]" replaced with your own identifying
1376N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1376N/A *
1376N/A * CDDL HEADER END
1376N/A */
1376N/A
1376N/A/*
1376N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1376N/A */
1376N/Apackage org.opensolaris.opengrok.history;
1376N/A
1376N/Aimport java.io.BufferedReader;
1376N/Aimport java.io.ByteArrayInputStream;
1376N/Aimport java.io.File;
1376N/Aimport java.io.IOException;
1376N/Aimport java.io.InputStream;
1376N/Aimport java.util.ArrayList;
1376N/Aimport java.util.logging.Level;
1376N/Aimport java.util.regex.Matcher;
1376N/Aimport java.util.regex.Pattern;
1376N/Aimport org.opensolaris.opengrok.OpenGrokLogger;
1377N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1376N/Aimport org.opensolaris.opengrok.util.Executor;
1376N/Aimport org.opensolaris.opengrok.util.IOUtils;
1376N/A
1376N/A/**
1376N/A * Access to an AccuRev repository (here an actual user workspace)
1376N/A *
1377N/A * AccuRev requires that a user logs into their system before it can be used. So
1377N/A * on the machine acting as the OpenGrok server, some valid user has to be
1377N/A * permanently logged in. (accurev login -n <user>)
1376N/A *
1377N/A * It appears that the file path that is given to all these methods is the
1377N/A * complete path to the file which includes the path to the root of the source
1377N/A * location. This means that when using the -P option of OpenGrok to make all
1377N/A * the directories pointed to by the source root to be seen as separate projects
1377N/A * is not all as it would seem. The History GURU always starts building the
1377N/A * history cache using the source root. Well there is NO HISTORY for anything at
1377N/A * the source root because it is not part of an actual AccuRev depot. The
1377N/A * directories within the source root directory represent the work areas of
1377N/A * AccuRev and it is those areas where history can be obtained. This
1377N/A * implementation allows those directories to be symbolic links to the actual
1377N/A * workspaces.
1376N/A *
1376N/A * Other assumptions:
1376N/A *
1377N/A * There is only one associated AccuRev depot associated with workspaces.
1376N/A *
1376N/A * @author Steven Haehn
1376N/A */
1376N/Apublic class AccuRevRepository extends Repository {
1376N/A
1376N/A private static final long serialVersionUID = 1L;
1377N/A /**
1377N/A * The property name used to obtain the client command for this repository.
1377N/A */
1376N/A public static final String CMD_PROPERTY_KEY =
1377N/A "org.opensolaris.opengrok.history.AccuRev";
1377N/A /**
1377N/A * The command to use to access the repository if none was given explicitly
1377N/A */
1376N/A public static final String CMD_FALLBACK = "accurev";
1377N/A private static final Pattern annotationPattern =
1377N/A Pattern.compile("^\\s+(\\d+/\\d+)\\s+(\\w+)"); // version, user
1377N/A private static final Pattern depotPattern =
1377N/A Pattern.compile("^Depot:\\s+(\\w+)");
1377N/A private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1376N/A
1376N/A public AccuRevRepository() {
1376N/A type = "AccuRev";
1376N/A datePattern = "yyyy/MM/dd hh:mm:ss";
1377N/A ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
1376N/A }
1376N/A
1376N/A @Override
1376N/A public Annotation annotate(File file, String rev) throws IOException {
1376N/A
1377N/A Annotation a = new Annotation(file.getName());
1376N/A
1376N/A ArrayList<String> cmd = new ArrayList<String>();
1377N/A
1377N/A /*
1377N/A * ----------------------------------------------- Strip off source root
1377N/A * to get to workspace path.
1377N/A *-----------------------------------------------
1377N/A */
1377N/A String path = getDepotRelativePath(file);
1377N/A
1376N/A cmd.add(this.cmd);
1376N/A cmd.add("annotate");
1376N/A cmd.add("-fvu"); // version & user
1377N/A
1377N/A if (rev != null) {
1377N/A cmd.add("-v");
1377N/A cmd.add(rev.trim());
1376N/A }
1377N/A
1377N/A cmd.add(path);
1376N/A
1376N/A Executor executor = new Executor(cmd, file.getParentFile());
1376N/A executor.exec();
1376N/A
1376N/A BufferedReader reader = new BufferedReader(executor.getOutputReader());
1376N/A String line;
1376N/A int lineno = 0;
1376N/A try {
1376N/A while ((line = reader.readLine()) != null) {
1376N/A ++lineno;
1376N/A Matcher matcher = annotationPattern.matcher(line);
1377N/A
1376N/A if (matcher.find()) {
1376N/A String version = matcher.group(1);
1377N/A String author = matcher.group(2);
1376N/A a.addLine(version, author, true);
1376N/A } else {
1376N/A OpenGrokLogger.getLogger().log(Level.SEVERE,
1377N/A "Did not find annotation in line "
1377N/A + lineno + ": [" + line + "]");
1376N/A }
1376N/A }
1376N/A } catch (IOException e) {
1376N/A OpenGrokLogger.getLogger().log(Level.SEVERE,
1376N/A "Could not read annotations for " + file, e);
1376N/A }
1377N/A
1376N/A IOUtils.close(reader);
1376N/A return a;
1376N/A }
1376N/A
1376N/A /**
1377N/A * Get an executor to be used for retrieving the history log for the given
1377N/A * file. (used by AccuRevHistoryParser).
1376N/A *
1376N/A * @param file file for which history is to be retrieved.
1376N/A * @return An Executor ready to be started
1376N/A */
1377N/A Executor getHistoryLogExecutor(File file) throws IOException {
1376N/A
1377N/A /*
1377N/A * ----------------------------------------------- Strip off source root
1377N/A * to get to workspace path.
1377N/A *-----------------------------------------------
1377N/A */
1377N/A String path = getDepotRelativePath(file);
1376N/A
1376N/A ArrayList<String> cmd = new ArrayList<String>();
1377N/A
1377N/A cmd.add(this.cmd);
1377N/A cmd.add("hist");
1377N/A
1377N/A if (!file.isDirectory()) {
1377N/A cmd.add("-k");
1377N/A cmd.add("keep"); // get a list of all 'real' file versions
1376N/A }
1376N/A
1377N/A cmd.add(path);
1377N/A
1377N/A return new Executor(cmd, file.getParentFile());
1376N/A }
1376N/A
1376N/A @Override
1376N/A InputStream getHistoryGet(String parent, String basename, String rev) {
1376N/A
1376N/A ArrayList<String> cmd = new ArrayList<String>();
1376N/A InputStream inputStream = null;
1377N/A File directory = new File(parent);
1377N/A
1377N/A /*
1377N/A * ----------------------------------------------------------------- The
1377N/A * only way to guarantee getting the contents of a file is to fire off
1377N/A * an AccuRev 'stat'us command to get the element ID number for the
1377N/A * subsequent 'cat' command. (Element ID's are unique for a file, unless
1377N/A * evil twins are present) This is because it is possible that the file
1377N/A * may have been moved to a different place in the depot. The 'stat'
1377N/A * command will produce a line with the format:
1377N/A *
1377N/A * <filePath> <elementID> <virtualVersion> (<realVersion>) (<status>)
1377N/A *
1377N/A * /./myFile e:17715 CP.73_Depot/2 (3220/2) (backed)
1377N/A *-----------------------------------------------------------------
1377N/A */
1377N/A cmd.add(this.cmd);
1377N/A cmd.add("stat");
1377N/A cmd.add("-fe");
1377N/A cmd.add(basename);
1377N/A Executor executor = new Executor(cmd, directory);
1376N/A executor.exec();
1376N/A
1377N/A BufferedReader info = new BufferedReader(executor.getOutputReader());
1376N/A String elementID = null;
1376N/A String line;
1376N/A
1376N/A try {
1376N/A line = info.readLine();
1377N/A
1377N/A String[] statInfo = line.split("\\s+");
1377N/A elementID = statInfo[1].substring(2); // skip over 'e:'
1377N/A
1377N/A } catch (IOException e) {
1377N/A OpenGrokLogger.getLogger().log(Level.SEVERE,
1377N/A "Could not obtain status for " + basename);
1376N/A }
1377N/A
1377N/A if (elementID != null) {
1377N/A /*
1377N/A * ------------------------------------------ This really gets the
1377N/A * contents of the file.
1377N/A *------------------------------------------
1377N/A */
1376N/A cmd.clear();
1377N/A cmd.add(this.cmd);
1377N/A cmd.add("cat");
1377N/A cmd.add("-v");
1377N/A cmd.add(rev.trim());
1377N/A cmd.add("-e");
1377N/A cmd.add(elementID);
1377N/A
1377N/A executor = new Executor(cmd, directory);
1376N/A executor.exec();
1377N/A
1377N/A inputStream =
1377N/A new ByteArrayInputStream(executor.getOutputString().getBytes());
1376N/A }
1377N/A
1376N/A return inputStream;
1376N/A }
1377N/A
1376N/A @Override
1376N/A public void update() throws IOException {
1377N/A throw new UnsupportedOperationException("Not supported yet.");
1376N/A }
1376N/A
1376N/A @Override
1376N/A boolean fileHasHistory(File file) {
1376N/A return true;
1376N/A }
1376N/A
1376N/A @Override
1376N/A public boolean fileHasAnnotation(File file) {
1376N/A return true;
1376N/A }
1377N/A
1376N/A /**
1376N/A * Check if a given path is associated with an AccuRev workspace
1376N/A *
1377N/A * The AccuRev 'info' command provides a Depot name when in a known
1377N/A * workspace. Otherwise, the Depot name will be missing.
1376N/A *
1376N/A * @param path The presumed path to an AccuRev workspace directory.
1376N/A * @return true if the given path is in the depot, false otherwise
1376N/A */
1377N/A private boolean isInAccuRevDepot(File wsPath) {
1377N/A
1376N/A boolean status = false;
1377N/A
1376N/A if (isWorking()) {
1376N/A ArrayList<String> cmd = new ArrayList<String>();
1376N/A
1376N/A cmd.add(this.cmd);
1377N/A cmd.add("info");
1376N/A
1377N/A Executor executor = new Executor(cmd, wsPath);
1377N/A executor.exec(true);
1376N/A
1377N/A BufferedReader info = new BufferedReader(executor.getOutputReader());
1376N/A String line;
1376N/A
1376N/A try {
1377N/A while ((line = info.readLine()) != null) {
1376N/A
1377N/A Matcher depotMatch = depotPattern.matcher(line);
1376N/A
1377N/A if (line.indexOf("not logged in") != -1) {
1377N/A OpenGrokLogger.getLogger().log(
1377N/A Level.SEVERE, "Not logged into AccuRev server");
1376N/A break;
1376N/A }
1377N/A
1377N/A if (depotMatch.find()) {
1376N/A status = true;
1376N/A break;
1376N/A }
1376N/A }
1377N/A } catch (IOException e) {
1377N/A OpenGrokLogger.getLogger().log(Level.SEVERE,
1377N/A "Could not find AccuRev repository for " + wsPath);
1376N/A }
1376N/A }
1376N/A
1376N/A return status;
1376N/A }
1376N/A
1377N/A public String getDepotRelativePath(File file) {
1377N/A
1376N/A String path = "/./";
1377N/A
1376N/A try {
1377N/A path = env.getPathRelativeToSourceRoot(file, 0);
1377N/A
1377N/A if (path.startsWith("/")) {
1376N/A path = "/." + path;
1377N/A } else {
1376N/A path = "/./" + path;
1376N/A }
1377N/A } catch (IOException e) {
1377N/A OpenGrokLogger.getLogger().log(Level.WARNING,
1377N/A "Unable to determine depot relative path for " + file.getPath());
1376N/A }
1377N/A
1376N/A return path;
1376N/A }
1377N/A
1376N/A @Override
1377N/A boolean isRepositoryFor(File sourceHome) {
1377N/A
1377N/A return isInAccuRevDepot(sourceHome);
1376N/A }
1376N/A
1376N/A @Override
1376N/A public boolean isWorking() {
1377N/A
1376N/A if (working == null) {
1376N/A
1377N/A working = checkCmd(cmd, "info");
1376N/A }
1377N/A
1376N/A return working.booleanValue();
1376N/A }
1376N/A
1376N/A @Override
1376N/A boolean hasHistoryForDirectories() {
1376N/A return true;
1376N/A }
1376N/A
1376N/A @Override
1376N/A History getHistory(File file) throws HistoryException {
1376N/A return new AccuRevHistoryParser().parse(file, this);
1376N/A }
1376N/A}