Send a detailed event on working tree modifications

Currently there is no way to determine the precise changes done
to the working tree by a JGit command. Only the CheckoutCommand
actually provides access to the lists of modified, deleted, and
to-be-deleted files, but those lists may be inaccurate (since they
are determined up-front before the working tree is modified) if
the actual checkout then fails halfway through. Moreover, other
JGit commands that modify the working tree do not offer any way to
figure out which files were changed.

This poses problems for EGit, which may need to refresh parts of the
Eclipse workspace when JGit has done java.io file operations.

Provide the foundations for better file change tracking: the working
tree is modified exclusively in DirCacheCheckout. Make it emit a new
type of RepositoryEvent that lists all files that were modified or
deleted, even if the checkout failed halfway through. We update the
'updated' and 'removed' lists determined up-front in case of file
system problems to reflect the actual state of changes made.

EGit thus can register a listener for these events and then knows
exactly which parts of the Eclipse workspace may need to be refreshed.

Two commands manage checking out individual DirCacheEntries themselves:
checkout specific paths, and applying a stash with untracked files.
Make those two also emit such a new WorkingTreeModifiedEvent.

Furthermore, merges may modify files, and clean, rm, and stash create
may delete files.

CQ: 13969
Bug: 500106
Change-Id: I7a100aee315791fa1201f43bbad61fbae60b35cb
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
Thomas Wolf 2017-07-07 11:26:02 +02:00 committed by Matthias Sohn
parent 81d020aba9
commit b13a285098
14 changed files with 1195 additions and 434 deletions

View File

@ -66,7 +66,6 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- Optional security provider for encryption tests. -->

View File

@ -0,0 +1,108 @@
/*
* Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.events;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* A {@link WorkingTreeModifiedListener} that can be used in tests to check
* expected events.
*/
public class ChangeRecorder implements WorkingTreeModifiedListener {
public static final String[] EMPTY = new String[0];
private Set<String> modified = new HashSet<>();
private Set<String> deleted = new HashSet<>();
private int eventCount;
@Override
public void onWorkingTreeModified(WorkingTreeModifiedEvent event) {
eventCount++;
modified.removeAll(event.getDeleted());
deleted.removeAll(event.getModified());
modified.addAll(event.getModified());
deleted.addAll(event.getDeleted());
}
private String[] getModified() {
return modified.toArray(new String[modified.size()]);
}
private String[] getDeleted() {
return deleted.toArray(new String[deleted.size()]);
}
private void reset() {
eventCount = 0;
modified.clear();
deleted.clear();
}
public void assertNoEvent() {
assertEquals("Unexpected WorkingTreeModifiedEvent ", 0, eventCount);
}
public void assertEvent(String[] expectedModified,
String[] expectedDeleted) {
String[] actuallyModified = getModified();
String[] actuallyDeleted = getDeleted();
Arrays.sort(actuallyModified);
Arrays.sort(expectedModified);
Arrays.sort(actuallyDeleted);
Arrays.sort(expectedDeleted);
assertArrayEquals("Unexpected modifications reported", expectedModified,
actuallyModified);
assertArrayEquals("Unexpected deletions reported", expectedDeleted,
actuallyDeleted);
reset();
}
}

View File

