JGit/Android: prevent NetworkOnMainThreadException during commit [message #1212893] |
Wed, 27 November 2013 03:11 |
Andrew F Messages: 2 Registered: November 2013 |
Junior Member |
|
|
If I try to do a api.CommitCommand.call() on the main thread, JGit appears to do a network call to get the hostname, so it can figure out the default email address. Android is a stickler about doing network operations on the UI thread, so it throws a hissy fit (aka NetworkOnMainThreadException) and crashes the app. JGit persists in doing this even after I set the committer and author before calling the command.
So, is it possible to prevent JGit from doing network access while committing? I know I can do the commit in a background task and avoid the issue (and maybe should do that anyway) but right now I'm not sure that's the right solution. It seems silly to have to do a background task just to commit.
My current code. This is in a button handler in the main activity, basically just for playing around:
// create git repository
// the `repo` variable is a related concept specific to my app
// there are already some files in the mentioned RepositoryRoot
FileRepositoryBuilder builder = new FileRepositoryBuilder();
org.eclipse.jgit.lib.Repository gitrepository = builder.setGitDir(new File(repo.getConfig().getRepositoryRoot(), ".git"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
gitrepository.create(false);
// add files and make a commit
Git git = new Git(gitrepository);
AddCommand adder = git.add();
adder.addFilepattern("*");
try {
adder.call();
} catch (NoFilepatternException e) {
// this can literally never happen
} catch (GitAPIException e) {
DialogUtils.QuickToast(this, "failed to add files? the heck?");
}
CommitCommand comm = git.commit();
try {
comm.setMessage("Initial commit for LynxNotes repository")
.setAuthor("LynxNotes", "lynxnotes@example.com?")
.setCommitter("LynxNotes", "lynxnotes@example.com?").call();
} catch (GitAPIException e1) {
DialogUtils.QuickToast(this, "failed to commit");
}
Stack trace:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
...snip...
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getLocalHost(InetAddress.java:365)
at org.eclipse.jgit.util.SystemReader$1.getHostname(SystemReader.java:109)
at org.eclipse.jgit.lib.UserConfig.getDefaultEmail(UserConfig.java:217)
at org.eclipse.jgit.lib.UserConfig.<init>(UserConfig.java:84)
at org.eclipse.jgit.lib.UserConfig.<init>(UserConfig.java:52)
at org.eclipse.jgit.lib.UserConfig$1.parse(UserConfig.java:56)
at org.eclipse.jgit.lib.UserConfig$1.parse(UserConfig.java:54)
at org.eclipse.jgit.lib.Config.get(Config.java:547)
at org.eclipse.jgit.lib.PersonIdent.<init>(PersonIdent.java:81)
at org.eclipse.jgit.internal.storage.file.ReflogWriter.log(ReflogWriter.java:217)
at org.eclipse.jgit.internal.storage.file.RefDirectory.log(RefDirectory.java:704)
at org.eclipse.jgit.internal.storage.file.RefDirectoryUpdate.doUpdate(RefDirectoryUpdate.java:120)
at org.eclipse.jgit.lib.RefUpdate$1.execute(RefUpdate.java:491)
at org.eclipse.jgit.lib.RefUpdate.updateImpl(RefUpdate.java:616)
at org.eclipse.jgit.lib.RefUpdate.update(RefUpdate.java:486)
at org.eclipse.jgit.lib.RefUpdate.update(RefUpdate.java:465)
at org.eclipse.jgit.lib.RefUpdate.forceUpdate(RefUpdate.java:444)
at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:242)
at com.scrivulet.lynxnotes.MainActivity.onCreateRepoButton(MainActivity.java:69)
... 14 more
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03352 seconds