Skip to content
Permalink
Browse files
vcs: add method Repository.deleteUntrackedFiles
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Aug 24, 2020
1 parent cfaa082 commit b15e755df6a54d73a37318e612abd23c6cc5316c
Showing 4 changed files with 48 additions and 0 deletions.
@@ -147,6 +147,7 @@ default void setPaths(String remote, String pullPath) throws IOException {
}
void addSubmodule(String pullPath, Path path) throws IOException;
void updateSubmodule(Path path) throws IOException;
void deleteUntrackedFiles() throws IOException;
default void updateSubmodule(Submodule s) throws IOException {
updateSubmodule(s.path());
}
@@ -365,6 +365,17 @@ public void clean() throws IOException {
}
}

@Override
public void deleteUntrackedFiles() throws IOException {
var root = root();
try (var p = capture("git", "ls-files", "--full-name", "--other")) {
var res = await(p);
for (var line : res.stdout()) {
Files.delete(root.resolve(line));
}
}
}

@Override
public void reset(Hash target, boolean hard) throws IOException {
var cmd = new ArrayList<>(List.of("git", "reset"));
@@ -358,6 +358,17 @@ public boolean isHealthy() throws IOException {
Files.exists(Path.of(root, ".hg", "store", "lock")));
}

@Override
public void deleteUntrackedFiles() throws IOException {
var root = root();
try (var p = capture("hg", "status", "--unknown", "--no-status")) {
var res = await(p);
for (var line : res.stdout()) {
Files.delete(root.resolve(line));
}
}
}

@Override
public void clean() throws IOException {
try (var p = capture("hg", "merge", "--abort")) {
@@ -2571,4 +2571,29 @@ void testFastForwardMerge(VCS vcs) throws IOException {
assertEquals(2, r.commits().asList().size());
}
}

@ParameterizedTest
@EnumSource(VCS.class)
void testDeleteUntrackedFiles(VCS vcs) throws IOException {
try (var dir = new TemporaryDirectory()) {
var r = Repository.init(dir.path(), vcs);

var readme = dir.path().resolve("README");
Files.write(readme, List.of("Hello, readme!"));

r.add(readme);
var hash1 = r.commit("Add README", "duke", "duke@openjdk.java.net");
var untracked = dir.path().resolve("UNTRACKED");
Files.write(untracked, List.of("Hello, untracked!"));

var paths = Files.list(r.root()).collect(Collectors.toList());
assertTrue(paths.contains(untracked));
assertTrue(paths.contains(readme));

r.deleteUntrackedFiles();
paths = Files.list(r.root()).collect(Collectors.toList());
assertFalse(paths.contains(untracked));
assertTrue(paths.contains(readme));
}
}
}

1 comment on commit b15e755

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on b15e755 Aug 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.