|
22 | 22 | import java.io.File;
|
23 | 23 | import java.io.FileInputStream;
|
24 | 24 | import java.io.IOException;
|
| 25 | +import java.nio.file.Files; |
25 | 26 | import java.time.Instant;
|
26 | 27 | import java.util.Arrays;
|
| 28 | +import java.util.HashSet; |
27 | 29 | import java.util.Map;
|
| 30 | +import java.util.Set; |
28 | 31 |
|
29 | 32 | import org.eclipse.jgit.api.Git;
|
30 | 33 | import org.eclipse.jgit.api.MergeResult;
|
|
51 | 54 | import org.eclipse.jgit.lib.ObjectLoader;
|
52 | 55 | import org.eclipse.jgit.lib.ObjectReader;
|
53 | 56 | import org.eclipse.jgit.lib.ObjectStream;
|
| 57 | +import org.eclipse.jgit.lib.Repository; |
54 | 58 | import org.eclipse.jgit.lib.StoredConfig;
|
55 | 59 | import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
|
56 | 60 | import org.eclipse.jgit.revwalk.RevCommit;
|
@@ -1789,6 +1793,188 @@ public void checkModeMergeConflictInVirtualAncestor(MergeStrategy strategy) thro
|
1789 | 1793 |
|
1790 | 1794 | }
|
1791 | 1795 |
|
| 1796 | + /** |
| 1797 | + * File is binary in ours, theirs and base with different content in each of |
| 1798 | + * them. Content of the file should not change after the merge conflict as |
| 1799 | + * no conflict markers are added to the binary files |
| 1800 | + */ |
| 1801 | + @Theory |
| 1802 | + public void oursBinaryTheirsBinaryBaseBinary(MergeStrategy strategy) |
| 1803 | + throws Exception { |
| 1804 | + Git git = Git.wrap(db); |
| 1805 | + String binaryFile = "file"; |
| 1806 | + |
| 1807 | + writeTrashFile(binaryFile, "\u0000\u0001"); |
| 1808 | + git.add().addFilepattern(binaryFile).call(); |
| 1809 | + RevCommit parent = git.commit().setMessage("BASE COMMIT").call(); |
| 1810 | + String fileHashInBase = getFileHashInWorkTree(git, binaryFile); |
| 1811 | + |
| 1812 | + writeTrashFile(binaryFile, "\u0001\u0002"); |
| 1813 | + git.add().addFilepattern(binaryFile).call(); |
| 1814 | + RevCommit child1 = git.commit().setMessage("THEIRS COMMIT").call(); |
| 1815 | + String fileHashInChild1 = getFileHashInWorkTree(git, binaryFile); |
| 1816 | + |
| 1817 | + git.checkout().setCreateBranch(true).setStartPoint(parent) |
| 1818 | + .setName("side").call(); |
| 1819 | + |
| 1820 | + writeTrashFile(binaryFile, "\u0002\u0000"); |
| 1821 | + git.add().addFilepattern(binaryFile).call(); |
| 1822 | + git.commit().setMessage("OURS COMMIT").call(); |
| 1823 | + String fileHashInChild2 = getFileHashInWorkTree(git, binaryFile); |
| 1824 | + |
| 1825 | + MergeResult mergeResult = git.merge().setStrategy(strategy) |
| 1826 | + .include(child1).call(); |
| 1827 | + |
| 1828 | + // check if the merge caused a conflict |
| 1829 | + assertTrue(mergeResult.getConflicts() != null |
| 1830 | + && !mergeResult.getConflicts().isEmpty()); |
| 1831 | + String fileHashInChild2AfterMerge = getFileHashInWorkTree(git, |
| 1832 | + binaryFile); |
| 1833 | + |
| 1834 | + // check if the file content changed during a conflicting merge |
| 1835 | + assertEquals(fileHashInChild2AfterMerge, fileHashInChild2); |
| 1836 | + |
| 1837 | + Set<String> hashesInIndexFile = new HashSet<>(); |
| 1838 | + DirCache indexContent = git.getRepository().readDirCache(); |
| 1839 | + for (int i = 0; i < indexContent.getEntryCount(); ++i) { |
| 1840 | + DirCacheEntry indexEntry = indexContent.getEntry(i); |
| 1841 | + if (binaryFile.equals(indexEntry.getPathString())) { |
| 1842 | + hashesInIndexFile.add(indexEntry.getObjectId().name()); |
| 1843 | + } |
| 1844 | + } |
| 1845 | + |
| 1846 | + // check if all the three stages are added to index file |
| 1847 | + assertTrue(hashesInIndexFile.contains(fileHashInBase)); |
| 1848 | + assertTrue(hashesInIndexFile.contains(fileHashInChild1)); |
| 1849 | + assertTrue(hashesInIndexFile.contains(fileHashInChild2)); |
| 1850 | + } |
| 1851 | + |
| 1852 | + /** |
| 1853 | + * File is text in ours and theirs with different content but binary in |
| 1854 | + * base. Even in this case, file will be treated as a binary and no conflict |
| 1855 | + * markers are added to it |
| 1856 | + */ |
| 1857 | + @Theory |
| 1858 | + public void oursAndTheirsDifferentTextBaseBinary(MergeStrategy strategy) |
| 1859 | + throws Exception { |
| 1860 | + Git git = Git.wrap(db); |
| 1861 | + String binaryFile = "file"; |
| 1862 | + |
| 1863 | + writeTrashFile(binaryFile, "\u0000\u0001"); |
| 1864 | + git.add().addFilepattern(binaryFile).call(); |
| 1865 | + RevCommit parent = git.commit().setMessage("BASE COMMIT").call(); |
| 1866 | + String fileHashInBase = getFileHashInWorkTree(git, binaryFile); |
| 1867 | + |
| 1868 | + writeTrashFile(binaryFile, "TEXT1"); |
| 1869 | + git.add().addFilepattern(binaryFile).call(); |
| 1870 | + RevCommit child1 = git.commit().setMessage("THEIRS COMMIT").call(); |
| 1871 | + String fileHashInChild1 = getFileHashInWorkTree(git, binaryFile); |
| 1872 | + |
| 1873 | + git.checkout().setCreateBranch(true).setStartPoint(parent) |
| 1874 | + .setName("side").call(); |
| 1875 | + |
| 1876 | + writeTrashFile(binaryFile, "TEXT2"); |
| 1877 | + git.add().addFilepattern(binaryFile).call(); |
| 1878 | + git.commit().setMessage("OURS COMMIT").call(); |
| 1879 | + String fileHashInChild2 = getFileHashInWorkTree(git, binaryFile); |
| 1880 | + |
| 1881 | + MergeResult mergeResult = git.merge().setStrategy(strategy) |
| 1882 | + .include(child1).call(); |
| 1883 | + |
| 1884 | + assertTrue(mergeResult.getConflicts() != null |
| 1885 | + && !mergeResult.getConflicts().isEmpty()); |
| 1886 | + String fileHashInChild2AfterMerge = getFileHashInWorkTree(git, |
| 1887 | + binaryFile); |
| 1888 | + |
| 1889 | + assertEquals(fileHashInChild2AfterMerge, fileHashInChild2); |
| 1890 | + |
| 1891 | + Set<String> hashesInIndexFile = new HashSet<>(); |
| 1892 | + DirCache indexContent = git.getRepository().readDirCache(); |
| 1893 | + for (int i = 0; i < indexContent.getEntryCount(); ++i) { |
| 1894 | + DirCacheEntry indexEntry = indexContent.getEntry(i); |
| 1895 | + if (binaryFile.equals(indexEntry.getPathString())) { |
| 1896 | + hashesInIndexFile.add(indexEntry.getObjectId().name()); |
| 1897 | + } |
| 1898 | + } |
| 1899 | + |
| 1900 | + assertTrue(hashesInIndexFile.contains(fileHashInBase)); |
| 1901 | + assertTrue(hashesInIndexFile.contains(fileHashInChild1)); |
| 1902 | + assertTrue(hashesInIndexFile.contains(fileHashInChild2)); |
| 1903 | + } |
| 1904 | + |
| 1905 | + /** |
| 1906 | + * Tests the scenario where a file is expected to be treated as binary |
| 1907 | + * according to Git attributes |
| 1908 | + */ |
| 1909 | + @Theory |
| 1910 | + public void fileInBinaryInAttribute(MergeStrategy strategy) |
| 1911 | + throws Exception { |
| 1912 | + Git git = Git.wrap(db); |
| 1913 | + String binaryFile = "file.bin"; |
| 1914 | + |
| 1915 | + writeTrashFile(".gitattributes", binaryFile + " binary"); |
| 1916 | + git.add().addFilepattern(".gitattributes").call(); |
| 1917 | + git.commit().setMessage("ADDING GITATTRIBUTES").call(); |
| 1918 | + |
| 1919 | + writeTrashFile(binaryFile, "\u0000\u0001"); |
| 1920 | + git.add().addFilepattern(binaryFile).call(); |
| 1921 | + RevCommit parent = git.commit().setMessage("BASE COMMIT").call(); |
| 1922 | + String fileHashInBase = getFileHashInWorkTree(git, binaryFile); |
| 1923 | + |
| 1924 | + writeTrashFile(binaryFile, "\u0001\u0002"); |
| 1925 | + git.add().addFilepattern(binaryFile).call(); |
| 1926 | + RevCommit child1 = git.commit().setMessage("THEIRS COMMIT").call(); |
| 1927 | + String fileHashInChild1 = getFileHashInWorkTree(git, binaryFile); |
| 1928 | + |
| 1929 | + git.checkout().setCreateBranch(true).setStartPoint(parent) |
| 1930 | + .setName("side").call(); |
| 1931 | + |
| 1932 | + writeTrashFile(binaryFile, "\u0002\u0000"); |
| 1933 | + git.add().addFilepattern(binaryFile).call(); |
| 1934 | + git.commit().setMessage("OURS COMMIT").call(); |
| 1935 | + String fileHashInChild2 = getFileHashInWorkTree(git, binaryFile); |
| 1936 | + |
| 1937 | + MergeResult mergeResult = git.merge().setStrategy(strategy) |
| 1938 | + .include(child1).call(); |
| 1939 | + |
| 1940 | + // check if the merge caused a conflict |
| 1941 | + assertTrue(mergeResult.getConflicts() != null |
| 1942 | + && !mergeResult.getConflicts().isEmpty()); |
| 1943 | + String fileHashInChild2AfterMerge = getFileHashInWorkTree(git, |
| 1944 | + binaryFile); |
| 1945 | + |
| 1946 | + // check if the file content changed during a conflicting merge |
| 1947 | + assertEquals(fileHashInChild2AfterMerge, fileHashInChild2); |
| 1948 | + |
| 1949 | + Set<String> hashesInIndexFile = new HashSet<>(); |
| 1950 | + DirCache indexContent = git.getRepository().readDirCache(); |
| 1951 | + for (int i = 0; i < indexContent.getEntryCount(); ++i) { |
| 1952 | + DirCacheEntry indexEntry = indexContent.getEntry(i); |
| 1953 | + if (binaryFile.equals(indexEntry.getPathString())) { |
| 1954 | + hashesInIndexFile.add(indexEntry.getObjectId().name()); |
| 1955 | + } |
| 1956 | + } |
| 1957 | + |
| 1958 | + // check if all the three stages are added to index file |
| 1959 | + assertTrue(hashesInIndexFile.contains(fileHashInBase)); |
| 1960 | + assertTrue(hashesInIndexFile.contains(fileHashInChild1)); |
| 1961 | + assertTrue(hashesInIndexFile.contains(fileHashInChild2)); |
| 1962 | + } |
| 1963 | + |
| 1964 | + private String getFileHashInWorkTree(Git git, String filePath) |
| 1965 | + throws IOException { |
| 1966 | + Repository repository = git.getRepository(); |
| 1967 | + ObjectInserter objectInserter = repository.newObjectInserter(); |
| 1968 | + |
| 1969 | + File conflictingFile = new File(repository.getWorkTree(), filePath); |
| 1970 | + byte[] fileContent = Files.readAllBytes(conflictingFile.toPath()); |
| 1971 | + ObjectId blobId = objectInserter.insert(Constants.OBJ_BLOB, |
| 1972 | + fileContent); |
| 1973 | + objectInserter.flush(); |
| 1974 | + |
| 1975 | + return blobId.name(); |
| 1976 | + } |
| 1977 | + |
1792 | 1978 | private void writeSubmodule(String path, ObjectId commit)
|
1793 | 1979 | throws IOException, ConfigInvalidException {
|
1794 | 1980 | addSubmoduleToIndex(path, commit);
|
|
0 commit comments