GpgConfig.java

  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.  * Typed access to GPG related configuration options.
  13.  *
  14.  * @since 5.2
  15.  */
  16. public class GpgConfig {

  17.     /**
  18.      * Config values for gpg.format.
  19.      */
  20.     public enum GpgFormat implements Config.ConfigEnum {

  21.         /** Value for openpgp */
  22.         OPENPGP("openpgp"), //$NON-NLS-1$
  23.         /** Value for x509 */
  24.         X509("x509"); //$NON-NLS-1$

  25.         private final String configValue;

  26.         private GpgFormat(String configValue) {
  27.             this.configValue = configValue;
  28.         }

  29.         @Override
  30.         public boolean matchConfigValue(String s) {
  31.             return configValue.equals(s);
  32.         }

  33.         @Override
  34.         public String toConfigValue() {
  35.             return configValue;
  36.         }
  37.     }

  38.     private final GpgFormat keyFormat;

  39.     private final String signingKey;

  40.     private final String program;

  41.     private final boolean signCommits;

  42.     private final boolean signAllTags;

  43.     private final boolean forceAnnotated;

  44.     /**
  45.      * Create a {@link GpgConfig} with the given parameters and default
  46.      * {@code true} for signing commits and {@code false} for tags.
  47.      *
  48.      * @param keySpec
  49.      *            to use
  50.      * @param format
  51.      *            to use
  52.      * @param gpgProgram
  53.      *            to use
  54.      * @since 5.11
  55.      */
  56.     public GpgConfig(String keySpec, GpgFormat format, String gpgProgram) {
  57.         keyFormat = format;
  58.         signingKey = keySpec;
  59.         program = gpgProgram;
  60.         signCommits = true;
  61.         signAllTags = false;
  62.         forceAnnotated = false;
  63.     }

  64.     /**
  65.      * Create a new GPG config that reads the configuration from config.
  66.      *
  67.      * @param config
  68.      *            the config to read from
  69.      */
  70.     public GpgConfig(Config config) {
  71.         keyFormat = config.getEnum(GpgFormat.values(),
  72.                 ConfigConstants.CONFIG_GPG_SECTION, null,
  73.                 ConfigConstants.CONFIG_KEY_FORMAT, GpgFormat.OPENPGP);
  74.         signingKey = config.getString(ConfigConstants.CONFIG_USER_SECTION, null,
  75.                 ConfigConstants.CONFIG_KEY_SIGNINGKEY);

  76.         String exe = config.getString(ConfigConstants.CONFIG_GPG_SECTION,
  77.                 keyFormat.toConfigValue(), ConfigConstants.CONFIG_KEY_PROGRAM);
  78.         if (exe == null) {
  79.             exe = config.getString(ConfigConstants.CONFIG_GPG_SECTION, null,
  80.                     ConfigConstants.CONFIG_KEY_PROGRAM);
  81.         }
  82.         program = exe;
  83.         signCommits = config.getBoolean(ConfigConstants.CONFIG_COMMIT_SECTION,
  84.                 ConfigConstants.CONFIG_KEY_GPGSIGN, false);
  85.         signAllTags = config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
  86.                 ConfigConstants.CONFIG_KEY_GPGSIGN, false);
  87.         forceAnnotated = config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
  88.                 ConfigConstants.CONFIG_KEY_FORCE_SIGN_ANNOTATED, false);
  89.     }

  90.     /**
  91.      * Retrieves the config value of gpg.format.
  92.      *
  93.      * @return the {@link org.eclipse.jgit.lib.GpgConfig.GpgFormat}
  94.      */
  95.     public GpgFormat getKeyFormat() {
  96.         return keyFormat;
  97.     }

  98.     /**
  99.      * Retrieves the value of the configured GPG program to use, as defined by
  100.      * gpg.openpgp.program, gpg.x509.program (depending on the defined
  101.      * {@link #getKeyFormat() format}), or gpg.program.
  102.      *
  103.      * @return the program string configured, or {@code null} if none
  104.      * @since 5.11
  105.      */
  106.     public String getProgram() {
  107.         return program;
  108.     }

  109.     /**
  110.      * Retrieves the config value of user.signingKey.
  111.      *
  112.      * @return the value of user.signingKey (may be <code>null</code>)
  113.      */
  114.     public String getSigningKey() {
  115.         return signingKey;
  116.     }

  117.     /**
  118.      * Retrieves the config value of commit.gpgSign.
  119.      *
  120.      * @return the value of commit.gpgSign (defaults to <code>false</code>)
  121.      */
  122.     public boolean isSignCommits() {
  123.         return signCommits;
  124.     }

  125.     /**
  126.      * Retrieves the value of git config {@code tag.gpgSign}.
  127.      *
  128.      * @return the value of {@code tag.gpgSign}; by default {@code false}
  129.      *
  130.      * @since 5.11
  131.      */
  132.     public boolean isSignAllTags() {
  133.         return signAllTags;
  134.     }

  135.     /**
  136.      * Retrieves the value of git config {@code tag.forceSignAnnotated}.
  137.      *
  138.      * @return the value of {@code tag.forceSignAnnotated}; by default
  139.      *         {@code false}
  140.      *
  141.      * @since 5.11
  142.      */
  143.     public boolean isSignAnnotated() {
  144.         return forceAnnotated;
  145.     }
  146. }