Exemple #1
0
  /**
   * Save the configuration as a Git text style configuration file.
   *
   * <p><b>Warning:</b> Although this method uses the traditional Git file locking approach to
   * protect against concurrent writes of the configuration file, it does not ensure that the file
   * has not been modified since the last read, which means updates performed by other objects
   * accessing the same backing file may be lost.
   *
   * @throws IOException the file could not be written.
   */
  public void save() throws IOException {
    final byte[] out;
    final String text = toText();
    if (utf8Bom) {
      final ByteArrayOutputStream bos = new ByteArrayOutputStream();
      bos.write(0xEF);
      bos.write(0xBB);
      bos.write(0xBF);
      bos.write(text.getBytes(RawParseUtils.UTF8_CHARSET.name()));
      out = bos.toByteArray();
    } else {
      out = Constants.encode(text);
    }

    final LockFile lf = new LockFile(getFile(), fs);
    if (!lf.lock()) throw new LockFailedException(getFile());
    try {
      lf.setNeedSnapshot(true);
      lf.write(out);
      if (!lf.commit())
        throw new IOException(MessageFormat.format(JGitText.get().cannotCommitWriteTo, getFile()));
    } finally {
      lf.unlock();
    }
    snapshot = lf.getCommitSnapshot();
    hash = hash(out);
    // notify the listeners
    fireConfigChangedEvent();
  }
Exemple #2
0
 /**
  * Commit this change and release the lock.
  *
  * <p>If this method fails (returns false) the lock is still released.
  *
  * @return true if the commit was successful and the file contains the new data; false if the
  *     commit failed and the file remains with the old data.
  * @throws IllegalStateException the lock is not held.
  */
 public boolean commit() {
   final LockFile tmp = myLock;
   requireLocked(tmp);
   myLock = null;
   if (!tmp.commit()) return false;
   snapshot = tmp.getCommitSnapshot();
   if (indexChangedListener != null && !Arrays.equals(readIndexChecksum, writeIndexChecksum))
     indexChangedListener.onIndexChanged(new IndexChangedEvent());
   return true;
 }