]> source.dussan.org Git - jgit.git/commitdiff
SSH signing: make OpenSSH pattern matching public 24/1202324/3
authorThomas Wolf <twolf@apache.org>
Sat, 28 Sep 2024 13:52:31 +0000 (15:52 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 22 Oct 2024 11:16:51 +0000 (13:16 +0200)
SSH signing needs the same pattern matching algorithm as is used for
host matching in host entries in ~/.ssh/config. So make that pattern
matching available via a static method.

Change-Id: Ia26f23666f323f44ce66f769fbcd6c85965eb219
Signed-off-by: Thomas Wolf <twolf@apache.org>
org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/ssh/OpenSshConfigFile.java

index 3e75a9dde33c351100b2d0416bd5ba8705ed6b9a..542d6e94f341d50a7540c61a9ca32e265eb9af72 100644 (file)
@@ -427,7 +427,35 @@ public class OpenSshConfigFile implements SshConfigStore {
                return value;
        }
 
-       private static boolean patternMatchesHost(String pattern, String name) {
+       /**
+        * Tells whether a given {@code name} matches the given list of patterns,
+        * accounting for negative matches.
+        *
+        * @param patterns
+        *            to test {@code name} against; any pattern starting with an
+        *            exclamation mark is a negative pattern
+        * @param name
+        *            to test
+        * @return {@code true} if the {@code name} matches at least one of the
+        *         non-negative patterns and none of the negative patterns,
+        *         {@code false} otherwise
+        * @since 7.1
+        */
+       public static boolean patternMatch(Iterable<String> patterns, String name) {
+               boolean doesMatch = false;
+               for (String pattern : patterns) {
+                       if (pattern.startsWith("!")) { //$NON-NLS-1$
+                               if (patternMatches(pattern.substring(1), name)) {
+                                       return false;
+                               }
+                       } else if (!doesMatch && patternMatches(pattern, name)) {
+                               doesMatch = true;
+                       }
+               }
+               return doesMatch;
+       }
+
+       private static boolean patternMatches(String pattern, String name) {
                if (pattern.indexOf('*') >= 0 || pattern.indexOf('?') >= 0) {
                        final FileNameMatcher fn;
                        try {
@@ -680,18 +708,7 @@ public class OpenSshConfigFile implements SshConfigStore {
                }
 
                boolean matches(String hostName) {
-                       boolean doesMatch = false;
-                       for (String pattern : patterns) {
-                               if (pattern.startsWith("!")) { //$NON-NLS-1$
-                                       if (patternMatchesHost(pattern.substring(1), hostName)) {
-                                               return false;
-                                       }
-                               } else if (!doesMatch
-                                               && patternMatchesHost(pattern, hostName)) {
-                                       doesMatch = true;
-                               }
-                       }
-                       return doesMatch;
+                       return patternMatch(patterns, hostName);
                }
 
                private static String toKey(String key) {