Remove local Git repo when working with JGit - jgit

I am building a small tool to propose some Git commands to users not familiar with Git. The commands are not intended to modify the repo, just consult some information.
I am creating the tool in Java, using JGit which seems to be the best match to do this kind of stuff.
The issue I face so far is that I create a temporary folder to host the repo content, but I am unable to delete it automatically at the end of the execution.
Here is the code (I removed the try/catch stuff to simplify the reading):
// Create temporary folder
Path folderPath = Paths.get(System.getProperty("user.dir"));
File localRepoFolder = Files.createTempDirectory(folderPath, "local-repo").toFile();
// Clone the repo
CloneCommand clone = new CloneCommand();
clone.setURI("https://myrepo");
clone.setNoCheckout(true);
clone.setDirectory(localRepoFolder);
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("user", "password"));
Git gitRepo = clone.call();
// Do some stuff
[...]
// Cleanup before closing
gitRepo.getRepository().close();
gitRepo.close();
localRepoFolder.deleteOnExit();
I searched quite a lot on this topic, but I get everywhere that it should be automatically deleted... Am I missing something?

I would use something like Apache Commons IO (http://commons.apache.org/proper/commons-io/) which has a FileUtils.deleteDirectory

Related

with rstudio and github, issue with renamed repo

I had a repository named tags, I renamed it to tag.
I then created a new repository named tags (the old name of the first one).
Now when commiting from R Studio both projects try to commit to the same repository (tags).
I initiated my projects with :
shell("git remote add origin https://github.com/moodymudskipper/tag.git", intern = TRUE)
shell("git push -u origin master",intern = TRUE)
and
shell("git remote add origin https://github.com/moodymudskipper/tags.git",intern = TRUE)
shell("git push -u origin master",intern = TRUE)
And after this I only committed through Rstudio's API and usethis functions, I don't know much more than that about git.
Links to the packages :
https://github.com/moodymudskipper/tag
https://github.com/moodymudskipper/tags
How can I sort this out ?
I'm hesitant to throw this out as an answer, but: you can manually edit the ./.git/config file to update the [remote ...] section to change the remote URL. I have done this confidently enough with an empty repo ...
Check for presence of the tag with grep -rli tags.git .git/*; if all you get is .git/config, then you're good to edit and move on. If you find other files, though, I don't know for certain that they will be updated as you continue with your git remote work. In that case, it might be helpful to look at https://help.github.com/en/articles/changing-a-remotes-url in order to formally change the URL.

How to “git log -S” in JGit

i want to get the changes of a source file with the git command git log -S'hello world'.
It works, but I don't know how to do the same with JGit. The JGit LogCommand has no such option -S.
Here’s a quick example of how to get some log messages:
Git git = new Git(db);
Iterable<RevCommit> log = git.log().call();
UPDATE
Please try to use a modified version of the JGitUtils from the great http://gitblit.com/:
Look at the source of the method getFilesInCommit to see how this can work (PathChangeModel is just used to hold the data.)
also try to read from this tutorial about gitlog in JGIT and dont forget to check properties of gitlog
https://wiki.eclipse.org/JGit/User_Guide#LogCommand_.28git-log.29

How do I add a Nexus resolver after local repository, but before the default repositories?

We have an internal Nexus repository that we use to publish artifacts to and also to cache external dependencies (from Maven Central, Typesafe, etc.)
I want to add the repository as a resolver in my SBT build, under the following restrictions:
The settings need to be part of the build declaration (either .sbt or .scala, but not in the "global" sbt settings
If a dependency exists in the local repository, it should be taken from there. I don't want to have to access the network to get all the dependencies for every build.
If a dependency doesn't exist locally, sbt should first try to get it from the Nexus repository before trying the external repositories.
I saw several similar questions here, but didn't find any solution that does exactly this. Specifically, the code I currently have is:
externalResolvers ~= { rs => nexusResolver +: rs }
But when I show externalResolvers the Nexus repo appears before the local one.
So far, I've come up with the following solution:
externalResolvers ~= { rs =>
val grouped = rs.groupBy(_.isInstanceOf[FileRepository])
val fileRepos = grouped(true)
val remoteRepos = grouped(false)
fileRepos ++ (nexusResolver +: remoteRepos)
}
It works, but is kinda dirty... If anyone has a "cleaner" solution, I'd love to hear it.

Meteor reading CSV files to populate database don't work after deploy

So I have a bunch of data that I want to load into database from CSV. I've hacked together a solution that works in local development, but when I deploy to meteor.com, it no longer works.
I'm loading the csv file in the folder /server/data/:
function readData(name){
var fs = __meteor_bootstrap__.require('fs');
var path = __meteor_bootstrap__.require('path');
var base = path.resolve('.');
var data = fs.readFileSync(path.join(base, '/server/data/', name));
return CSVToArray(data);
}
After I deploy to meteor.com, i got:
INFO Error: ENOENT, no such file or directory '/meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/public/data/categories.csv'
at Object.openSync (fs.js:240:18)
at Object.readFileSync (fs.js:128:15)
at readData (app/server/models.js:10:16)
at app/server/categories.js:6:7
at /meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/bundle/server/server.js:132:63
at Array.forEach (native)
at Function.<anonymous> (/meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/bundle/server/underscore.js:76:11)
at /meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/bundle/server/server.js:132:7
Any idea how I can get meteor to see the csv file after deployment?
I realize this question is old, but it still ranks high on certain keyword searches. So, if you're using Meteor 0.6.5+, you can use the new Assets API.
The issue is that meteor only bundles files that it knows about (ie. JS/CSS/HTML/+more depending on which packages you use) up when it deploys.
Try putting the file you need in the public directory (this directory is exempt from the above rule).
Thanks to SamuelDavis and Tom Coleman's tips. I ended up figuring out what the problem is. Turns out the bundled app is no longer formated as client, public, and server. I ended up debugging it by running meteor bundle to create a tarball. extract the tarball and took a look inside to find where the data folder is. Tom was also right that the data folder needed to be in the public folder in order to get bundled in.
It appears that the base directory is not in the same location that contains the file '/server/data/xxx.csv'.
Before you try anything else, log the base path after calling "var base = path.resolve('.'). If that value is what you expected, log the files that appear in that directory. Again if the files are what you expected, navigate into the /server folder and print out those directories and so forth.
This should pinpoint you to which folder and/or directory is missing and should indicate where you should place the CSV file in future.

JGit PullCommand Exception

We are using git to maintain our source. URL like git#xx.xx.xx.xx:XYZ.git. I'm using JGit to pull the changes.
UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider("xxxx", "xxxx");
localPath = "E:\\murugan\\Test\\GIT_LOCALDEPY";
Git git = new Git(localRepo);
PullCommand pcmd = git.pull();
pcmd.setCredentialsProvider(user);
pcmd.call();
I'm getting the following exception when I execute the code.
org.eclipse.jgit.errors.UnsupportedCredentialItem: ssh://git#xx.xx.xx.xx:22:
org.eclipse.jgit.transport.CredentialItem$StringType:Passphrase for C:\Users\Murugan.SOLVER\.ssh\id_rsa
If username/password security is not an issue, you can specify the credentials as part of the connection in the .git/config file of the local Git repo:
[remote "origin"]
url = ssh://<user>:<pwd>#<host>:22/<remote-path-to-repo>/
fetch = +refs/heads/*:refs/remotes/origin/*
You have to configure your SSH parameters on your machine before using Git. Here is a link, from github, for configuring it.
https://help.github.com/categories/56/articles
especially this one: https://help.github.com/articles/generating-ssh-keys
This will help you set up everything properly (you should adapt everything, since you are probably not connecting to GitHub)

Resources