Mercurial > hg > mercurial
view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgRepo.java @ 615:7ea82d063131 Gaya-8.0.x
TW-30589 fix create temp dir race condition
author | Dmitry Neverov <dmitry.neverov@gmail.com> |
---|---|
date | Thu, 01 Aug 2013 11:53:48 +0400 |
parents | 6c480513e5c2 |
children | d28d52336ce5 |
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial; import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*; import jetbrains.buildServer.log.Loggers; import jetbrains.buildServer.util.FileUtil; import jetbrains.buildServer.vcs.VcsException; import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; import java.util.*; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; import static jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil.deleteDir; import static jetbrains.buildServer.util.FileUtil.isEmptyDir; /** * @author dmitry.neverov */ public class HgRepo { protected final CommandSettingsFactory myCommandSettingsFactory; protected final File myWorkingDir; protected final String myHgPath; protected final AuthSettings myAuthSettings; protected final Map<String, Map<String, SubRepo>> mySubreposCache = new HashMap<String, Map<String, SubRepo>>(); public HgRepo(@NotNull CommandSettingsFactory commandSettingsFactory, @NotNull File workingDir, @NotNull String hgPath, @NotNull AuthSettings authSettings) { myCommandSettingsFactory = commandSettingsFactory; myWorkingDir = workingDir; myHgPath = hgPath; myAuthSettings = authSettings; } public PullCommand pull() { return new PullCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public PushCommand push() { return new PushCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public CloneCommand doClone() { return new CloneCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public IdentifyCommand id() { return new IdentifyCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public Init init() { return new Init(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public LogCommand log() { return new LogCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public UpdateCommand update() { return new UpdateCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public BranchesCommand branches() { return new BranchesCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public BookmarksCommand bookmarks() { return new BookmarksCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public StatusCommand status() { return new StatusCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public TagCommand tag() { return new TagCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public CatCommand cat() { return new CatCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public ArchiveCommand archive() { return new ArchiveCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings); } public VersionCommand version() { return new VersionCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir); } public ParentsCommand parents() { return new ParentsCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir); } public String path() { return myWorkingDir.getAbsolutePath(); } public File getWorkingDir() { return myWorkingDir; } public boolean isEmpty() { return isEmptyDir(myWorkingDir); } public void resetBookmarks() { File dotHg = new File(getWorkingDir(), ".hg"); FileUtil.delete(new File(dotHg, "bookmarks")); FileUtil.delete(new File(dotHg, "bookmarks.current")); } public String getHgPath() { return myHgPath; } @NotNull public List<String> listFiles(@NotNull String revision) throws VcsException { List<FileStatus> fileStatuses = status() .fromRevision(revision) .toRevision(revision) .hideStatus() .showAllFiles() .call(); List<String> files = new ArrayList<String>(fileStatuses.size()); for (FileStatus fileStatus : fileStatuses) files.add(fileStatus.getPath()); return files; } public String getWorkingDirRevision() throws VcsException { return id().inLocalRepository().call(); } public boolean containsRevision(@NotNull String revision) { return containsRevision(new ChangeSet(revision)); } public boolean containsRevision(@NotNull ChangeSet cset) { try { id().revision(cset).inLocalRepository().call(); return true; } catch (VcsException e) { return false; } } public boolean isValidRepository() { // need better way to check that repository copy is ok return myWorkingDir.isDirectory() && new File(myWorkingDir, ".hg").isDirectory(); } public void setDefaultPath(@NotNull String defaultPath) throws VcsException { try { File hgrc = new File(new File(myWorkingDir, ".hg"), "hgrc"); String content = "[paths]\ndefault = " + defaultPath; FileUtil.writeFileAndReportErrors(hgrc, content); } catch (IOException e) { throw new VcsException(e); } } public boolean hasSubreposAtRevision(@NotNull String revision) { return !getSubrepositories(new ChangeSet(revision)).isEmpty(); } public boolean hasSubreposAtRevision(@NotNull ChangeSet cset) { return !getSubrepositories(cset).isEmpty(); } public Map<String, SubRepo> getSubrepositories(@NotNull String revision) { return getSubrepositories(new ChangeSet(revision)); } public List<HgSubrepoConfigChange> getSubrepoConfigChanges(@NotNull String revision) throws VcsException { if (containsSubrepoConfigChange(revision)) { List<String> parents = parents().ofRevision(revision).call(); return getSubrepoConfigChanges(revision, parents); } return emptyList(); } public List<HgSubrepoConfigChange> getSubrepoConfigChanges(@NotNull ChangeSet cset) { if (containsSubrepoConfigChange(cset)) { List<String> parents = new ArrayList<String>(); for (ChangeSetRevision p : cset.getParents()) { parents.add(p.getId()); } return getSubrepoConfigChanges(cset.getId(), parents); } return emptyList(); } public List<HgSubrepoConfigChange> getSubrepoConfigChanges(@NotNull String fromRevision, @NotNull String toRevision) { return getSubrepoConfigChanges(toRevision, asList(fromRevision)); } public List<HgSubrepoConfigChange> getSubrepoConfigChanges(@NotNull String revision, @NotNull List<String> parentRevisions) { Map<String, SubRepo> curSubrepos = getSubrepositories(revision); List<Map<String, SubRepo>> prevSubrepos = new ArrayList<Map<String, SubRepo>>(); for (String parentRevision : parentRevisions) { prevSubrepos.add(getSubrepositories(parentRevision)); } return getSubrepoConfigChanges(prevSubrepos, curSubrepos); } private List<HgSubrepoConfigChange> getSubrepoConfigChanges(@NotNull List<Map<String, SubRepo>> prevSubrepos, @NotNull Map<String, SubRepo> curSubrepos) { List<HgSubrepoConfigChange> configChanges = new ArrayList<HgSubrepoConfigChange>(); for (Map.Entry<String, SubRepo> e : curSubrepos.entrySet()) { String path = e.getKey(); SubRepo curSubrepo = e.getValue(); List<SubRepo> prevs = new ArrayList<SubRepo>(); for (Map<String, SubRepo> prev : prevSubrepos) { SubRepo prevSubrepo = prev.remove(path); if (prevSubrepo == null) //no subrepo at this path in previous revision continue; if (prevSubrepo.equals(curSubrepo)) //subrepo configuration doesn't change since previous revision continue; prevs.add(prevSubrepo); } configChanges.add(new HgSubrepoConfigChange(e.getKey(), prevs, curSubrepo)); } for (Map<String, SubRepo> prev : prevSubrepos) { for (Map.Entry<String, SubRepo> e : prev.entrySet()) { configChanges.add(new HgSubrepoConfigChange(e.getKey(), e.getValue(), null)); } } return configChanges; } private boolean containsSubrepoConfigChange(@NotNull ChangeSet cset) { for (FileStatus f : cset.getModifiedFiles()) { if (containsSubrepoConfigChange(f)) return true; } return false; } private boolean containsSubrepoConfigChange(@NotNull String revision) throws VcsException { List<FileStatus> changedFiles = status() .fromRevision(revision) .toRevision(revision) .showAllFiles() .call(); for (FileStatus f : changedFiles) { if (containsSubrepoConfigChange(f)) return true; } return false; } private boolean containsSubrepoConfigChange(@NotNull FileStatus f) { return f.getPath().equals(".hgsubstate"); } //path->subrepo public Map<String, SubRepo> getSubrepositories(@NotNull ChangeSet cset) { String revId = cset.getId(); Map<String, SubRepo> subrepos = mySubreposCache.get(revId); if (subrepos != null) return new HashMap<String, SubRepo>(subrepos); CatCommand cc = cat(); cc.setRevId(revId); File catDir = null; try { catDir = cc.execute(asList(".hgsub", ".hgsubstate"), false); File hgsub = new File(catDir, ".hgsub"); File hgsubstate = new File(catDir, ".hgsubstate"); subrepos = readSubrepositories(hgsub, hgsubstate); mySubreposCache.put(revId, subrepos); return new HashMap<String, SubRepo>(subrepos); } catch (VcsException e) { return emptyMap(); } finally { deleteDir(catDir, Loggers.VCS); } } @Override public String toString() { return myWorkingDir.getAbsolutePath(); } private Map<String, SubRepo> readSubrepositories(@NotNull final File hgsub, @NotNull final File hgsubstate) { if (hgsub.exists() && hgsubstate.exists()) { try { Map<String, String> path2repo = readHgsub(hgsub); Map<String, String> path2revision = readHgsubstate(hgsubstate); Map<String, SubRepo> result = new HashMap<String, SubRepo>(); for (Map.Entry<String, String> entry : path2repo.entrySet()) { String path = entry.getKey(); String url = entry.getValue(); String revision = path2revision.get(path); if (revision != null) result.put(path, new SubRepo(path, url, revision)); } return result; } catch (IOException e) { return emptyMap(); } } else { return emptyMap(); } } /*returns map: relative path -> repository url */ private Map<String, String> readHgsub(@NotNull final File hgsub) throws IOException { Map<String, String> result = new HashMap<String, String>(); for (String line : FileUtil.readFile(hgsub)) { String[] parts = line.split(" = "); if (parts.length == 2) result.put(parts[0], parts[1]); } return result; } /*returns map: relative path -> revision */ private Map<String, String> readHgsubstate(@NotNull final File hgsubstate) throws IOException { Map<String, String> result = new HashMap<String, String>(); for (String line : FileUtil.readFile(hgsubstate)) { String[] parts = line.split(" "); if (parts.length == 2) result.put(parts[1], parts[0]); } return result; } }