1 /*
2 * Copyright (C) 2016, 2018 Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.util;
45
46 import static org.eclipse.jgit.lib.FileMode.TYPE_GITLINK;
47 import static org.eclipse.jgit.lib.FileMode.TYPE_MASK;
48 import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
49
50 /**
51 * Utility functions for paths inside of a Git repository.
52 *
53 * @since 4.2
54 */
55 public class Paths {
56 /**
57 * Remove trailing {@code '/'} if present.
58 *
59 * @param path
60 * input path to potentially remove trailing {@code '/'} from.
61 * @return null if {@code path == null}; {@code path} after removing a
62 * trailing {@code '/'}.
63 */
64 public static String stripTrailingSeparator(String path) {
65 if (path == null || path.isEmpty()) {
66 return path;
67 }
68
69 int i = path.length();
70 if (path.charAt(path.length() - 1) != '/') {
71 return path;
72 }
73 do {
74 i--;
75 } while (path.charAt(i - 1) == '/');
76 return path.substring(0, i);
77 }
78
79 /**
80 * Compare two paths according to Git path sort ordering rules.
81 *
82 * @param aPath
83 * first path buffer. The range {@code [aPos, aEnd)} is used.
84 * @param aPos
85 * index into {@code aPath} where the first path starts.
86 * @param aEnd
87 * 1 past last index of {@code aPath}.
88 * @param aMode
89 * mode of the first file. Trees are sorted as though
90 * {@code aPath[aEnd] == '/'}, even if aEnd does not exist.
91 * @param bPath
92 * second path buffer. The range {@code [bPos, bEnd)} is used.
93 * @param bPos
94 * index into {@code bPath} where the second path starts.
95 * @param bEnd
96 * 1 past last index of {@code bPath}.
97 * @param bMode
98 * mode of the second file. Trees are sorted as though
99 * {@code bPath[bEnd] == '/'}, even if bEnd does not exist.
100 * @return <0 if {@code aPath} sorts before {@code bPath};
101 * 0 if the paths are the same;
102 * >0 if {@code aPath} sorts after {@code bPath}.
103 */
104 public static int compare(byte[] aPath, int aPos, int aEnd, int aMode,
105 byte[] bPath, int bPos, int bEnd, int bMode) {
106 int cmp = coreCompare(
107 aPath, aPos, aEnd, aMode,
108 bPath, bPos, bEnd, bMode);
109 if (cmp == 0) {
110 cmp = modeCompare(aMode, bMode);
111 }
112 return cmp;
113 }
114
115 /**
116 * Compare two paths, checking for identical name.
117 * <p>
118 * Unlike {@code compare} this method returns {@code 0} when the paths have
119 * the same characters in their names, even if the mode differs. It is
120 * intended for use in validation routines detecting duplicate entries.
121 * <p>
122 * Returns {@code 0} if the names are identical and a conflict exists
123 * between {@code aPath} and {@code bPath}, as they share the same name.
124 * <p>
125 * Returns {@code <0} if all possibles occurrences of {@code aPath} sort
126 * before {@code bPath} and no conflict can happen. In a properly sorted
127 * tree there are no other occurrences of {@code aPath} and therefore there
128 * are no duplicate names.
129 * <p>
130 * Returns {@code >0} when it is possible for a duplicate occurrence of
131 * {@code aPath} to appear later, after {@code bPath}. Callers should
132 * continue to examine candidates for {@code bPath} until the method returns
133 * one of the other return values.
134 *
135 * @param aPath
136 * first path buffer. The range {@code [aPos, aEnd)} is used.
137 * @param aPos
138 * index into {@code aPath} where the first path starts.
139 * @param aEnd
140 * 1 past last index of {@code aPath}.
141 * @param bPath
142 * second path buffer. The range {@code [bPos, bEnd)} is used.
143 * @param bPos
144 * index into {@code bPath} where the second path starts.
145 * @param bEnd
146 * 1 past last index of {@code bPath}.
147 * @param bMode
148 * mode of the second file. Trees are sorted as though
149 * {@code bPath[bEnd] == '/'}, even if bEnd does not exist.
150 * @return <0 if no duplicate name could exist;
151 * 0 if the paths have the same name;
152 * >0 other {@code bPath} should still be checked by caller.
153 */
154 public static int compareSameName(
155 byte[] aPath, int aPos, int aEnd,
156 byte[] bPath, int bPos, int bEnd, int bMode) {
157 return coreCompare(
158 aPath, aPos, aEnd, TYPE_TREE,
159 bPath, bPos, bEnd, bMode);
160 }
161
162 private static int coreCompare(
163 byte[] aPath, int aPos, int aEnd, int aMode,
164 byte[] bPath, int bPos, int bEnd, int bMode) {
165 while (aPos < aEnd && bPos < bEnd) {
166 int cmp = (aPath[aPos++] & 0xff) - (bPath[bPos++] & 0xff);
167 if (cmp != 0) {
168 return cmp;
169 }
170 }
171 if (aPos < aEnd) {
172 return (aPath[aPos] & 0xff) - lastPathChar(bMode);
173 }
174 if (bPos < bEnd) {
175 return lastPathChar(aMode) - (bPath[bPos] & 0xff);
176 }
177 return 0;
178 }
179
180 private static int lastPathChar(int mode) {
181 if ((mode & TYPE_MASK) == TYPE_TREE) {
182 return '/';
183 }
184 return 0;
185 }
186
187 private static int modeCompare(int aMode, int bMode) {
188 if ((aMode & TYPE_MASK) == TYPE_GITLINK
189 || (bMode & TYPE_MASK) == TYPE_GITLINK) {
190 // Git links can be equal to files or folders
191 return 0;
192 }
193 return lastPathChar(aMode) - lastPathChar(bMode);
194 }
195
196 private Paths() {
197 }
198 }