DiffConfig.java

  1. /*
  2.  * Copyright (C) 2010, 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.diff;

  11. import java.text.MessageFormat;

  12. import org.eclipse.jgit.internal.JGitText;
  13. import org.eclipse.jgit.lib.Config;
  14. import org.eclipse.jgit.lib.Config.SectionParser;
  15. import org.eclipse.jgit.lib.ConfigConstants;
  16. import org.eclipse.jgit.util.StringUtils;

  17. /**
  18.  * Keeps track of diff related configuration options.
  19.  */
  20. public class DiffConfig {
  21.     /** Key for {@link Config#get(SectionParser)}. */
  22.     public static final Config.SectionParser<DiffConfig> KEY = DiffConfig::new;

  23.     /** Permissible values for {@code diff.renames}. */
  24.     public enum RenameDetectionType {
  25.         /** Rename detection is disabled. */
  26.         FALSE,

  27.         /** Rename detection is enabled. */
  28.         TRUE,

  29.         /** Copies should be detected too. */
  30.         COPY
  31.     }

  32.     private final boolean noPrefix;

  33.     private final RenameDetectionType renameDetectionType;

  34.     private final int renameLimit;

  35.     private DiffConfig(Config rc) {
  36.         noPrefix = rc.getBoolean(ConfigConstants.CONFIG_DIFF_SECTION,
  37.                 ConfigConstants.CONFIG_KEY_NOPREFIX, false);
  38.         renameDetectionType = parseRenameDetectionType(rc.getString(
  39.                 ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_RENAMES));
  40.         renameLimit = rc.getInt(ConfigConstants.CONFIG_DIFF_SECTION,
  41.                 ConfigConstants.CONFIG_KEY_RENAMELIMIT, 400);
  42.     }

  43.     /**
  44.      * If prefix should be suppressed
  45.      *
  46.      * @return true if the prefix "a/" and "b/" should be suppressed
  47.      */
  48.     public boolean isNoPrefix() {
  49.         return noPrefix;
  50.     }

  51.     /**
  52.      * If rename detection is enabled
  53.      *
  54.      * @return true if rename detection is enabled by default
  55.      */
  56.     public boolean isRenameDetectionEnabled() {
  57.         return renameDetectionType != RenameDetectionType.FALSE;
  58.     }

  59.     /**
  60.      * Get the rename detection type
  61.      *
  62.      * @return type of rename detection to perform
  63.      */
  64.     public RenameDetectionType getRenameDetectionType() {
  65.         return renameDetectionType;
  66.     }

  67.     /**
  68.      * Get the rename limit
  69.      *
  70.      * @return limit on number of paths to perform inexact rename detection
  71.      */
  72.     public int getRenameLimit() {
  73.         return renameLimit;
  74.     }

  75.     private static RenameDetectionType parseRenameDetectionType(
  76.             final String renameString) {
  77.         if (renameString == null)
  78.             return RenameDetectionType.FALSE;
  79.         else if (StringUtils.equalsIgnoreCase(
  80.                 ConfigConstants.CONFIG_RENAMELIMIT_COPY, renameString)
  81.                 || StringUtils
  82.                         .equalsIgnoreCase(
  83.                                 ConfigConstants.CONFIG_RENAMELIMIT_COPIES,
  84.                                 renameString))
  85.             return RenameDetectionType.COPY;
  86.         else {
  87.             final Boolean renameBoolean = StringUtils
  88.                     .toBooleanOrNull(renameString);
  89.             if (renameBoolean == null)
  90.                 throw new IllegalArgumentException(MessageFormat.format(
  91.                         JGitText.get().enumValueNotSupported2,
  92.                         ConfigConstants.CONFIG_DIFF_SECTION,
  93.                         ConfigConstants.CONFIG_KEY_RENAMES, renameString));
  94.             else if (renameBoolean.booleanValue())
  95.                 return RenameDetectionType.TRUE;
  96.             else
  97.                 return RenameDetectionType.FALSE;
  98.         }
  99.     }
  100. }