Sort email comment groups
In changes involving C++, *.h files should appear before their corresponding *.cc file, although their lexicographic order is the reverse. With this change, the relative order of *.h and *.cpp file pairs in file groups of change comment emails is corrected. Bug: Issue 4586 Change-Id: Ic9618a07167bde570bdfeb0ea54721c763034863
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
// Copyright (C) 2016 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.common.data;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.Patch;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class FilenameComparator implements Comparator<String> {
|
||||
public static final FilenameComparator INSTANCE = new FilenameComparator();
|
||||
|
||||
private static final Set<String> cppHeaderSuffixes = new HashSet<String>(
|
||||
Arrays.asList(".h", ".hxx", ".hpp"));
|
||||
|
||||
private FilenameComparator() {}
|
||||
|
||||
@Override
|
||||
public int compare(final String path1, final String path2) {
|
||||
if (Patch.COMMIT_MSG.equals(path1) && Patch.COMMIT_MSG.equals(path2)) {
|
||||
return 0;
|
||||
} else if (Patch.COMMIT_MSG.equals(path1)) {
|
||||
return -1;
|
||||
} else if (Patch.COMMIT_MSG.equals(path2)) {
|
||||
return 1;
|
||||
}
|
||||
if (Patch.MERGE_LIST.equals(path1) && Patch.MERGE_LIST.equals(path2)) {
|
||||
return 0;
|
||||
} else if (Patch.MERGE_LIST.equals(path1)) {
|
||||
return -1;
|
||||
} else if (Patch.MERGE_LIST.equals(path2)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int s1 = path1.lastIndexOf('.');
|
||||
int s2 = path2.lastIndexOf('.');
|
||||
if (s1 > 0 && s2 > 0 &&
|
||||
path1.substring(0, s1).equals(path2.substring(0, s2))) {
|
||||
String suffixA = path1.substring(s1);
|
||||
String suffixB = path2.substring(s2);
|
||||
// C++ and C: give priority to header files (.h/.hpp/...)
|
||||
if (cppHeaderSuffixes.contains(suffixA)) {
|
||||
return -1;
|
||||
} else if (cppHeaderSuffixes.contains(suffixB)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return path1.compareTo(path2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2016 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.common.data;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FilenameComparatorTest {
|
||||
private FilenameComparator comparator = FilenameComparator.INSTANCE;
|
||||
|
||||
@Test
|
||||
public void basicPaths() {
|
||||
assertThat(comparator.compare(
|
||||
"abc/xyz/FileOne.java", "xyz/abc/FileTwo.java")).isLessThan(0);
|
||||
assertThat(comparator.compare(
|
||||
"abc/xyz/FileOne.java", "abc/xyz/FileOne.java")).isEqualTo(0);
|
||||
assertThat(comparator.compare(
|
||||
"zzz/yyy/FileOne.java", "abc/xyz/FileOne.java")).isGreaterThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void specialPaths() {
|
||||
assertThat(comparator.compare(
|
||||
"ABC/xyz/FileOne.java", "/COMMIT_MSG")).isGreaterThan(0);
|
||||
assertThat(comparator.compare(
|
||||
"/COMMIT_MSG", "ABC/xyz/FileOne.java")).isLessThan(0);
|
||||
|
||||
assertThat(comparator.compare(
|
||||
"ABC/xyz/FileOne.java", "/MERGE_LIST")).isGreaterThan(0);
|
||||
assertThat(comparator.compare(
|
||||
"/MERGE_LIST", "ABC/xyz/FileOne.java")).isLessThan(0);
|
||||
|
||||
assertThat(comparator.compare(
|
||||
"/COMMIT_MSG", "/MERGE_LIST")).isLessThan(0);
|
||||
assertThat(comparator.compare(
|
||||
"/MERGE_LIST", "/COMMIT_MSG")).isGreaterThan(0);
|
||||
|
||||
assertThat(comparator.compare(
|
||||
"/COMMIT_MSG", "/COMMIT_MSG")).isEqualTo(0);
|
||||
assertThat(comparator.compare(
|
||||
"/MERGE_LIST", "/MERGE_LIST")).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cppExtensions() {
|
||||
assertThat(comparator.compare("abc/file.h", "abc/file.cc")).isLessThan(0);
|
||||
assertThat(comparator.compare("abc/file.c", "abc/file.hpp"))
|
||||
.isGreaterThan(0);
|
||||
assertThat(comparator.compare("abc..xyz.file.h", "abc.xyz.file.cc"))
|
||||
.isLessThan(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user