Reputation: 504
I wrote custom ant task which uses jgit. It should take local repository address together with directory address and add all files in the directory to index. It builds successfully but unfortunately it doesn't work. I'm totally new to that, so I wonder if some of you could help me finding the problem or what i am missing. Here's my code:
package customGitTasks;
import java.io.File;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.Git;
public class GitAdd extends Task{
private String dir;
private String repository;
public void setRepository(String repository) {
this.repository = repository;
}
public void setDir(String dir) {
this.dir = dir;
}
public void execute() throws BuildException {
try {
Git git = Git.open(new File(repository));
AddCommand add = git.add();
add.addFilepattern(dir).call();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Thank you
Upvotes: 0
Views: 230
Reputation: 10377
JGit provides a git-add ant task among other git related tasks (gitadd, gitcheckout, gitclone, gitinit).
Either use that task => get the jarfile here or check the sources of gitadd task for inspiration.
Beware - when running on Windows OS you need to patch the git-add task to make it work :
// original line 149
//return new File(file).getCanonicalPath().substring(prefix.length() + 1);
String result = new File(file).getCanonicalPath().substring(prefix.length() + 1);
if (File.separatorChar != '/') {
result = result.replace(File.separatorChar, '/');
}
return result;
means the fileseparator has to be unix style.
Upvotes: 2