@ -55,12 +55,15 @@
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.api.errors.StashApplyFailureException;
import org.eclipse.jgit.events.ChangeRecorder;
import org.eclipse.jgit.events.ListenerHandle;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -77,15 +80,31 @@ public class StashApplyCommandTest extends RepositoryTestCase {
private File committedFile;
private ChangeRecorder recorder;
private ListenerHandle handle;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
git = Git.wrap(db);
recorder = new ChangeRecorder();
handle = db.getListenerList().addWorkingTreeModifiedListener(recorder);
committedFile = writeTrashFile(PATH, "content");
git.add().addFilepattern(PATH).call();
head = git.commit().setMessage("add file").call();
assertNotNull(head);
recorder.assertNoEvent();
}
@Override
@After
public void tearDown() throws Exception {
if (handle != null) {
handle.remove();
}
super.tearDown();
}
@Test
@ -95,10 +114,12 @@ public void workingDirectoryDelete() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(committedFile));
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertFalse(committedFile.exists());
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { PATH });
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -121,11 +142,13 @@ public void indexAdd() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertFalse(addedFile.exists());
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { addedPath });
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertTrue(addedFile.exists());
assertEquals("content2", read(addedFile));
recorder.assertEvent(new String[] { addedPath }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getChanged().isEmpty());
@ -142,14 +165,17 @@ public void indexAdd() throws Exception {
@Test
public void indexDelete() throws Exception {
git.rm().addFilepattern("file.txt").call();
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { "file.txt" });
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertFalse(committedFile.exists());
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { "file.txt" });
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -170,10 +196,12 @@ public void workingDirectoryModify() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertEquals("content2", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -193,16 +221,21 @@ public void workingDirectoryModifyInSubfolder() throws Exception {
File subfolderFile = writeTrashFile(path, "content");
git.add().addFilepattern(path).call();
head = git.commit().setMessage("add file").call();
recorder.assertNoEvent();
writeTrashFile(path, "content2");
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(subfolderFile));
recorder.assertEvent(new String[] { "d1/d2/f.txt" },
ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertEquals("content2", read(subfolderFile));
recorder.assertEvent(new String[] { "d1/d2/f.txt", "d1/d2", "d1" },
ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -225,10 +258,12 @@ public void workingDirectoryModifyIndexChanged() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertEquals("content3", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -252,10 +287,12 @@ public void workingDirectoryCleanIndexModify() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertEquals("content2", read(committedFile));
recorder.assertEvent(new String[] { "file.txt" }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -281,10 +318,12 @@ public void workingDirectoryDeleteIndexAdd() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertFalse(added.exists());
recorder.assertNoEvent();
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertEquals("content2", read(added));
recorder.assertEvent(new String[] { path }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getChanged().isEmpty());
@ -308,10 +347,12 @@ public void workingDirectoryDeleteIndexEdit() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertEquals("content", read(committedFile));
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
assertFalse(committedFile.exists());
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { PATH });
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -337,9 +378,13 @@ public void multipleEdits() throws Exception {
assertNotNull(stashed);
assertTrue(committedFile.exists());
assertFalse(addedFile.exists());
recorder.assertEvent(new String[] { PATH },
new String[] { "file2.txt" });
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
recorder.assertEvent(new String[] { "file2.txt" },
new String[] { PATH });
Status status = git.status().call();
assertTrue(status.getChanged().isEmpty());
@ -362,6 +407,7 @@ public void workingDirectoryContentConflict() throws Exception {
assertNotNull(stashed);
assertEquals("content", read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
writeTrashFile(PATH, "content3");
@ -372,6 +418,7 @@ public void workingDirectoryContentConflict() throws Exception {
// expected
}
assertEquals("content3", read(PATH));
recorder.assertNoEvent();
}
@Test
@ -391,10 +438,12 @@ public void stashedContentMerge() throws Exception {
assertEquals("content\nhead change\nmore content\n",
read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("committed change").call();
recorder.assertNoEvent();
try {
git.stashApply().call();
@ -402,6 +451,7 @@ public void stashedContentMerge() throws Exception {
} catch (StashApplyFailureException e) {
// expected
}
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
Status status = new StatusCommand(db).call();
assertEquals(1, status.getConflicting().size());
assertEquals(
@ -426,12 +476,15 @@ public void stashedApplyOnOtherBranch() throws Exception {
writeTrashFile(PATH, "master content");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("even content").call();
recorder.assertNoEvent();
git.checkout().setName(otherBranch).call();
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
writeTrashFile(PATH, "otherBranch content");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("even more content").call();
recorder.assertNoEvent();
writeTrashFile(path2, "content\nstashed change\nmore content\n");
@ -442,12 +495,15 @@ public void stashedApplyOnOtherBranch() throws Exception {
assertEquals("otherBranch content",
read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { path2 }, ChangeRecorder.EMPTY);
git.checkout().setName("master").call();
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
git.stashApply().call();
assertEquals("content\nstashed change\nmore content\n", read(file2));
assertEquals("master content",
read(committedFile));
recorder.assertEvent(new String[] { path2 }, ChangeRecorder.EMPTY);
}
@Test
@ -467,12 +523,15 @@ public void stashedApplyOnOtherBranchWithStagedChange() throws Exception {
writeTrashFile(PATH, "master content");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("even content").call();
recorder.assertNoEvent();
git.checkout().setName(otherBranch).call();
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
writeTrashFile(PATH, "otherBranch content");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("even more content").call();
recorder.assertNoEvent();
writeTrashFile(path2,
"content\nstashed change in index\nmore content\n");
@ -485,8 +544,10 @@ public void stashedApplyOnOtherBranchWithStagedChange() throws Exception {
assertEquals("content\nmore content\n", read(file2));
assertEquals("otherBranch content", read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { path2 }, ChangeRecorder.EMPTY);
git.checkout().setName("master").call();
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
git.stashApply().call();
assertEquals("content\nstashed change\nmore content\n", read(file2));
assertEquals(
@ -494,6 +555,7 @@ public void stashedApplyOnOtherBranchWithStagedChange() throws Exception {
+ "[file2.txt, mode:100644, content:content\nstashed change in index\nmore content\n]",
indexState(CONTENT));
assertEquals("master content", read(committedFile));
recorder.assertEvent(new String[] { path2 }, ChangeRecorder.EMPTY);
}
@Test
@ -501,6 +563,7 @@ public void workingDirectoryContentMerge() throws Exception {
writeTrashFile(PATH, "content\nmore content\n");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("more content").call();
recorder.assertNoEvent();
writeTrashFile(PATH, "content\nstashed change\nmore content\n");
@ -508,15 +571,18 @@ public void workingDirectoryContentMerge() throws Exception {
assertNotNull(stashed);
assertEquals("content\nmore content\n", read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
writeTrashFile(PATH, "content\nmore content\ncommitted change\n");
git.add().addFilepattern(PATH).call();
git.commit().setMessage("committed change").call();
recorder.assertNoEvent();
git.stashApply().call();
assertEquals(
"content\nstashed change\nmore content\ncommitted change\n",
read(committedFile));
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
}
@Test
@ -527,6 +593,7 @@ public void indexContentConflict() throws Exception {
assertNotNull(stashed);
assertEquals("content", read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
writeTrashFile(PATH, "content3");
git.add().addFilepattern(PATH).call();
@ -538,6 +605,7 @@ public void indexContentConflict() throws Exception {
} catch (StashApplyFailureException e) {
// expected
}
recorder.assertNoEvent();
assertEquals("content2", read(PATH));
}
@ -549,6 +617,7 @@ public void workingDirectoryEditPreCommit() throws Exception {
assertNotNull(stashed);
assertEquals("content", read(committedFile));
assertTrue(git.status().call().isClean());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
String path2 = "file2.txt";
writeTrashFile(path2, "content3");
@ -557,6 +626,7 @@ public void workingDirectoryEditPreCommit() throws Exception {
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getAdded().isEmpty());
@ -583,12 +653,15 @@ public void stashChangeInANewSubdirectory() throws Exception {
RevCommit stashed = git.stashCreate().call();
assertNotNull(stashed);
assertTrue(git.status().call().isClean());
recorder.assertEvent(ChangeRecorder.EMPTY,
new String[] { subdir, path });
git.branchCreate().setName(otherBranch).call();
git.checkout().setName(otherBranch).call();
ObjectId unstashed = git.stashApply().call();
assertEquals(stashed, unstashed);
recorder.assertEvent(new String[] { path }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertTrue(status.getChanged().isEmpty());
@ -643,12 +716,15 @@ public void testApplyStashWithDeletedFile() throws Exception {
git.commit().setMessage("x").call();
file.delete();
git.rm().addFilepattern("file").call();
recorder.assertNoEvent();
git.stashCreate().call();
recorder.assertEvent(new String[] { "file" }, ChangeRecorder.EMPTY);
file.delete();
git.stashApply().setStashRef("stash@{0}").call();
assertFalse(file.exists());
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { "file" });
}
@Test
@ -660,9 +736,11 @@ public void untrackedFileNotIncluded() throws Exception {
git.add().addFilepattern(PATH).call();
git.stashCreate().call();
assertTrue(untrackedFile.exists());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
git.stashApply().setStashRef("stash@{0}").call();
assertTrue(untrackedFile.exists());
recorder.assertEvent(new String[] { PATH }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertEquals(1, status.getUntracked().size());
@ -684,11 +762,14 @@ public void untrackedFileIncluded() throws Exception {
.call();
assertNotNull(stashedCommit);
assertFalse(untrackedFile.exists());
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { path });
deleteTrashFile("a/b"); // checkout should create parent dirs
git.stashApply().setStashRef("stash@{0}").call();
assertTrue(untrackedFile.exists());
assertEquals("content", read(path));
recorder.assertEvent(new String[] { path }, ChangeRecorder.EMPTY);
Status status = git.status().call();
assertEquals(1, status.getUntracked().size());
@ -706,6 +787,7 @@ public void untrackedFileConflictsWithCommit() throws Exception {
String path = "untracked.txt";
writeTrashFile(path, "untracked");
git.stashCreate().setIncludeUntracked(true).call();
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { path });
writeTrashFile(path, "committed");
head = git.commit().setMessage("add file").call();
@ -719,6 +801,7 @@ public void untrackedFileConflictsWithCommit() throws Exception {
assertEquals(e.getMessage(), JGitText.get().stashApplyConflict);
}
assertEquals("committed", read(path));
recorder.assertNoEvent();
}
@Test
@ -727,6 +810,7 @@ public void untrackedFileConflictsWithWorkingDirectory()
String path = "untracked.txt";
writeTrashFile(path, "untracked");
git.stashCreate().setIncludeUntracked(true).call();
recorder.assertEvent(ChangeRecorder.EMPTY, new String[] { path });
writeTrashFile(path, "working-directory");
try {
@ -736,6 +820,7 @@ public void untrackedFileConflictsWithWorkingDirectory()
assertEquals(e.getMessage(), JGitText.get().stashApplyConflict);
}
assertEquals("working-directory", read(path));
recorder.assertNoEvent();
}
@Test
@ -747,11 +832,13 @@ public void untrackedAndTrackedChanges() throws Exception {
assertTrue(PATH + " should exist", check(PATH));
assertEquals(PATH + " should have been reset", "content", read(PATH));
assertFalse(path + " should not exist", check(path));
recorder.assertEvent(new String[] { PATH }, new String[] { path });
git.stashApply().setStashRef("stash@{0}").call();
assertTrue(PATH + " should exist", check(PATH));
assertEquals(PATH + " should have new content", "changed", read(PATH));
assertTrue(path + " should exist", check(path));
assertEquals(path + " should have new content", "untracked",
read(path));
recorder.assertEvent(new String[] { PATH, path }, ChangeRecorder.EMPTY);
}
}

View File

@ -47,8 +47,10 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.api.CheckoutResult.Status;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
@ -66,6 +68,7 @@
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
@ -175,6 +178,8 @@ private Stage(int number) {
private boolean checkoutAllPaths;
private Set<String> actuallyModifiedPaths;
/**
* @param repo
*/
@ -410,7 +415,8 @@ public CheckoutCommand setAllPaths(boolean all) {
}
/**
* Checkout paths into index and working directory
* Checkout paths into index and working directory, firing a
* {@link WorkingTreeModifiedEvent} if the working tree was modified.
*
* @return this instance
* @throws IOException
@ -418,6 +424,7 @@ public CheckoutCommand setAllPaths(boolean all) {
*/
protected CheckoutCommand checkoutPaths() throws IOException,
RefNotFoundException {
actuallyModifiedPaths = new HashSet<>();
DirCache dc = repo.lockDirCache();
try (RevWalk revWalk = new RevWalk(repo);
TreeWalk treeWalk = new TreeWalk(repo,
@ -432,7 +439,16 @@ protected CheckoutCommand checkoutPaths() throws IOException,
checkoutPathsFromCommit(treeWalk, dc, commit);
}
} finally {
dc.unlock();
try {
dc.unlock();
} finally {
WorkingTreeModifiedEvent event = new WorkingTreeModifiedEvent(
actuallyModifiedPaths, null);
actuallyModifiedPaths = null;
if (!event.isEmpty()) {
repo.fireEvent(event);
}
}
}
return this;
}
@ -461,9 +477,11 @@ public void apply(DirCacheEntry ent) {
int stage = ent.getStage();
if (stage > DirCacheEntry.STAGE_0) {
if (checkoutStage != null) {
if (stage == checkoutStage.number)
if (stage == checkoutStage.number) {
checkoutPath(ent, r, new CheckoutMetadata(
eolStreamType, filterCommand));
actuallyModifiedPaths.add(path);
}
} else {
UnmergedPathException e = new UnmergedPathException(
ent);
@ -472,6 +490,7 @@ public void apply(DirCacheEntry ent) {
} else {
checkoutPath(ent, r, new CheckoutMetadata(eolStreamType,
filterCommand));
actuallyModifiedPaths.add(path);
}
}
});
@ -492,13 +511,15 @@ private void checkoutPathsFromCommit(TreeWalk treeWalk, DirCache dc,
final EolStreamType eolStreamType = treeWalk.getEolStreamType();
final String filterCommand = treeWalk
.getFilterCommand(Constants.ATTR_FILTER_TYPE_SMUDGE);
editor.add(new PathEdit(treeWalk.getPathString()) {
final String path = treeWalk.getPathString();
editor.add(new PathEdit(path) {
@Override
public void apply(DirCacheEntry ent) {
ent.setObjectId(blobId);
ent.setFileMode(mode);
checkoutPath(ent, r,
new CheckoutMetadata(eolStreamType, filterCommand));
actuallyModifiedPaths.add(path);
}
});
}

View File

@ -54,6 +54,7 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
@ -135,6 +136,10 @@ else if (fs.isDirectory(f))
}
} catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e);
} finally {
if (!files.isEmpty()) {
repo.fireEvent(new WorkingTreeModifiedEvent(null, files));
}
}
return files;
}

View File

@ -64,6 +64,7 @@
import org.eclipse.jgit.api.errors.NoMessageException;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.dircache.DirCacheCheckout;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Config.ConfigEnum;
@ -355,6 +356,10 @@ public MergeResult call() throws GitAPIException, NoHeadException,
.getMergeResults();
failingPaths = resolveMerger.getFailingPaths();
unmergedPaths = resolveMerger.getUnmergedPaths();
if (!resolveMerger.getModifiedFiles().isEmpty()) {
repo.fireEvent(new WorkingTreeModifiedEvent(
resolveMerger.getModifiedFiles(), null));
}
} else
noProblems = merger.merge(headCommit, srcCommit);
refLogMessage.append(": Merge made by "); //$NON-NLS-1$

View File

@ -44,8 +44,10 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
@ -53,6 +55,7 @@
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheBuildIterator;
import org.eclipse.jgit.dircache.DirCacheBuilder;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
@ -145,6 +148,7 @@ public DirCache call() throws GitAPIException,
checkCallable();
DirCache dc = null;
List<String> actuallyDeletedFiles = new ArrayList<>();
try (final TreeWalk tw = new TreeWalk(repo)) {
dc = repo.lockDirCache();
DirCacheBuilder builder = dc.builder();
@ -157,11 +161,14 @@ public DirCache call() throws GitAPIException,
if (!cached) {
final FileMode mode = tw.getFileMode(0);
if (mode.getObjectType() == Constants.OBJ_BLOB) {
String relativePath = tw.getPathString();
final File path = new File(repo.getWorkTree(),
tw.getPathString());
relativePath);
// Deleting a blob is simply a matter of removing
// the file or symlink named by the tree entry.
delete(path);
if (delete(path)) {
actuallyDeletedFiles.add(relativePath);
}
}
}
}
@ -171,16 +178,28 @@ public DirCache call() throws GitAPIException,
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfRmCommand, e);
} finally {
if (dc != null)
dc.unlock();
try {
if (dc != null) {
dc.unlock();
}
} finally {
if (!actuallyDeletedFiles.isEmpty()) {
repo.fireEvent(new WorkingTreeModifiedEvent(null,
actuallyDeletedFiles));
}
}
}
return dc;
}
private void delete(File p) {
while (p != null && !p.equals(repo.getWorkTree()) && p.delete())
private boolean delete(File p) {
boolean deleted = false;
while (p != null && !p.equals(repo.getWorkTree()) && p.delete()) {
deleted = true;
p = p.getParentFile();
}
return deleted;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2012, GitHub Inc.
* Copyright (C) 2012, 2017 GitHub Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
@ -44,6 +44,9 @@
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
@ -58,6 +61,7 @@
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.CheckoutConflictException;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
@ -198,7 +202,13 @@ public ObjectId call() throws GitAPIException,
"stash" }); //$NON-NLS-1$
merger.setBase(stashHeadCommit);
merger.setWorkingTreeIterator(new FileTreeIterator(repo));
if (merger.merge(headCommit, stashCommit)) {
boolean mergeSucceeded = merger.merge(headCommit, stashCommit);
List<String> modifiedByMerge = merger.getModifiedFiles();
if (!modifiedByMerge.isEmpty()) {
repo.fireEvent(
new WorkingTreeModifiedEvent(modifiedByMerge, null));
}
if (mergeSucceeded) {
DirCache dc = repo.lockDirCache();
DirCacheCheckout dco = new DirCacheCheckout(repo, headTree,
dc, merger.getResultTreeId());
@ -329,6 +339,7 @@ private void resetIndex(RevTree tree) throws IOException {
private void resetUntracked(RevTree tree) throws CheckoutConflictException,
IOException {
Set<String> actuallyModifiedPaths = new HashSet<>();
// TODO maybe NameConflictTreeWalk ?
try (TreeWalk walk = new TreeWalk(repo)) {
walk.addTree(tree);
@ -361,6 +372,12 @@ private void resetUntracked(RevTree tree) throws CheckoutConflictException,
checkoutPath(entry, reader,
new CheckoutMetadata(eolStreamType, null));
actuallyModifiedPaths.add(entry.getPathString());
}
} finally {
if (!actuallyModifiedPaths.isEmpty()) {
repo.fireEvent(new WorkingTreeModifiedEvent(
actuallyModifiedPaths, null));
}
}
}

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
@ -240,6 +241,7 @@ private Ref getHead() throws GitAPIException {
public RevCommit call() throws GitAPIException {
checkCallable();
List<String> deletedFiles = new ArrayList<>();
Ref head = getHead();
try (ObjectReader reader = repo.newObjectReader()) {
RevCommit headCommit = parseCommit(reader, head.getObjectId());
@ -377,9 +379,11 @@ public void apply(DirCacheEntry ent) {
// Remove untracked files
if (includeUntracked) {
for (DirCacheEntry entry : untracked) {
String repoRelativePath = entry.getPathString();
File file = new File(repo.getWorkTree(),
entry.getPathString());
repoRelativePath);
FileUtils.delete(file);
deletedFiles.add(repoRelativePath);
}
}
@ -394,6 +398,11 @@ public void apply(DirCacheEntry ent) {
return parseCommit(reader, commitId);
} catch (IOException e) {
throw new JGitInternalException(JGitText.get().stashFailed, e);
} finally {
if (!deletedFiles.isEmpty()) {
repo.fireEvent(
new WorkingTreeModifiedEvent(null, deletedFiles));
}
}
}
}

View File

@ -50,6 +50,7 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -61,6 +62,7 @@
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.IndexWriteException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
@ -85,6 +87,7 @@
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FS.ExecutionResult;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.IntList;
import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.jgit.util.SystemReader;
import org.eclipse.jgit.util.io.EolStreamTypeUtil;
@ -151,6 +154,8 @@ public CheckoutMetadata(EolStreamType eolStreamType,
private boolean emptyDirCache;
private boolean performingCheckout;
/**
* @return a list of updated paths and smudgeFilterCommands
*/
@ -432,7 +437,8 @@ void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i,
}
/**
* Execute this checkout
* Execute this checkout. A {@link WorkingTreeModifiedEvent} is fired if the
* working tree was modified; even if the checkout fails.
*
* @return <code>false</code> if this method could not delete all the files
* which should be deleted (e.g. because of of the files was
@ -448,7 +454,17 @@ public boolean checkout() throws IOException {
try {
return doCheckout();
} finally {
dc.unlock();
try {
dc.unlock();
} finally {
if (performingCheckout) {
WorkingTreeModifiedEvent event = new WorkingTreeModifiedEvent(
getUpdated().keySet(), getRemoved());
if (!event.isEmpty()) {
repo.fireEvent(event);
}
}
}
}
}
@ -472,11 +488,13 @@ private boolean doCheckout() throws CorruptObjectException, IOException,
// update our index
builder.finish();
performingCheckout = true;
File file = null;
String last = null;
// 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
IntList nonDeleted = new IntList();
for (int i = removed.size() - 1; i >= 0; i--) {
String r = removed.get(i);
file = new File(repo.getWorkTree(), r);
@ -486,25 +504,47 @@ private boolean doCheckout() throws CorruptObjectException, IOException,
// 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 (!repo.getFS().isDirectory(file))
if (!repo.getFS().isDirectory(file)) {
nonDeleted.add(i);
toBeDeleted.add(r);
}
} else {
if (last != null && !isSamePrefix(r, last))
removeEmptyParents(new File(repo.getWorkTree(), last));
last = r;
}
}
if (file != null)
if (file != null) {
removeEmptyParents(file);
for (Map.Entry<String, CheckoutMetadata> e : updated.entrySet()) {
String path = e.getKey();
CheckoutMetadata meta = e.getValue();
DirCacheEntry entry = dc.getEntry(path);
if (!FileMode.GITLINK.equals(entry.getRawMode()))
checkoutEntry(repo, entry, objectReader, false, meta);
}
removed = filterOut(removed, nonDeleted);
nonDeleted = null;
Iterator<Map.Entry<String, CheckoutMetadata>> toUpdate = updated
.entrySet().iterator();
Map.Entry<String, CheckoutMetadata> e = null;
try {
while (toUpdate.hasNext()) {
e = toUpdate.next();
String path = e.getKey();
CheckoutMetadata meta = e.getValue();
DirCacheEntry entry = dc.getEntry(path);
if (!FileMode.GITLINK.equals(entry.getRawMode())) {
checkoutEntry(repo, entry, objectReader, false, meta);
}
e = null;
}
} catch (Exception ex) {
// We didn't actually modify the current entry nor any that
// might follow.
if (e != null) {
toUpdate.remove();
}
while (toUpdate.hasNext()) {
e = toUpdate.next();
toUpdate.remove();
}
throw ex;
}
// commit the index builder - a new index is persisted
if (!builder.commit())
throw new IndexWriteException();
@ -512,6 +552,36 @@ private boolean doCheckout() throws CorruptObjectException, IOException,
return toBeDeleted.size() == 0;
}
private static ArrayList<String> filterOut(ArrayList<String> strings,
IntList indicesToRemove) {
int n = indicesToRemove.size();
if (n == strings.size()) {
return new ArrayList<>(0);
}
switch (n) {
case 0:
return strings;
case 1:
strings.remove(indicesToRemove.get(0));
return strings;
default:
int length = strings.size();
ArrayList<String> result = new ArrayList<>(length - n);
// Process indicesToRemove from the back; we know that it
// contains indices in descending order.
int j = n - 1;
int idx = indicesToRemove.get(j);
for (int i = 0; i < length; i++) {
if (i == idx) {
idx = (--j >= 0) ? indicesToRemove.get(j) : -1;
} else {
result.add(strings.get(i));
}
}
return result;
}
}
private static boolean isSamePrefix(String a, String b) {
int as = a.lastIndexOf('/');
int bs = b.lastIndexOf('/');

View File

@ -52,6 +52,19 @@
public class ListenerList {
private final ConcurrentMap<Class<? extends RepositoryListener>, CopyOnWriteArrayList<ListenerHandle>> lists = new ConcurrentHashMap<>();
/**
* Register a {@link WorkingTreeModifiedListener}.
*
* @param listener
* the listener implementation.
* @return handle to later remove the listener.
* @since 4.9
*/
public ListenerHandle addWorkingTreeModifiedListener(
WorkingTreeModifiedListener listener) {
return addListener(WorkingTreeModifiedListener.class, listener);
}
/**
* Register an IndexChangedListener.
*

View File

@ -0,0 +1,129 @@
/*
* Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.events;
import java.util.Collections;
import java.util.Collection;
import org.eclipse.jgit.annotations.NonNull;
/**
* A {@link RepositoryEvent} describing changes to the working tree. It is fired
* whenever a {@link org.eclipse.jgit.dircache.DirCacheCheckout} modifies
* (adds/deletes/updates) files in the working tree.
*
* @since 4.9
*/
public class WorkingTreeModifiedEvent
extends RepositoryEvent<WorkingTreeModifiedListener> {
private Collection<String> modified;
private Collection<String> deleted;
/**
* Creates a new {@link WorkingTreeModifiedEvent} with the given
* collections.
*
* @param modified
* repository-relative paths that were added or updated
* @param deleted
* repository-relative paths that were deleted
*/
public WorkingTreeModifiedEvent(Collection<String> modified,
Collection<String> deleted) {
this.modified = modified;
this.deleted = deleted;
}
/**
* Determines whether there are any changes recorded in this event.
*
* @return {@code true} if no files were modified or deleted, {@code false}
* otherwise
*/
public boolean isEmpty() {
return (modified == null || modified.isEmpty())
&& (deleted == null || deleted.isEmpty());
}
/**
* Retrieves the {@link Collection} of repository-relative paths of files
* that were modified (added or updated).
*
* @return the set
*/
public @NonNull Collection<String> getModified() {
Collection<String> result = modified;
if (result == null) {
result = Collections.emptyList();
modified = result;
}
return result;
}
/**
* Retrieves the {@link Collection} of repository-relative paths of files
* that were deleted.
*
* @return the set
*/
public @NonNull Collection<String> getDeleted() {
Collection<String> result = deleted;
if (result == null) {
result = Collections.emptyList();
deleted = result;
}
return result;
}
@Override
public Class<WorkingTreeModifiedListener> getListenerType() {
return WorkingTreeModifiedListener.class;
}
@Override
public void dispatch(WorkingTreeModifiedListener listener) {
listener.onWorkingTreeModified(this);
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.events;
/**
* Receives {@link WorkingTreeModifiedEvent}s, which are fired whenever a
* {@link org.eclipse.jgit.dircache.DirCacheCheckout} modifies
* (adds/deletes/updates) files in the working tree.
*
* @since 4.9
*/
public interface WorkingTreeModifiedListener extends RepositoryListener {
/**
* Respond to working tree modifications.
*
* @param event
*/
void onWorkingTreeModified(WorkingTreeModifiedEvent event);
}