/*
* #%L
* JavaHg
* %%
* Copyright (C) 2011 aragost Trifork ag
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
package com.aragost.javahg;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.aragost.javahg.commands.HeadsCommand;
import com.aragost.javahg.commands.LogCommand;
import com.aragost.javahg.internals.GenericCommand;
import com.aragost.javahg.internals.PhaseLogCommand;
import com.aragost.javahg.internals.Server;
import com.aragost.javahg.internals.Utils;
/**
* A Mercurial repository.
*/
public abstract class Repository {
/**
* Open an existing Mercurial repository.
*
* @param mercurialRepository
* path to the local repository.
* @return the repository.
* @throws IOException
*/
public static BaseRepository open(RepositoryConfiguration conf, File mercurialRepository) {
return new BaseRepository(conf, mercurialRepository, false, null);
}
/**
* Create a new Mercurial repository and open a javahg Repository
* on it.
*
* @param directory
* the local destination path of the clone
* @return the newly created repository.
*/
public static BaseRepository create(RepositoryConfiguration conf, File directory) {
return new BaseRepository(conf, directory, true, null);
}
/**
* Clone an existing Mercurial repository.
*
* @param directory
* the local destination path of the clone
* @param otherRepoUrl
* the (possible remote) repository to clone.
* @return the newly cloned repository.
*/
public static BaseRepository clone(RepositoryConfiguration conf, File directory, String otherRepoUrl) {
return new BaseRepository(conf, directory, false, otherRepoUrl);
}
/**
* Open an existing Mercurial repository. This uses the default
* Mercurial binary from {@link RepositoryConfiguration#DEFAULT}.
*
* @param mercurialRepository
* the local path to the repository.
* @return the repository.
*/
public static BaseRepository open(File mercurialRepository) {
return new BaseRepository(RepositoryConfiguration.DEFAULT, mercurialRepository, false, null);
}
/**
* Create a new Mercurial repository. This uses the default
* Mercurial binary from {@link RepositoryConfiguration#DEFAULT}.
*
* @param directory
* the local destination path of the clone
* @return the newly created repository.
*/
public static BaseRepository create(File directory) {
return new BaseRepository(RepositoryConfiguration.DEFAULT, directory, true, null);
}
/**
* Clone an existing Mercurial repository. This uses the default
* Mercurial binary from {@link RepositoryConfiguration#DEFAULT}.
*
* @param directory
* the local destination path of the clone
* @param otherRepoUrl
* the (possible remote) repository to clone.
* @return the newly cloned repository.
*/
public static BaseRepository clone(File directory, String otherRepoUrl) {
return new BaseRepository(RepositoryConfiguration.DEFAULT, directory, false, otherRepoUrl);
}
/**
* @deprecated Use changeset instead.
*
* @param node
* a changeset ID, must be 40 hexadecimal characters.
* @return the changeset
*/
@Deprecated
public final Changeset changeSet(String node) {
return changeset(node);
}
/**
* @param node
* a changeset ID, must be 40 hexadecimal characters.
* @return the changeset
*/
public abstract Changeset changeset(String node);
/**
* @return the command server associated with this repository.
*/
public abstract Server getServer();
/**
* Clone the repository. This also stops the command server
* associated with the repository.
*/
public void close() {
getServer().decrementRefCount();
}
/**
* @return the root directory of this repository.
*/
public abstract File getDirectory();
public abstract BaseRepository getBaseRepository();
@Override
public String toString() {
return "repo@" + getDirectory();
}
/**
* Add repository specific arguments to the hg command line for
* command execution
*
* @param commandLine
*/
public void addToCommandLine(List<String> commandLine) {
}
/**
*
* @return The version string for the Mercurial server
* @throws IOException
*/
public String getHgVersion() {
return getServer().getHgVersion(this);
}
/**
* Create a new WorkingCopy object for this repository.
*
* @return a working copy object for this repository
*/
public WorkingCopy workingCopy() {
return new WorkingCopy(this);
}
/**
*
* @return heads for the repository
*/
public List<Changeset> heads() {
return HeadsCommand.on(this).execute();
}
public Map<Changeset, Phase> readPhases(String... revs) {
PhaseLogCommand cmd = new PhaseLogCommand(this);
cmd.rev(revs);
return cmd.execute();
}
/**
*
* @return the tip Changeset for the repository
*/
public Changeset tip() {
List<Changeset> changesets = LogCommand.on(this).rev("tip").execute();
return Utils.single(changesets);
}
public File relativeFile(File file) {
if (file.isAbsolute()) {
String filePath = Utils.resolveSymlinks(file).getPath();
String repoPath = getDirectory().getPath() + File.separator;
if (filePath.startsWith(repoPath)) {
return new File(filePath.substring(repoPath.length()));
} else {
throw new IllegalArgumentException("" + file + " is not under root of repository");
}
} else {
return file;
}
}
/**
* Return a File object for the file name specified in this
* repository.
*
* @param name
* @return
*/
public File file(String name) {
return new File(getDirectory(), name);
}
public void lock() {
GenericCommand lock = new GenericCommand(this, "javahglock");
lock.execute();
}
public void unlock() {
GenericCommand unlock = new GenericCommand(this, "javahgunlock");
unlock.execute();
}
}