View Javadoc
1   /*
2    * Copyright (C) 2017, 2022 David Pursehouse <david.pursehouse@gmail.com> 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.transport;
12  
13  import java.util.Locale;
14  
15  import org.eclipse.jgit.lib.Config;
16  import org.eclipse.jgit.lib.ConfigConstants;
17  import org.eclipse.jgit.util.StringUtils;
18  
19  /**
20   * Push section of a Git configuration file.
21   *
22   * @since 4.9
23   */
24  public class PushConfig {
25  
26  	/**
27  	 * Git config values for {@code push.recurseSubmodules}.
28  	 */
29  	public enum PushRecurseSubmodulesMode implements Config.ConfigEnum {
30  		/**
31  		 * Verify that all submodule commits that changed in the revisions to be
32  		 * pushed are available on at least one remote of the submodule.
33  		 */
34  		CHECK("check"), //$NON-NLS-1$
35  
36  		/**
37  		 * All submodules that changed in the revisions to be pushed will be
38  		 * pushed.
39  		 */
40  		ON_DEMAND("on-demand"), //$NON-NLS-1$
41  
42  		/** Default behavior of ignoring submodules when pushing is retained. */
43  		NO("false"); //$NON-NLS-1$
44  
45  		private final String configValue;
46  
47  		private PushRecurseSubmodulesMode(String configValue) {
48  			this.configValue = configValue;
49  		}
50  
51  		@Override
52  		public String toConfigValue() {
53  			return configValue;
54  		}
55  
56  		@Override
57  		public boolean matchConfigValue(String s) {
58  			if (StringUtils.isEmptyOrNull(s)) {
59  				return false;
60  			}
61  			s = s.replace('-', '_');
62  			return name().equalsIgnoreCase(s)
63  					|| configValue.equalsIgnoreCase(s);
64  		}
65  	}
66  
67  	/**
68  	 * Git config values for {@code push.default}.
69  	 *
70  	 * @since 6.1
71  	 */
72  	public enum PushDefault implements Config.ConfigEnum {
73  
74  		/**
75  		 * Do not push if there are no explicit refspecs.
76  		 */
77  		NOTHING,
78  
79  		/**
80  		 * Push the current branch to an upstream branch of the same name.
81  		 */
82  		CURRENT,
83  
84  		/**
85  		 * Push the current branch to an upstream branch determined by git
86  		 * config {@code branch.<currentBranch>.merge}.
87  		 */
88  		UPSTREAM("tracking"), //$NON-NLS-1$
89  
90  		/**
91  		 * Like {@link #UPSTREAM}, but only if the upstream name is the same as
92  		 * the name of the current local branch.
93  		 */
94  		SIMPLE,
95  
96  		/**
97  		 * Push all current local branches that match a configured push refspec
98  		 * of the remote configuration.
99  		 */
100 		MATCHING;
101 
102 		private final String alias;
103 
104 		private PushDefault() {
105 			alias = null;
106 		}
107 
108 		private PushDefault(String alias) {
109 			this.alias = alias;
110 		}
111 
112 		@Override
113 		public String toConfigValue() {
114 			return name().toLowerCase(Locale.ROOT);
115 		}
116 
117 		@Override
118 		public boolean matchConfigValue(String in) {
119 			return toConfigValue().equalsIgnoreCase(in)
120 					|| (alias != null && alias.equalsIgnoreCase(in));
121 		}
122 	}
123 
124 	private final PushRecurseSubmodulesMode recurseSubmodules;
125 
126 	private final PushDefault pushDefault;
127 
128 	/**
129 	 * Creates a new instance.
130 	 *
131 	 * @param config
132 	 *            {@link Config} to fill the {@link PushConfig} from
133 	 * @since 6.1
134 	 */
135 	public PushConfig(Config config) {
136 		recurseSubmodules = config.getEnum(ConfigConstants.CONFIG_PUSH_SECTION,
137 				null, ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES,
138 				PushRecurseSubmodulesMode.NO);
139 		pushDefault = config.getEnum(ConfigConstants.CONFIG_PUSH_SECTION, null,
140 				ConfigConstants.CONFIG_KEY_DEFAULT, PushDefault.SIMPLE);
141 	}
142 
143 	/**
144 	 * Retrieves the value of git config {@code push.recurseSubmodules}.
145 	 *
146 	 * @return the value
147 	 * @since 6.1
148 	 */
149 	public PushRecurseSubmodulesMode getRecurseSubmodules() {
150 		return recurseSubmodules;
151 	}
152 
153 	/**
154 	 * Retrieves the value of git config {@code push.default}.
155 	 *
156 	 * @return the value
157 	 * @since 6.1
158 	 */
159 	public PushDefault getPushDefault() {
160 		return pushDefault;
161 	}
162 }