/**
  * If necessary create a new task. Otherwise return the current task.
  *
  * @param logger a logger.
  * @param synopsis short description of task.
  * @param release release.
  * @param defaultTask should this task become the default task?
  * @param ccmAddr current Synergy session ID. Used to run in multi-session.
  * @return Task number
  * @throws ScmException
  */
 public int createTask(
     ScmLogger logger, String synopsis, String release, boolean defaultTask, String ccmAddr)
     throws ScmException {
   if (logger.isDebugEnabled()) {
     logger.debug("Synergy : Entering createTask method of SynergyTaskManager");
   }
   switch (currentTaskState) {
     case TASK_STATE_CREATED:
       if (defaultTask) {
         // make sure the current task is the default task
         if (SynergyUtil.getDefaultTask(logger, ccmAddr) != currentTaskNumber) {
           SynergyUtil.setDefaultTask(logger, currentTaskNumber, ccmAddr);
         }
       }
       break;
     case TASK_STATE_NONE: // fall through
     case TASK_STATE_COMPLETED:
       currentTaskNumber = SynergyUtil.createTask(logger, synopsis, release, defaultTask, ccmAddr);
       currentTaskState = TASK_STATE_CREATED;
       break;
     default:
       throw new IllegalStateException(
           "Programming error: SynergyTaskManager is in unkown state.");
   }
   if (logger.isDebugEnabled()) {
     logger.debug("createTask returns " + currentTaskNumber);
   }
   return currentTaskNumber;
 }
  private void processAddedFile(String line, int pos) {
    String addedFilePath = this.currentDir + "/" + line.substring(0, pos);

    this.files.add(new ScmFile(addedFilePath, ScmFileStatus.ADDED));

    if (logger.isInfoEnabled()) {
      logger.info("Added: " + addedFilePath);
    }
  }
  private void processDirectory(String line, int pos) {
    String dirPath =
        line.substring(pos + DIR_MARKER.length(), line.length() - 1).replace('\\', '/');

    try {
      this.currentDir =
          StarteamCommandLineUtils.getRelativeChildDirectory(this.workingDirectory, dirPath);
    } catch (IllegalStateException e) {
      String error = "Working and checkout directories are not on the same tree";

      if (logger.isErrorEnabled()) {
        logger.error(error);
        logger.error("Working directory: " + workingDirectory);
        logger.error("Checked out directory: " + dirPath);
      }

      throw new IllegalStateException(error);
    }
  }
  /** {@inheritDoc} */
  public void consumeLine(String line) {
    if (logger.isDebugEnabled()) {
      logger.debug(line);
    }

    int pos = 0;

    if ((pos = line.indexOf(DIR_MARKER)) != -1) {
      processDirectory(line, pos);
    } else if ((pos = line.indexOf(ADDED_MARKER)) != -1) {
      processAddedFile(line, pos);
    } else if ((pos = line.indexOf(LINKTO_MARKER)) != -1) {
      // ignore
    } else {
      if (logger.isWarnEnabled()) {
        this.logger.warn("Unknown add ouput: " + line);
      }
    }
  }
  public static int executeCleanUp(
      File workinDirectory, StreamConsumer stdout, StreamConsumer stderr, ScmLogger logger)
      throws CommandLineException {
    Commandline cl = new Commandline();

    cl.setExecutable("svn");

    cl.setWorkingDirectory(workinDirectory.getAbsolutePath());

    if (logger != null) {
      if (logger.isInfoEnabled()) {
        logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));

        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
          logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
        }
      }
    }

    return CommandLineUtils.executeCommandLine(cl, stdout, stderr);
  }
 /**
  * Check in (that is: complete) the default task. This is either the current task managed by
  * <code>SynergyTaskManager</code> or, if none is managed, the default task.<br>
  * In case no task has yet been created by <code>SynergyTaskManager</code> AND no default task is
  * set, then this is an error.<br>
  * However, if the task that was created by <code>SynergyTaskManager</code> has already been
  * checked in AND no default task is set, then it is assumed that all files that were checked out
  * are already checked in because checking in a task checks in all files associated with it.
  *
  * @param logger a logger.
  * @param comment a comment for checkin.
  * @param ccmAddr current Synergy session ID. Used to run in multi-session.
  * @throws ScmException
  */
 public void checkinDefaultTask(ScmLogger logger, String comment, String ccmAddr)
     throws ScmException {
   if (logger.isDebugEnabled()) {
     logger.debug("Synergy : Entering checkinDefaultTask method of SynergyTaskManager");
   }
   switch (currentTaskState) {
     case TASK_STATE_NONE:
       // if a default task is set, then check in that
       // otherwise we have an error
       if (SynergyUtil.getDefaultTask(logger, ccmAddr) != 0) {
         SynergyUtil.checkinDefaultTask(logger, comment, ccmAddr);
       } else {
         throw new ScmException(
             "Check in not possible: no default task is set and "
                 + "no task has been created with SynergyTaskManager.");
       }
       break;
     case TASK_STATE_CREATED:
       SynergyUtil.checkinTask(logger, currentTaskNumber, comment, ccmAddr);
       currentTaskState = TASK_STATE_COMPLETED;
       break;
     case TASK_STATE_COMPLETED:
       // if a default task is set, then check in that
       // otherwise do nothing, as all tasks and all files with them have
       // been checked in
       if (SynergyUtil.getDefaultTask(logger, ccmAddr) != 0) {
         SynergyUtil.checkinDefaultTask(logger, comment, ccmAddr);
       } else {
         if (logger.isDebugEnabled()) {
           logger.debug(
               "Synergy : No check in necessary as default task and "
                   + "all tasks created with SynergyTaskManager have already been checked in.");
         }
       }
       break;
     default:
       throw new IllegalStateException(
           "Programming error: SynergyTaskManager is in unkown state.");
   }
 }
  private static int checkIfCleanUpIsNeeded(
      int exitCode,
      Commandline cl,
      StreamConsumer consumer,
      CommandLineUtils.StringStreamConsumer stderr,
      ScmLogger logger)
      throws CommandLineException {
    if (exitCode != 0
        && stderr.getOutput() != null
        && stderr.getOutput().indexOf("'svn cleanup'") > 0
        && stderr.getOutput().indexOf("'svn help cleanup'") > 0) {
      if (logger.isInfoEnabled()) {
        logger.info(
            "Svn command failed due to some locks in working copy. We try to run a 'svn cleanup'.");
      }

      if (executeCleanUp(cl.getWorkingDirectory(), consumer, stderr, logger) == 0) {
        exitCode = CommandLineUtils.executeCommandLine(cl, consumer, stderr);
      }
    }
    return exitCode;
  }