View Javadoc
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  
11  package org.eclipse.jgit.diff;
12  
13  import java.text.MessageFormat;
14  
15  import org.eclipse.jgit.internal.JGitText;
16  import org.eclipse.jgit.lib.Config;
17  import org.eclipse.jgit.lib.Config.SectionParser;
18  import org.eclipse.jgit.lib.ConfigConstants;
19  import org.eclipse.jgit.util.StringUtils;
20  
21  /**
22   * Keeps track of diff related configuration options.
23   */
24  public class DiffConfig {
25  	/** Key for {@link Config#get(SectionParser)}. */
26  	public static final Config.SectionParser<DiffConfig> KEY = DiffConfig::new;
27  
28  	/** Permissible values for {@code diff.renames}. */
29  	public enum RenameDetectionType {
30  		/** Rename detection is disabled. */
31  		FALSE,
32  
33  		/** Rename detection is enabled. */
34  		TRUE,
35  
36  		/** Copies should be detected too. */
37  		COPY
38  	}
39  
40  	private final boolean noPrefix;
41  
42  	private final RenameDetectionType renameDetectionType;
43  
44  	private final int renameLimit;
45  
46  	private DiffConfig(Config rc) {
47  		noPrefix = rc.getBoolean(ConfigConstants.CONFIG_DIFF_SECTION,
48  				ConfigConstants.CONFIG_KEY_NOPREFIX, false);
49  		renameDetectionType = parseRenameDetectionType(rc.getString(
50  				ConfigConstants.CONFIG_DIFF_SECTION, null, ConfigConstants.CONFIG_KEY_RENAMES));
51  		renameLimit = rc.getInt(ConfigConstants.CONFIG_DIFF_SECTION,
52  				ConfigConstants.CONFIG_KEY_RENAMELIMIT, 400);
53  	}
54  
55  	/**
56  	 * If prefix should be suppressed
57  	 *
58  	 * @return true if the prefix "a/" and "b/" should be suppressed
59  	 */
60  	public boolean isNoPrefix() {
61  		return noPrefix;
62  	}
63  
64  	/**
65  	 * If rename detection is enabled
66  	 *
67  	 * @return true if rename detection is enabled by default
68  	 */
69  	public boolean isRenameDetectionEnabled() {
70  		return renameDetectionType != RenameDetectionType.FALSE;
71  	}
72  
73  	/**
74  	 * Get the rename detection type
75  	 *
76  	 * @return type of rename detection to perform
77  	 */
78  	public RenameDetectionType getRenameDetectionType() {
79  		return renameDetectionType;
80  	}
81  
82  	/**
83  	 * Get the rename limit
84  	 *
85  	 * @return limit on number of paths to perform inexact rename detection
86  	 */
87  	public int getRenameLimit() {
88  		return renameLimit;
89  	}
90  
91  	private static RenameDetectionType parseRenameDetectionType(
92  			final String renameString) {
93  		if (renameString == null)
94  			return RenameDetectionType.FALSE;
95  		else if (StringUtils.equalsIgnoreCase(
96  				ConfigConstants.CONFIG_RENAMELIMIT_COPY, renameString)
97  				|| StringUtils
98  						.equalsIgnoreCase(
99  								ConfigConstants.CONFIG_RENAMELIMIT_COPIES,
100 								renameString))
101 			return RenameDetectionType.COPY;
102 		else {
103 			final Boolean renameBoolean = StringUtils
104 					.toBooleanOrNull(renameString);
105 			if (renameBoolean == null)
106 				throw new IllegalArgumentException(MessageFormat.format(
107 						JGitText.get().enumValueNotSupported2,
108 						ConfigConstants.CONFIG_DIFF_SECTION,
109 						ConfigConstants.CONFIG_KEY_RENAMES, renameString));
110 			else if (renameBoolean.booleanValue())
111 				return RenameDetectionType.TRUE;
112 			else
113 				return RenameDetectionType.FALSE;
114 		}
115 	}
116 }