hg 命令 http://blog.sina.com.cn/s/blog_53b7ddf00101jzrf.html
http://hg4j.com/
https://code.google.com/p/hg4j/source/checkout
import java.io.File;
import java.util.ArrayList;
import org.tmatesoft.hg.core.HgCheckoutCommand;
import org.tmatesoft.hg.core.HgCloneCommand;
import org.tmatesoft.hg.core.HgInitCommand;
import org.tmatesoft.hg.core.HgPullCommand;
import org.tmatesoft.hg.core.HgPushCommand;
import org.tmatesoft.hg.internal.FileUtils;
import org.tmatesoft.hg.internal.RepoInitializer;
import org.tmatesoft.hg.repo.HgLookup;
import org.tmatesoft.hg.repo.HgRemoteRepository;
import org.tmatesoft.hg.repo.HgRepository;
import com.srvctmpl.util.DataBaseException;
public class HgHandler {
private String hgUri = "";
private static File destLoc = null;
private HgRemoteRepository hgRemote = null;
private static HgRepository dstRepo = null;
private static HgLookup lookup = new HgLookup();
public HgHandler(String hgUri, String destDir) {
this.hgUri = hgUri;
this.destLoc = new File(destDir);
}
// 初始化 .hg 仓库
public HgRepository repositoryInit() {
try {
if (destLoc.exists()) {
FileUtils.rmdir(destLoc);
}
File parentDir = new File(destLoc.getParent());
new HgInitCommand().location(parentDir).revlogV1().dotencode(false).fncache(false).execute();
new RepoInitializer().initRequiresFromFile(new File(parentDir, ".hg")).getRequires();
hgRemote = lookup.detectRemote(hgUri, null);
dstRepo = lookup.detect(destLoc);
HgCloneCommand cmd = new HgCloneCommand();
return cmd.source(hgRemote).destination(destLoc).execute();
} catch (Exception e) {
throw new DataBaseException("hg init Repository 错误", e.getCause());
}
}
// hg pull ,返回pull的资源数目
public int hgPull() {
try {
hgRemote = lookup.detectRemote(hgUri, null);
dstRepo = lookup.detect(destLoc);
HgPullCommand cmd = new HgPullCommand(dstRepo);
cmd.source(hgRemote).execute();
// new HgIncomingCommand(dstRepo).against(hgRemote).executeLite();
return cmd.getPulledRevisions().size();
} catch (Exception e) {
throw new DataBaseException("hg Pull错误", e.getCause());
}
}
// hg update ,返回 update资源的数目
public int hgUpdate() {
try {
dstRepo = lookup.detect(destLoc);
new HgCheckoutCommand(dstRepo).clean(true).execute();
return destLoc.listFiles().length;
} catch (Exception e) {
throw new DataBaseException("hg Update错误", e.getCause());
}
}
// hg clone ,返回 更新数目
public int hgClone() {
HgRepository repository = repositoryInit();
if (!repository.isInvalid()) {
hgPull();
return hgUpdate();
} else {
throw new DataBaseException("hg Clone 本地创建仓库无效");
}
}
// hg commit -m “head信息” 。 当成功执行 返回1
//true outCome is OK, commit well,outCome no to add is not file to commiit,others are file
public int hgCommit() {
try {
/*
dstRepo = lookup.detect(destLoc);
HgCommitCommand cmd = new HgCommitCommand(dstRepo).message("files update");
return cmd.execute();
*/
hgRemote = lookup.detectRemote(hgUri, null);
ArrayList<String> cmd = new ArrayList<String>();
cmd.add("hg");
cmd.add("commit");
cmd.add("--addremove");
cmd.add("-m");
cmd.add(hgRemote.heads().toString());
ExecHelper eh = new ExecHelper(new OutputParser.Stub(), destLoc);
eh.run(cmd);
return eh.getExitValue();
} catch (Exception e) {
throw new DataBaseException("hg Commit错误", e.getCause());
}
}
// hg push,返回 push结果的数目
public int hgPush() {
try {
hgRemote = lookup.detectRemote(hgUri, null);
dstRepo = lookup.detect(destLoc);
HgPushCommand cmd = new HgPushCommand(dstRepo);
cmd.destination(hgRemote).execute();
return cmd.getPushedRevisions().size();
} catch (Exception e) {
throw new DataBaseException("hg Push错误", e.getCause());
}
}
}