Skip to content

Commit

Permalink
#394 added completion method for completing on file names in the git …
Browse files Browse the repository at this point in the history
…repo
  • Loading branch information
jstrachan committed Jul 16, 2013
1 parent 07c9685 commit 0b2c2ad
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
55 changes: 54 additions & 1 deletion hawtio-git/src/main/java/io/hawt/git/GitFacade.java
Expand Up @@ -303,6 +303,59 @@ public FileContents call() throws Exception {
});
}

/**
* Provides a file/path completion hook so we can start typing the name of a file or directory
*/
public List<String> completePath(String completionText, boolean directoriesOnly) {
boolean empty = Strings.isBlank(completionText);
String pattern = completionText;
File file = getFile(completionText);
String prefix = completionText;
if (file.exists()) {
pattern = "";
} else {
String startPath = ".";
if (!empty) {
int idx = completionText.lastIndexOf('/');
if (idx >= 0) {
startPath = completionText.substring(0, idx);
if (startPath.length() == 0) {
startPath = "/";
}
pattern = completionText.substring(idx + 1);
}
}
file = getFile(startPath);
prefix = startPath;
}
if (prefix.length() > 0 && !prefix.endsWith("/")) {
prefix += "/";
}
if (prefix.equals("./")) {
prefix = "";
}
File[] list = file.listFiles();
List<String> answer = new ArrayList<String>();
for (File aFile : list) {
String name = aFile.getName();
if (pattern.length() == 0 || name.contains(pattern)) {
if (!isIgnoreFile(aFile) && (!directoriesOnly || aFile.isDirectory())) {
answer.add(prefix + name);
}
}
}
return answer;
}


protected String removeLeadingSlash(String path) {
if (path.startsWith("/")) {
return path.substring(1);
} else {
return path;
}
}

protected boolean isIgnoreFile(File child) {
return child.getName().startsWith(".");
}
Expand Down Expand Up @@ -729,7 +782,7 @@ protected void doPull() {
*/
public File getFile(String path) {
File rootDir = getConfigDirectory();
return new File(rootDir, path);
return new File(rootDir, removeLeadingSlash(path));
}


Expand Down
5 changes: 5 additions & 0 deletions hawtio-git/src/main/java/io/hawt/git/GitFacadeMXBean.java
Expand Up @@ -38,6 +38,11 @@ void remove(String branch, String path, String commitMessage,
*/
String getContent(String objectId, String blobPath);

/**
* Provides a file/path completion hook so we can start typing the name of a file or directory
*/
List<String> completePath(String completionText, boolean directoriesOnly);

/**
* Reads the child JSON file contents which match the given search string (if specified) and which match the given file name wildcard (using * to match any characters in the name).
*/
Expand Down
15 changes: 15 additions & 0 deletions hawtio-git/src/test/java/io/hawt/git/GitFacadeTest.java
Expand Up @@ -234,6 +234,21 @@ public void createFileAndListDirectory() throws Exception {
assertTrue("Should contain camel-spring but was " + list, list.contains("http://camel.apache.org/schema/spring"));
}
}


// now lets test the completions
assertCompletePaths("", true, "another", "foo");
assertCompletePaths("/", true, "/another", "/foo");
assertCompletePaths("/another", true, "/another/thing");
assertCompletePaths("another", true, "another/thing");
assertCompletePaths("/foo", false, "/foo/1.json", "/foo/2.json");
assertCompletePaths("foo", false, "foo/1.json", "foo/2.json");
}

protected void assertCompletePaths(String completePath, boolean directoriesOnly, String... expected) {
List<String> expectedList = Arrays.asList(expected);
List<String> paths = git.completePath(completePath, directoriesOnly);
assertEquals("complete paths for '" + completePath + "' and directoriesOnly " + directoriesOnly, expectedList, paths);
}

protected String assertReadFileContents(String path, String expectedContents) throws IOException, GitAPIException {
Expand Down

0 comments on commit 0b2c2ad

Please sign in to comment.