Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcs: add method Repository.deleteUntrackedFiles #732

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/Repository.java
Expand Up @@ -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());
}
Expand Down
11 changes: 11 additions & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/git/GitRepository.java
Expand Up @@ -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"));
Expand Down
11 changes: 11 additions & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/hg/HgRepository.java
Expand Up @@ -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")) {
Expand Down
25 changes: 25 additions & 0 deletions vcs/src/test/java/org/openjdk/skara/vcs/RepositoryTests.java
Expand Up @@ -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));
}
}
}