/** * A conflict is detected - add the three different stages to the index * * @param path the path of the conflicting entry * @param e the previous index entry * @param h the first tree you want to merge (the HEAD) * @param m the second tree you want to merge */ private void conflict( String path, DirCacheEntry e, AbstractTreeIterator h, AbstractTreeIterator m) { conflicts.add(path); DirCacheEntry entry; if (e != null) { entry = new DirCacheEntry(e.getPathString(), DirCacheEntry.STAGE_1); entry.copyMetaData(e, true); builder.add(entry); } if (h != null && !FileMode.TREE.equals(h.getEntryFileMode())) { entry = new DirCacheEntry(h.getEntryPathString(), DirCacheEntry.STAGE_2); entry.setFileMode(h.getEntryFileMode()); entry.setObjectId(h.getEntryObjectId()); builder.add(entry); } if (m != null && !FileMode.TREE.equals(m.getEntryFileMode())) { entry = new DirCacheEntry(m.getEntryPathString(), DirCacheEntry.STAGE_3); entry.setFileMode(m.getEntryFileMode()); entry.setObjectId(m.getEntryObjectId()); builder.add(entry); } }
@Test public void testEntriesWithin() throws Exception { final DirCache dc = db.readDirCache(); final String[] paths = {"a.", "a/b", "a/c", "a/d", "a0b"}; final DirCacheEntry[] ents = new DirCacheEntry[paths.length]; for (int i = 0; i < paths.length; i++) { ents[i] = new DirCacheEntry(paths[i]); ents[i].setFileMode(FileMode.REGULAR_FILE); } final int aFirst = 1; final int aLast = 3; final DirCacheBuilder b = dc.builder(); for (int i = 0; i < ents.length; i++) b.add(ents[i]); b.finish(); assertEquals(paths.length, dc.getEntryCount()); for (int i = 0; i < ents.length; i++) assertSame(ents[i], dc.getEntry(i)); { final DirCacheEntry[] aContents = dc.getEntriesWithin("a"); assertNotNull(aContents); assertEquals(aLast - aFirst + 1, aContents.length); for (int i = aFirst, j = 0; i <= aLast; i++, j++) assertSame(ents[i], aContents[j]); } { final DirCacheEntry[] aContents = dc.getEntriesWithin("a/"); assertNotNull(aContents); assertEquals(aLast - aFirst + 1, aContents.length); for (int i = aFirst, j = 0; i <= aLast; i++, j++) assertSame(ents[i], aContents[j]); } { final DirCacheEntry[] aContents = dc.getEntriesWithin(""); assertNotNull(aContents); assertEquals(ents.length, aContents.length); for (int i = 0; i < ents.length; i++) assertSame(ents[i], aContents[i]); } assertNotNull(dc.getEntriesWithin("a.")); assertEquals(0, dc.getEntriesWithin("a.").length); assertNotNull(dc.getEntriesWithin("a0b")); assertEquals(0, dc.getEntriesWithin("a0b.").length); assertNotNull(dc.getEntriesWithin("zoo")); assertEquals(0, dc.getEntriesWithin("zoo.").length); }
@Test public void testDetectUnmergedPaths() throws Exception { final DirCache dc = db.readDirCache(); final DirCacheEntry[] ents = new DirCacheEntry[3]; ents[0] = new DirCacheEntry("a", 1); ents[0].setFileMode(FileMode.REGULAR_FILE); ents[1] = new DirCacheEntry("a", 2); ents[1].setFileMode(FileMode.REGULAR_FILE); ents[2] = new DirCacheEntry("a", 3); ents[2].setFileMode(FileMode.REGULAR_FILE); final DirCacheBuilder b = dc.builder(); for (int i = 0; i < ents.length; i++) b.add(ents[i]); b.finish(); assertTrue(dc.hasUnmergedPaths()); }
private void update(String path, ObjectId mId, FileMode mode) { if (!FileMode.TREE.equals(mode)) { updated.put(path, mId); DirCacheEntry entry = new DirCacheEntry(path, DirCacheEntry.STAGE_0); entry.setObjectId(mId); entry.setFileMode(mode); builder.add(entry); } }
@Test public void testBuildThenClear() throws Exception { final DirCache dc = db.readDirCache(); final String[] paths = {"a-", "a.b", "a/b", "a0b"}; final DirCacheEntry[] ents = new DirCacheEntry[paths.length]; for (int i = 0; i < paths.length; i++) { ents[i] = new DirCacheEntry(paths[i]); ents[i].setFileMode(FileMode.REGULAR_FILE); } final DirCacheBuilder b = dc.builder(); for (int i = 0; i < ents.length; i++) b.add(ents[i]); b.finish(); assertFalse(dc.hasUnmergedPaths()); assertEquals(paths.length, dc.getEntryCount()); dc.clear(); assertEquals(0, dc.getEntryCount()); assertFalse(dc.hasUnmergedPaths()); }
@Test public void testRejectInvalidWindowsPaths() throws Exception { SystemReader.setInstance( new MockSystemReader() { { setUnix(); } }); String path = "src/con.txt"; DirCache dc = db.lockDirCache(); DirCacheBuilder b = dc.builder(); DirCacheEntry e = new DirCacheEntry(path); e.setFileMode(FileMode.REGULAR_FILE); try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) { e.setObjectId(formatter.idFor(Constants.OBJ_BLOB, Constants.encode(path))); } b.add(e); b.commit(); db.readDirCache(); SystemReader.setInstance( new MockSystemReader() { { setWindows(); } }); try { db.readDirCache(); fail("should have rejected " + path); } catch (CorruptObjectException err) { assertEquals(MessageFormat.format(JGitText.get().invalidPath, path), err.getMessage()); assertNotNull(err.getCause()); assertEquals("invalid name 'CON'", err.getCause().getMessage()); } }
private void keep(DirCacheEntry e) { if (e != null && !FileMode.TREE.equals(e.getFileMode())) builder.add(e); }
private boolean doCheckout() throws CorruptObjectException, IOException, MissingObjectException, IncorrectObjectTypeException, CheckoutConflictException, IndexWriteException { toBeDeleted.clear(); ObjectReader objectReader = repo.getObjectDatabase().newReader(); try { if (headCommitTree != null) preScanTwoTrees(); else prescanOneTree(); if (!conflicts.isEmpty()) { if (failOnConflict) throw new CheckoutConflictException(conflicts.toArray(new String[conflicts.size()])); else cleanUpConflicts(); } // update our index builder.finish(); File file = null; String last = ""; // $NON-NLS-1$ // when deleting files process them in the opposite order as they have // been reported. This ensures the files are deleted before we delete // their parent folders for (int i = removed.size() - 1; i >= 0; i--) { String r = removed.get(i); file = new File(repo.getWorkTree(), r); if (!file.delete() && file.exists()) { // The list of stuff to delete comes from the index // which will only contain a directory if it is // a submodule, in which case we shall not attempt // to delete it. A submodule is not empty, so it // is safe to check this after a failed delete. if (!file.isDirectory()) toBeDeleted.add(r); } else { if (!isSamePrefix(r, last)) removeEmptyParents(new File(repo.getWorkTree(), last)); last = r; } } if (file != null) removeEmptyParents(file); for (String path : updated.keySet()) { // ... create/overwrite this file ... file = new File(repo.getWorkTree(), path); if (!file.getParentFile().mkdirs()) { // ignore } DirCacheEntry entry = dc.getEntry(path); // submodules are handled with separate operations if (FileMode.GITLINK.equals(entry.getRawMode())) continue; checkoutEntry(repo, file, entry, objectReader); } // commit the index builder - a new index is persisted if (!builder.commit()) throw new IndexWriteException(); } finally { objectReader.release(); } return toBeDeleted.size() == 0; }