View Javadoc
1   /*
2    * Copyright (C) 2018, 2021 Salesforce 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.lib;
11  
12  /**
13   * Typed access to GPG related configuration options.
14   *
15   * @since 5.2
16   */
17  public class GpgConfig {
18  
19  	/**
20  	 * Config values for gpg.format.
21  	 */
22  	public enum GpgFormat implements Config.ConfigEnum {
23  
24  		/** Value for openpgp */
25  		OPENPGP("openpgp"), //$NON-NLS-1$
26  		/** Value for x509 */
27  		X509("x509"); //$NON-NLS-1$
28  
29  		private final String configValue;
30  
31  		private GpgFormat(String configValue) {
32  			this.configValue = configValue;
33  		}
34  
35  		@Override
36  		public boolean matchConfigValue(String s) {
37  			return configValue.equals(s);
38  		}
39  
40  		@Override
41  		public String toConfigValue() {
42  			return configValue;
43  		}
44  	}
45  
46  	private final GpgFormat keyFormat;
47  
48  	private final String signingKey;
49  
50  	private final String program;
51  
52  	private final boolean signCommits;
53  
54  	private final boolean signAllTags;
55  
56  	private final boolean forceAnnotated;
57  
58  	/**
59  	 * Create a {@link GpgConfig} with the given parameters and default
60  	 * {@code true} for signing commits and {@code false} for tags.
61  	 *
62  	 * @param keySpec
63  	 *            to use
64  	 * @param format
65  	 *            to use
66  	 * @param gpgProgram
67  	 *            to use
68  	 * @since 5.11
69  	 */
70  	public GpgConfig(String keySpec, GpgFormat format, String gpgProgram) {
71  		keyFormat = format;
72  		signingKey = keySpec;
73  		program = gpgProgram;
74  		signCommits = true;
75  		signAllTags = false;
76  		forceAnnotated = false;
77  	}
78  
79  	/**
80  	 * Create a new GPG config that reads the configuration from config.
81  	 *
82  	 * @param config
83  	 *            the config to read from
84  	 */
85  	public GpgConfig(Config config) {
86  		keyFormat = config.getEnum(GpgFormat.values(),
87  				ConfigConstants.CONFIG_GPG_SECTION, null,
88  				ConfigConstants.CONFIG_KEY_FORMAT, GpgFormat.OPENPGP);
89  		signingKey = config.getString(ConfigConstants.CONFIG_USER_SECTION, null,
90  				ConfigConstants.CONFIG_KEY_SIGNINGKEY);
91  
92  		String exe = config.getString(ConfigConstants.CONFIG_GPG_SECTION,
93  				keyFormat.toConfigValue(), ConfigConstants.CONFIG_KEY_PROGRAM);
94  		if (exe == null) {
95  			exe = config.getString(ConfigConstants.CONFIG_GPG_SECTION, null,
96  					ConfigConstants.CONFIG_KEY_PROGRAM);
97  		}
98  		program = exe;
99  		signCommits = config.getBoolean(ConfigConstants.CONFIG_COMMIT_SECTION,
100 				ConfigConstants.CONFIG_KEY_GPGSIGN, false);
101 		signAllTags = config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
102 				ConfigConstants.CONFIG_KEY_GPGSIGN, false);
103 		forceAnnotated = config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
104 				ConfigConstants.CONFIG_KEY_FORCE_SIGN_ANNOTATED, false);
105 	}
106 
107 	/**
108 	 * Retrieves the config value of gpg.format.
109 	 *
110 	 * @return the {@link org.eclipse.jgit.lib.GpgConfig.GpgFormat}
111 	 */
112 	public GpgFormat getKeyFormat() {
113 		return keyFormat;
114 	}
115 
116 	/**
117 	 * Retrieves the value of the configured GPG program to use, as defined by
118 	 * gpg.openpgp.program, gpg.x509.program (depending on the defined
119 	 * {@link #getKeyFormat() format}), or gpg.program.
120 	 *
121 	 * @return the program string configured, or {@code null} if none
122 	 * @since 5.11
123 	 */
124 	public String getProgram() {
125 		return program;
126 	}
127 
128 	/**
129 	 * Retrieves the config value of user.signingKey.
130 	 *
131 	 * @return the value of user.signingKey (may be <code>null</code>)
132 	 */
133 	public String getSigningKey() {
134 		return signingKey;
135 	}
136 
137 	/**
138 	 * Retrieves the config value of commit.gpgSign.
139 	 *
140 	 * @return the value of commit.gpgSign (defaults to <code>false</code>)
141 	 */
142 	public boolean isSignCommits() {
143 		return signCommits;
144 	}
145 
146 	/**
147 	 * Retrieves the value of git config {@code tag.gpgSign}.
148 	 *
149 	 * @return the value of {@code tag.gpgSign}; by default {@code false}
150 	 *
151 	 * @since 5.11
152 	 */
153 	public boolean isSignAllTags() {
154 		return signAllTags;
155 	}
156 
157 	/**
158 	 * Retrieves the value of git config {@code tag.forceSignAnnotated}.
159 	 *
160 	 * @return the value of {@code tag.forceSignAnnotated}; by default
161 	 *         {@code false}
162 	 *
163 	 * @since 5.11
164 	 */
165 	public boolean isSignAnnotated() {
166 		return forceAnnotated;
167 	}
168 }