RefDirectoryUpdate.java

  1. /*
  2.  * Copyright (C) 2009-2010, Google Inc.
  3.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.internal.storage.file;

  12. import static org.eclipse.jgit.lib.Constants.encode;

  13. import java.io.IOException;

  14. import org.eclipse.jgit.lib.Ref;
  15. import org.eclipse.jgit.lib.RefUpdate;
  16. import org.eclipse.jgit.lib.ReflogEntry;
  17. import org.eclipse.jgit.lib.Repository;

  18. /** Updates any reference stored by {@link RefDirectory}. */
  19. class RefDirectoryUpdate extends RefUpdate {
  20.     private final RefDirectory database;

  21.     private boolean shouldDeref;
  22.     private LockFile lock;

  23.     RefDirectoryUpdate(RefDirectory r, Ref ref) {
  24.         super(ref);
  25.         database = r;
  26.     }

  27.     /** {@inheritDoc} */
  28.     @Override
  29.     protected RefDirectory getRefDatabase() {
  30.         return database;
  31.     }

  32.     /** {@inheritDoc} */
  33.     @Override
  34.     protected Repository getRepository() {
  35.         return database.getRepository();
  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     protected boolean tryLock(boolean deref) throws IOException {
  40.         shouldDeref = deref;
  41.         Ref dst = getRef();
  42.         if (deref)
  43.             dst = dst.getLeaf();
  44.         String name = dst.getName();
  45.         lock = new LockFile(database.fileFor(name));
  46.         if (lock.lock()) {
  47.             dst = database.findRef(name);
  48.             setOldObjectId(dst != null ? dst.getObjectId() : null);
  49.             return true;
  50.         }
  51.         return false;
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     protected void unlock() {
  56.         if (lock != null) {
  57.             lock.unlock();
  58.             lock = null;
  59.         }
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     protected Result doUpdate(Result status) throws IOException {
  64.         WriteConfig wc = database.getRepository().getConfig()
  65.                 .get(WriteConfig.KEY);

  66.         lock.setFSync(wc.getFSyncRefFiles());
  67.         lock.setNeedStatInformation(true);
  68.         lock.write(getNewObjectId());

  69.         String msg = getRefLogMessage();
  70.         if (msg != null) {
  71.             if (isRefLogIncludingResult()) {
  72.                 String strResult = toResultString(status);
  73.                 if (strResult != null) {
  74.                     if (msg.length() > 0)
  75.                         msg = msg + ": " + strResult; //$NON-NLS-1$
  76.                     else
  77.                         msg = strResult;
  78.                 }
  79.             }
  80.             database.log(isForceRefLog(), this, msg, shouldDeref);
  81.         }
  82.         if (!lock.commit())
  83.             return Result.LOCK_FAILURE;
  84.         database.stored(this, lock.getCommitSnapshot());
  85.         return status;
  86.     }

  87.     private String toResultString(Result status) {
  88.         switch (status) {
  89.         case FORCED:
  90.             return ReflogEntry.PREFIX_FORCED_UPDATE;
  91.         case FAST_FORWARD:
  92.             return ReflogEntry.PREFIX_FAST_FORWARD;
  93.         case NEW:
  94.             return ReflogEntry.PREFIX_CREATED;
  95.         default:
  96.             return null;
  97.         }
  98.     }

  99.     /** {@inheritDoc} */
  100.     @Override
  101.     protected Result doDelete(Result status) throws IOException {
  102.         if (getRef().getStorage() != Ref.Storage.NEW)
  103.             database.delete(this);
  104.         return status;
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     protected Result doLink(String target) throws IOException {
  109.         WriteConfig wc = database.getRepository().getConfig()
  110.                 .get(WriteConfig.KEY);

  111.         lock.setFSync(wc.getFSyncRefFiles());
  112.         lock.setNeedStatInformation(true);
  113.         lock.write(encode(RefDirectory.SYMREF + target + '\n'));

  114.         String msg = getRefLogMessage();
  115.         if (msg != null)
  116.             database.log(isForceRefLog(), this, msg, false);
  117.         if (!lock.commit())
  118.             return Result.LOCK_FAILURE;
  119.         database.storedSymbolicRef(this, lock.getCommitSnapshot(), target);

  120.         if (getRef().getStorage() == Ref.Storage.NEW)
  121.             return Result.NEW;
  122.         return Result.FORCED;
  123.     }
  124. }