Paths.java

  1. /*
  2.  * Copyright (C) 2016, 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. package org.eclipse.jgit.util;

  11. import static org.eclipse.jgit.lib.FileMode.TYPE_MASK;
  12. import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;

  13. /**
  14.  * Utility functions for paths inside of a Git repository.
  15.  *
  16.  * @since 4.2
  17.  */
  18. public final class Paths {

  19.     /**
  20.      * Remove trailing {@code '/'} if present.
  21.      *
  22.      * @param path
  23.      *            input path to potentially remove trailing {@code '/'} from.
  24.      * @return null if {@code path == null}; {@code path} after removing a
  25.      *         trailing {@code '/'}.
  26.      */
  27.     public static String stripTrailingSeparator(String path) {
  28.         if (path == null || path.isEmpty()) {
  29.             return path;
  30.         }

  31.         int i = path.length();
  32.         if (path.charAt(path.length() - 1) != '/') {
  33.             return path;
  34.         }
  35.         do {
  36.             i--;
  37.         } while (path.charAt(i - 1) == '/');
  38.         return path.substring(0, i);
  39.     }

  40.     /**
  41.      * Determines whether a git path {@code folder} is a prefix of another git
  42.      * path {@code path}, or the same as {@code path}. An empty {@code folder}
  43.      * is <em>not</em> not considered a prefix and matches only if {@code path}
  44.      * is also empty.
  45.      *
  46.      * @param folder
  47.      *            a git path for a directory, without trailing slash
  48.      * @param path
  49.      *            a git path
  50.      * @return {@code true} if {@code folder} is a directory prefix of
  51.      *         {@code path}, or is equal to {@code path}, {@code false}
  52.      *         otherwise
  53.      * @since 6.3
  54.      */
  55.     public static boolean isEqualOrPrefix(String folder, String path) {
  56.         if (folder.isEmpty()) {
  57.             return path.isEmpty();
  58.         }
  59.         boolean isPrefix = path.startsWith(folder);
  60.         if (isPrefix) {
  61.             int length = folder.length();
  62.             return path.length() == length || path.charAt(length) == '/';
  63.         }
  64.         return false;
  65.     }

  66.     /**
  67.      * Compare two paths according to Git path sort ordering rules.
  68.      *
  69.      * @param aPath
  70.      *            first path buffer. The range {@code [aPos, aEnd)} is used.
  71.      * @param aPos
  72.      *            index into {@code aPath} where the first path starts.
  73.      * @param aEnd
  74.      *            1 past last index of {@code aPath}.
  75.      * @param aMode
  76.      *            mode of the first file. Trees are sorted as though
  77.      *            {@code aPath[aEnd] == '/'}, even if aEnd does not exist.
  78.      * @param bPath
  79.      *            second path buffer. The range {@code [bPos, bEnd)} is used.
  80.      * @param bPos
  81.      *            index into {@code bPath} where the second path starts.
  82.      * @param bEnd
  83.      *            1 past last index of {@code bPath}.
  84.      * @param bMode
  85.      *            mode of the second file. Trees are sorted as though
  86.      *            {@code bPath[bEnd] == '/'}, even if bEnd does not exist.
  87.      * @return &lt;0 if {@code aPath} sorts before {@code bPath}; 0 if the paths
  88.      *         are the same; &gt;0 if {@code aPath} sorts after {@code bPath}.
  89.      */
  90.     public static int compare(byte[] aPath, int aPos, int aEnd, int aMode,
  91.             byte[] bPath, int bPos, int bEnd, int bMode) {
  92.         int cmp = coreCompare(
  93.                 aPath, aPos, aEnd, aMode,
  94.                 bPath, bPos, bEnd, bMode);
  95.         if (cmp == 0) {
  96.             cmp = lastPathChar(aMode) - lastPathChar(bMode);
  97.         }
  98.         return cmp;
  99.     }

  100.     /**
  101.      * Compare two paths, checking for identical name.
  102.      * <p>
  103.      * Unlike {@code compare} this method returns {@code 0} when the paths have
  104.      * the same characters in their names, even if the mode differs. It is
  105.      * intended for use in validation routines detecting duplicate entries.
  106.      * <p>
  107.      * Returns {@code 0} if the names are identical and a conflict exists
  108.      * between {@code aPath} and {@code bPath}, as they share the same name.
  109.      * <p>
  110.      * Returns {@code <0} if all possibles occurrences of {@code aPath} sort
  111.      * before {@code bPath} and no conflict can happen. In a properly sorted
  112.      * tree there are no other occurrences of {@code aPath} and therefore there
  113.      * are no duplicate names.
  114.      * <p>
  115.      * Returns {@code >0} when it is possible for a duplicate occurrence of
  116.      * {@code aPath} to appear later, after {@code bPath}. Callers should
  117.      * continue to examine candidates for {@code bPath} until the method returns
  118.      * one of the other return values.
  119.      *
  120.      * @param aPath
  121.      *            first path buffer. The range {@code [aPos, aEnd)} is used.
  122.      * @param aPos
  123.      *            index into {@code aPath} where the first path starts.
  124.      * @param aEnd
  125.      *            1 past last index of {@code aPath}.
  126.      * @param bPath
  127.      *            second path buffer. The range {@code [bPos, bEnd)} is used.
  128.      * @param bPos
  129.      *            index into {@code bPath} where the second path starts.
  130.      * @param bEnd
  131.      *            1 past last index of {@code bPath}.
  132.      * @param bMode
  133.      *            mode of the second file. Trees are sorted as though
  134.      *            {@code bPath[bEnd] == '/'}, even if bEnd does not exist.
  135.      * @return &lt;0 if no duplicate name could exist;
  136.      *         0 if the paths have the same name;
  137.      *         &gt;0 other {@code bPath} should still be checked by caller.
  138.      */
  139.     public static int compareSameName(
  140.             byte[] aPath, int aPos, int aEnd,
  141.             byte[] bPath, int bPos, int bEnd, int bMode) {
  142.         return coreCompare(
  143.                 aPath, aPos, aEnd, TYPE_TREE,
  144.                 bPath, bPos, bEnd, bMode);
  145.     }

  146.     private static int coreCompare(
  147.             byte[] aPath, int aPos, int aEnd, int aMode,
  148.             byte[] bPath, int bPos, int bEnd, int bMode) {
  149.         while (aPos < aEnd && bPos < bEnd) {
  150.             int cmp = (aPath[aPos++] & 0xff) - (bPath[bPos++] & 0xff);
  151.             if (cmp != 0) {
  152.                 return cmp;
  153.             }
  154.         }
  155.         if (aPos < aEnd) {
  156.             return (aPath[aPos] & 0xff) - lastPathChar(bMode);
  157.         }
  158.         if (bPos < bEnd) {
  159.             return lastPathChar(aMode) - (bPath[bPos] & 0xff);
  160.         }
  161.         return 0;
  162.     }

  163.     private static int lastPathChar(int mode) {
  164.         if ((mode & TYPE_MASK) == TYPE_TREE) {
  165.             return '/';
  166.         }
  167.         return 0;
  168.     }

  169.     private Paths() {
  170.     }
  171. }