9

Recently upgraded Jgit to 5.10.0.202012080955-r. After this, we are getting IOException after clone of a bare git repository. It doesn't seem to cause any visible functional impact so far, but its a cause of concern. This issue is seen only in Kubernetes deployment not with Junit tests. Is it a known issue ? If yes, any workarounds or anything missing here ?

Thanks.

[36morg.eclipse.jgit.util.FS                [0;39m [2m:[0;39m Cannot save config file 'FileBasedConfig[/.config/jgit/config]'

java.io.IOException: Creating directories for /.config/jgit failed
    at org.eclipse.jgit.util.FileUtils.mkdirs(FileUtils.java:411) ~[org.eclipse.jgit-5.10.0.202012080955-r.jar!/:5.10.0.202012080955-r]
    at org.eclipse.jgit.internal.storage.file.LockFile.lock(LockFile.java:130) ~[org.eclipse.jgit-5.10.0.202012080955-r.jar!/:5.10.0.202012080955-r]
    at org.eclipse.jgit.storage.file.FileBasedConfig.save(FileBasedConfig.java:219) ~[org.eclipse.jgit-5.10.0.202012080955-r.jar!/:5.10.0.202012080955-r]
    at org.eclipse.jgit.util.FS$FileStoreAttributes.saveToConfig(FS.java:735) ~[org.eclipse.jgit-5.10.0.202012080955-r.jar!/:5.10.0.202012080955-r]
    at org.eclipse.jgit.util.FS$FileStoreAttributes.lambda$4(FS.java:424) ~[org.eclipse.jgit-5.10.0.202012080955-r.jar!/:5.10.0.202012080955-r]
    at org.eclipse.jgit.util.FS$FileStoreAttributes$$Lambda$1660/0x00000000f4008230.run(Unknown Source) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:836) ~[na:na]
Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50
  • 1
    Finally, this issue seems to be coming as Jgit trying to create a config file based on `XDG_CONFIG_HOME`. But in this case, it was not set in pod and default path is not accessible. Setting `XDG_CONFIG_HOME` to appropriate value resolves the issue. – Omkar Shetkar Jan 15 '21 at 07:16
  • 1
    Hi, since you found the answer to your issue, can you post it as an answer to your own question ? – LeGEC Jan 15 '21 at 10:00
  • How can we turn off this behavior altogether? And how can we do it programmatically, using the API, without needing to use an environment variable? I'll open a bounty. – Garret Wilson Jun 10 '23 at 13:56
  • 1
    I've opened [JGit Bug 582064](https://bugs.eclipse.org/bugs/show_bug.cgi?id=582064) to try to get a better approach than setting an environment variable. – Garret Wilson Jun 13 '23 at 21:09

3 Answers3

9

In this particular case Jgit is trying to create a configuration file at a path based on XDG_CONFIG_HOME.

If XDG_CONFIG_HOME is not set, it will be having default value $HOME/.config. This particular path is not accessible in my Kubernetes environment.

Changed XDG_CONFIG_HOME to point to appropriate path. Thus Jgit will be able to create configuration file.

Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50
1

JGit should not need to create a configuration file just to run.

But apparently, it does: $HOME/.config/jgit/config, which has caused issues in the past (for IntelliJ or, as recently as April 2023, Eclipse )

without needing to use an environment variable?

That seems problematic, unless, maybe, if you do implement a custom FS (File System) in JGit.

Something which would include subclassing the FS class in JGit to create your own custom file system.

public class CustomFS extends FS {
    // Override methods as needed
}

Using said CustomFS when you create a Repository through a FileRepositoryBuilder, where you can specify your custom FS:

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(new File("/path/to/repo"))
                         .setFS(new CustomFS())
                         .build();

This way, the repository will use your custom FS, which could prevent the creation of the configuration file in an inaccessible directory.

But that would work only if no restricted APIs are needed.

Using environment variable, though, you could change HOME or XDG_CONFIG_HOME in order to test an alternative location for .config/jgit/config.


From Eclipse JGit issue 582064, Thomas Wolf adds:

The normal way in git is indeed environment variables.

The usual way to get around this programmatically in JGit is to install a custom SystemReader that overrides some variables, or that provides custom system or global configs.

Currently, JGit has a few inconsistencies that might make this hard for the home directory. We're slowly working on this.

Part of it should be fixed in the change I pushed for bug 436127; this should enable one to have a custom SystemReader that returns custom values for getProperty("user.home") and getenv("HOME") to re-define it for JGit. (Or a custom value for getenv("XDG_CONFIG_HOME").)
There's also bug 581875 which is about the global (user) git config being in $XDG_CONFIG_HOME.

As for running in a container where neither $HOME nor $XDG_CONFIG_HOME are defined, and there is no home directory: in a container setup it's really not hard to define either variable to point to an existing directory where JGit has write permissions

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `System.setProperty("XDG_CONFIG_HOME", "/path/to/new/directory";)` (double-check the misplaced semicolon in your answer) doesn't set an environment variable—it sets a system property, which is a different thing. – Garret Wilson Jun 13 '23 at 21:21
  • @GarretWilson True. I have removed the `System.setProperty` which would not be useful here. I will follow your JGit bug ticket. – VonC Jun 13 '23 at 21:26
  • 1
    @GarretWilson I have updated the answer with the latest from [your issue 582064](https://bugs.eclipse.org/bugs/show_bug.cgi?id=582064) – VonC Jun 16 '23 at 15:56
  • If nothing else you get points for taking the time to document status updates (even if it was from my ticket) so I don't have to. Thank you. – Garret Wilson Jun 16 '23 at 16:16
0

Setting XDG_CONFIG_HOME not worked for me. I am using windows machine and below thing worked for me.

Pass below environment variable in your program

HOMEDRIVE=C:;HOMEPATH=\Users\<YourUserName>
c.sankhala
  • 850
  • 13
  • 27