DfsRepository.java

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

  10. package org.eclipse.jgit.internal.storage.dfs;

  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.text.MessageFormat;
  14. import java.util.Collections;

  15. import org.eclipse.jgit.attributes.AttributesNode;
  16. import org.eclipse.jgit.attributes.AttributesNodeProvider;
  17. import org.eclipse.jgit.attributes.AttributesRule;
  18. import org.eclipse.jgit.internal.JGitText;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.lib.RefUpdate;
  21. import org.eclipse.jgit.lib.ReflogReader;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.lib.StoredConfig;

  24. /**
  25.  * A Git repository on a DFS.
  26.  */
  27. public abstract class DfsRepository extends Repository {
  28.     private final DfsConfig config;

  29.     private final DfsRepositoryDescription description;

  30.     /**
  31.      * Initialize a DFS repository.
  32.      *
  33.      * @param builder
  34.      *            description of the repository.
  35.      */
  36.     protected DfsRepository(DfsRepositoryBuilder builder) {
  37.         super(builder);
  38.         this.config = new DfsConfig();
  39.         this.description = builder.getRepositoryDescription();
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public abstract DfsObjDatabase getObjectDatabase();

  44.     /**
  45.      * Get the description of this repository.
  46.      *
  47.      * @return the description of this repository.
  48.      */
  49.     public DfsRepositoryDescription getDescription() {
  50.         return description;
  51.     }

  52.     /**
  53.      * Check if the repository already exists.
  54.      *
  55.      * @return true if the repository exists; false if it is new.
  56.      * @throws java.io.IOException
  57.      *             the repository cannot be checked.
  58.      */
  59.     public boolean exists() throws IOException {
  60.         if (getRefDatabase() instanceof DfsRefDatabase) {
  61.             return ((DfsRefDatabase) getRefDatabase()).exists();
  62.         }
  63.         return true;
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public void create(boolean bare) throws IOException {
  68.         if (exists())
  69.             throw new IOException(MessageFormat.format(
  70.                     JGitText.get().repositoryAlreadyExists, "")); //$NON-NLS-1$

  71.         String master = Constants.R_HEADS + Constants.MASTER;
  72.         RefUpdate.Result result = updateRef(Constants.HEAD, true).link(master);
  73.         if (result != RefUpdate.Result.NEW)
  74.             throw new IOException(result.name());
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public StoredConfig getConfig() {
  79.         return config;
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public String getIdentifier() {
  84.         return getDescription().getRepositoryName();
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public void scanForRepoChanges() throws IOException {
  89.         getRefDatabase().refresh();
  90.         getObjectDatabase().clearCache();
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     public void notifyIndexChanged(boolean internal) {
  95.         // Do not send notifications.
  96.         // There is no index, as there is no working tree.
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     public ReflogReader getReflogReader(String refName) throws IOException {
  101.         throw new UnsupportedOperationException();
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public AttributesNodeProvider createAttributesNodeProvider() {
  106.         // TODO Check if the implementation used in FileRepository can be used
  107.         // for this kind of repository
  108.         return new EmptyAttributesNodeProvider();
  109.     }

  110.     private static class EmptyAttributesNodeProvider implements
  111.             AttributesNodeProvider {
  112.         private EmptyAttributesNode emptyAttributesNode = new EmptyAttributesNode();

  113.         @Override
  114.         public AttributesNode getInfoAttributesNode() throws IOException {
  115.             return emptyAttributesNode;
  116.         }

  117.         @Override
  118.         public AttributesNode getGlobalAttributesNode() throws IOException {
  119.             return emptyAttributesNode;
  120.         }

  121.         private static class EmptyAttributesNode extends AttributesNode {

  122.             public EmptyAttributesNode() {
  123.                 super(Collections.<AttributesRule> emptyList());
  124.             }

  125.             @Override
  126.             public void parse(InputStream in) throws IOException {
  127.                 // Do nothing
  128.             }
  129.         }
  130.     }
  131. }