    001package org.vishia.vcs;
    002
    003import java.io.File;
    004import java.io.FileNotFoundException;
    005import java.io.IOException;
    006import java.util.Map;
    007
    008import org.vishia.util.DataAccess;
    009import org.vishia.util.FileSystem;
    010import org.vishia.util.StringPartScan;
    011
    012public class Bzr
    013{
    014  /**Searches the repository starting from startDir in outer direction.
    015   * If the startDir contains a ".bzr" directory, it is the repository.
    016   * If any parent dir contains it, bzrdir will be set to the parent dir.
    017   * If startDir or any parent contains a file ".bzr.bat" and does not contain a ".bzr" dir,
    018   * that file is read in to find out where the repository is located.
    019   * It is the line which contains "bzr_mvExpl.bat BZRDIR".
    020   * The BZRDIR should be relative to a system width defined location of all repositories.
    021   * This routine does not deal with them, only returns the relativ path.
    022   *
    023   *
    024   * @param startDir The start directory where to find the ".bzr"
    025   * @param dst A map which contains variables, the result will be put into.
    026   * @param bzrdir Name of the variable for dst to put the absolute path of the ".bzr" or ".bzr.bat"-location
    027   * @param bzrsrc Name of the variable for dst to put the relativ ".bzr" location find out in the ".bzr.bat".
    028   *   This variable is put with "$" if a ".bzr" is found in bzrdir.
    029   * @return null if success, an error message if ".bzr" or ".bzr.bat" was not found.
    030   * @throws IOException on any unexpected exception.
    031   * @throws IllegalAccessException
    032   */
    033  public static String searchRepository(File startDir, Map<String, DataAccess.Variable<Object>> dst, String bzrdir, String bzrsrc)
    034  throws IOException, IllegalAccessException
    035  { File fBzr;
    036    File currDir = startDir.isDirectory() ? startDir : startDir.getParentFile();
    037    String ret = null;
    038    //search whether a .bzr or .bzr.bat exists and change to parent dir till it is found.
    039    do{
    040      fBzr = new File(currDir, ".bzr.bat");
    041      if(!fBzr.exists()){
    042        fBzr = new File(currDir, "_bzr.bat");
    043        if(!fBzr.exists()){
    044          fBzr = new File(currDir, ".bzr");
    045          if(!fBzr.exists()){
    046            try{
    047              currDir = FileSystem.getDirectory(currDir);  //NOTE: currDir.getParent() is not successfully on relative dir "."
    048            } catch(FileNotFoundException exc){ currDir = null;}
    049          }
    050        }
    051      }
    052    } while(!fBzr.exists() && currDir !=null);
    053    if(currDir ==null){
    054      throw new IOException("Bzr.searchRepository - .bzr... not found ;" + startDir.getAbsolutePath());
    055    } else {
    056      String sBzrDir = FileSystem.getCanonicalPath(currDir);
    057      DataAccess.createOrReplaceVariable(dst, bzrdir, 'S', sBzrDir, true);
    058      //dst.put(bzrdir, sBzrDir);
    059      if(!fBzr.getName().equals(".bzr")){   //one of the batch files found
    060        ret = FileSystem.readFile(fBzr);
    061        String sLine = FileSystem.grep1line(fBzr, "bzr_mvExpl.bat");
    062        if(sLine !=null){
    063          int pos = sLine.indexOf("bzr_mvExpl.bat");
    064          String sBzrSrc = sLine.substring(pos + 15).trim();
    065          DataAccess.createOrReplaceVariable(dst, bzrsrc, 'S', sBzrSrc, true);
    066          //dst.put(bzrsrc, sBzrSrc);
    067        }
    068        if(sLine == null) {
    069          throw new IOException("Bzr.searchRepository - .bzr.bat found but does not contain \"bzr_mvExpl.bat\"");
    070        }
    071      } else {
    072        dst.put(bzrsrc, null);
    073      }
    074    }
    075    return ret;
    076  }
    077}