try {
final ProgressSupport progress = getProgressSupport(null);
final CancelSupport cancellation = getCancelSupport(null, true);
cancellation.checkCancelled();
progress.start(6);
Internals internalRepo = Internals.getInstance(repo);
if (cleanCheckout) {
// remove tracked files from wd (perhaps, just forget 'Added'?)
// for now, just delete each and every tracked file
// TODO WorkingCopy container with getFile(HgDataFile/Path) to access files in WD
HgDirstate dirstate = new HgInternals(repo).getDirstate();
dirstate.walk(new HgDirstate.Inspector() {
public boolean next(EntryKind kind, Record entry) {
File f = new File(repo.getWorkingDir(), entry.name().toString());
if (f.exists()) {
f.delete();
}
return true;
}
});
} else {
throw new HgBadArgumentException("Sorry, only clean checkout is supported now, use #clean(true)", null);
}
progress.worked(1);
cancellation.checkCancelled();
final DirstateBuilder dirstateBuilder = new DirstateBuilder(internalRepo);
final CheckoutWorker worker = new CheckoutWorker(internalRepo);
HgManifest.Inspector insp = new HgManifest.Inspector() {
public boolean next(Nodeid nid, Path fname, Flags flags) {
if (worker.next(nid, fname, flags)) {
// Mercurial seems to write "n 0 -1 unset fname" on `hg --clean co -rev <earlier rev>`
// and the reason for 'force lookup' I suspect is a slight chance of simultaneous modification
// of the file by user that doesn't alter its size the very second dirstate is being written
// (or the file is being updated and the update brought in changes that didn't alter the file size -
// with size and timestamp set, later `hg status` won't notice these changes)
// However, as long as we use this class to write clean copies of the files, we can put all the fields
// right away.
int mtime = worker.getLastFileModificationTime();
// Manifest flags are chars (despite octal values `hg manifest --debug` displays),
// while dirstate keeps actual unix flags.
int fmode = worker.getLastFileMode();
dirstateBuilder.recordNormal(fname, fmode, mtime, worker.getLastFileSize());
return true;
}
return false;
}
public boolean end(int manifestRevision) {
return false;
}
public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
return true;
}
};
// checkout tip if no revision set
final int coRevision = revisionToCheckout.get(HgRepository.TIP);
dirstateBuilder.parents(repo.getChangelog().getRevision(coRevision), null);
repo.getManifest().walk(coRevision, coRevision, insp);
worker.checkFailed();
progress.worked(3);
cancellation.checkCancelled();
File dirstateFile = internalRepo.getRepositoryFile(Dirstate);
try {
FileChannel dirstateFileChannel = new FileOutputStream(dirstateFile).getChannel();
dirstateBuilder.serialize(dirstateFileChannel);
dirstateFileChannel.close();
} catch (IOException ex) {
throw new HgIOException("Can't write down new directory state", ex, dirstateFile);
}
progress.worked(1);
cancellation.checkCancelled();
String branchName = repo.getChangelog().range(coRevision, coRevision).get(0).branch();
assert branchName != null;
File branchFile = internalRepo.getRepositoryFile(Branch);
if (HgRepository.DEFAULT_BRANCH_NAME.equals(branchName)) {
// clean actual branch, if any
if (branchFile.isFile()) {
branchFile.delete();
}