Skip to content

Commit

Permalink
added a rename API to the GitFacade so that files can be renamed or m…
Browse files Browse the repository at this point in the history
…oved
  • Loading branch information
jstrachan committed Jul 15, 2013
1 parent d0f0e73 commit 88cd08a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
26 changes: 24 additions & 2 deletions hawtio-git/src/main/java/io/hawt/git/GitFacade.java
Expand Up @@ -386,8 +386,30 @@ protected static String getFilePattern(String path) {
return filePattern;
}

public void move(String branch, String oldPath, String newPath) {
// TODO
public void rename(String branch, final String oldPath, final String newPath, final String commitMessage,
final String authorName, final String authorEmail) {
final PersonIdent personIdent = new PersonIdent(authorName, authorEmail);
gitOperation(personIdent, new Callable<RevCommit>() {
public RevCommit call() throws Exception {
File file = getFile(oldPath);
File newFile = getFile(newPath);

if (file.exists()) {
File parentFile = newFile.getParentFile();
parentFile.mkdirs();
if (!parentFile.exists()) {
throw new IOException("Could not create directory " + parentFile + " when trying to move " + file + " to " + newFile + ". Maybe a file permission issue?");
}
file.renameTo(newFile);
String filePattern = getFilePattern(newPath);
git.add().addFilepattern(filePattern).call();
CommitCommand commit = git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
return commitThenPush(commit);
} else {
return null;
}
}
});
}

public void remove(final String branch, final String path, final String commitMessage,
Expand Down
9 changes: 6 additions & 3 deletions hawtio-git/src/main/java/io/hawt/git/GitFacadeMXBean.java
Expand Up @@ -17,11 +17,14 @@ public interface GitFacadeMXBean {
void write(String branch, String path, String commitMessage,
String authorName, String authorEmail, String contents);

void remove(String branch, String path, String commitMessage,
/**
* Renames the given oldPath to the newPath location for the given branch, commit message and user
*/
void rename(String branch, String oldPath, String newPath, String commitMessage,
String authorName, String authorEmail);

// TODO
// void move(String branch, String oldPath, String newPath);
void remove(String branch, String path, String commitMessage,
String authorName, String authorEmail);

String getHEAD();

Expand Down
21 changes: 21 additions & 0 deletions hawtio-git/src/test/java/io/hawt/git/GitFacadeTest.java
Expand Up @@ -27,6 +27,17 @@
public class GitFacadeTest {
GitFacade git = createTestGitFacade();

public static void main(String[] args) {
GitFacadeTest test = new GitFacadeTest();
try {
test.init();
test.createFileAndListDirectory();
test.destroy();
} catch (Throwable e) {
System.out.println("FAILED: " + e);
e.printStackTrace();
}
}
public static File targetDir() {
String basedir = System.getProperty("basedir", ".");
return new File(basedir + "/target");
Expand Down Expand Up @@ -151,6 +162,16 @@ public void createFileAndListDirectory() throws Exception {
System.out.println();
}

// now lets try rename a file
String newReadMePath = "NewReadMeFile.md";
git.rename(this.branch, readMePath, newReadMePath, "Renaming file", authorName, authorEmail);
assertReadFileContents(newReadMePath, readMeContent);

// now lets try move it to a completely different directory
String newDirectoryReadMePath = "another/thing/NewReadMeFile.md";
git.rename(this.branch, newReadMePath, newDirectoryReadMePath, "Moving file to another directory", authorName, authorEmail);
assertReadFileContents(newDirectoryReadMePath, readMeContent);

// now lets make a new git facade to check we can work with existing repos
GitFacade anotherGit = createTestGitFacade();
anotherGit.setObjectName(new ObjectName("io.hawt.git:type=GitFacadePart2"));
Expand Down

0 comments on commit 88cd08a

Please sign in to comment.