How to checkout a certain folder with JGit - java

FetchResult fr = git.fetch().setCredentialsProvider(credentials).setCheckFetchedObjects(true).Call();
git.checkout().setCreateBranch(true).setName("origin/" + branchName).setStartPoint("origin/" + branchName + "path/to/folder").call()
This is the code I'm using to check out a single folder from a remote repository.
Equivalent git commands are:
git fetch origin
git checkout origin/branch -- path/to/folder
But, the Java code doesn't work for me, I was only able to initialise the local repository and configure remote repository. The checkout didn't work and I couldn't find out what mistake I'm making.

Try making changes in the checkout part like this
"origin/"
and give it a try.
Otherwise you want to do is a sparse checkout
How do I implement sparse checkout in JGit?

In order to check out a particular folder with JGit (a sparse checkout), you need to tell the CheckoutCommand which folder to check out.
For example:
git.checkout().setName( "branch-to-check-out" ).addPath( "path/to/folder" ).call();
addPath() can be called multiple times to check out each of the given paths. The path is interpreted relative to the work directory.

Related

Git: Comparing two files in two branches [duplicate]

Is it possible to open a file in a git branch without checking out that branch? How?
Essentially I want to be able to open a file in my github pages branch without switching branches all the time. I don't want to modify it, just want to view it.
This should work:
git show branch:file
Where branch can be any ref (branch, tag, HEAD, ...) and file is the full path of the file. To export it you could use
git show branch:file > exported_file
You should also look at VonC's answers to some related questions:
How to retrieve a single file from specific revision in Git?
How to get just one file from another branch
UPDATE 2015-01-19:
Nowadays you can use relative paths with git show a1b35:./file.txt.
git show somebranch:path/to/your/file
you can also do multiple files and have them concatenated:
git show branchA~10:fileA branchB^^:fileB
You do not have to provide the full path to the file, relative paths are acceptable e.g.:
git show branchA~10:../src/hello.c
If you want to get the file in the local directory (revert just one file) you can checkout:
git checkout somebranch^^^ -- path/to/file
A simple, newbie friendly way for looking into a file:
git gui browser <branch> which lets you explore the contents of any file.
It's also there in the File menu of git gui. Most other -more advanced- GUI wrappers (Qgit, Egit, etc..) offer browsing/opening files as well.
If you're using Emacs, you can type C-x v ~ or M-x vc-revision-other-window to see a different revision of the file you're currently editing (tags, branches and hashes all work).
Add the following to your ~/.gitconfig file
[alias]
cat = "!git show \"$1:$2\" #"
And then try this
git cat BRANCHNAME FILEPATH
Personally I prefer separate parameters without a colon. Why? This choice mirrors the parameters of the checkout command, which I tend to use rather frequently and I find it thus much easier to remember than the bizarro colon-separated parameter of the show command.

How to delete a file locked by the Java Platform?

I am currently developing an app which clones Git repositories thanks to JGit (http://wiki.eclipse.org/JGit/User_Guide) every time a user logs on. When the user wants to quit the app, I want to delete the clone.
Here's the problem : when cloning a repository, a folder .git is created, in which can be found a file .pack (.git/objects/pack/sutpideFile.pack) and which cannot be deleted, because the Java Platform is locking it (when trying to delete it by hand, get the error 'The action can't be completed because file is open in Java(TM) Platform SE binary').
THIS IS A KNOWN PROBLEM with Jgit : .pack file from git repo can't be deleted using File delete() method.
Thus I have used the solution proposed here : https://github.com/ajoberstar/grgit/issues/33 which is to add those three lines before my deleting method :
WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitMMAP(true);
WindowCache.install(config);
BUT what really bothers me because I do not understand is that this solution works only once: I launch the server (TomCat), connect, and then disconnect. Here, the whole folder is deleted. However, when I re-connect and disconnect (without re-launching the sever), there rebels the files and am I not able anymore to delete it until I shut down the server.
Has anybody the slighest idea why it works, but only once ?
Thanks for your help,
EDIT :
Well, so I just needed to add git.getRepository().close(); when I finish to use the Git object. Then the deletion is possible !
This is a know bug in JGit, see the discussion at How do I release file system locks after cloning repo via JGit
Basically you currently need to add the call to "Git.getRepository().close()" in order to free all file system locks until a new version of JGit is released.
result = Git.cloneRepository()
.setURI( 'https://github.com/github/testrepo.git' )
.setDirectory( localPath )
.call();
// this is currently necessary to free all file locks
result.getRepository().close();
result.close();
JGit 4.1 is scheduled to have a fix for this included.

Is it possible to ignore a file once it is already tracked in Git?

Is it possible to add a dummy configuration file once (so that other developers can see how the configuration file should look) and then ignore it using the .gitignore file. Then replace the dummy config details with working ones so I can continue to develop the project and keep committing changes? But the original dummy config file will remain intact on Github?
This is the approach I attempted:
create repository on Github
intitialise local repo
pull remote repo
push local repo to github with Configuration.java (containing dummy details)
add Configuration.java to the .gitignore file
push local repo to github
add correct details to Configuration.java
push local repo to github
But the change to configuration.java is tracked, so this doesn't work.
If you are just worried about ignoring changes in your local dev repo, you can use git update-index --assume-unchanged <file> to ignore any changes to the file. See http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/ for more details.
You mention :
push local repo to github with Configuration.java (containing dummy
details)
add Configuration.java to the .gitignore file
The problem with this is, gitignore only works on files that aren't already being tracked, and since you've committed it it's too late for the gitignore to work. To quote the gitignore docs:
A gitignore file specifies intentionally untracked files that Git
should ignore. Files already tracked by Git are not affected;
If you want to commit your own work to a publicly visible repo, but want to hide sensitive info such as connection strings / passwords etc, then commit a dummy file first. For example, commit something like this:
public class Configuration {
public static final String USERNAME = "someUsername";
public static final String PASSWORD = "passwordHere";
}
Then, tell git to stop tracking changes on that file, so you can amend the values locally but not be prompted to commit those changes.
git update-index --assume-unchanged path/to/file.txt
A couple of things to point out:
Always check in a dummy config file, don't leave it out entirely. If
you do, when people checkout your project and the file is missing,
they won't be able to compile the project! Help them by giving some
sample data.
As git is no longer tracking that file, if you make any modifications
that you DO want to commit, you'll have to manually cater to that
(either do it via github.com or turn tracking back on, fix it, then
disable tracking again). For this purpose, keep that file relatively light, just variables, don't put logic in there that you may need to keep updating.

JGIT: git checkout --

I have a not staged modified file and I would like to discard the changes.
In git it would be something like
git checkout -- .
How can I emulate this behavior with JGit ?
Thanks in advance.
To revert a single file, you could use the CleanCommand:
Set<String> paths = new HashSet<String>();
paths.add( ... );
git.clean().setPaths( paths ).call();
Unfortunately there is a bug that prevents the CleanCommand to reset files in sub-directories.
If I interpret the '.' in git checkout -- . correctly you want to revert all changes in the work directory. The ResetCommand does that:
git.reset().setMode( ResetType.HARD ).call();
This would also override the index with the contents from HEAD.
If you don't care about the index, you could also read the file-contents from the HEAD commit and write them to the work directory yourself. Let me know if that is of interest for you and I will try to assemble a snippet that does so.

svnkit: how to get working copy revision number?

I am using SVNKit 1.3.2 and trying to obtain working copy revision number.
Working copy is checkouted also with SVNKit, but when I'm trying to call
clientManager.getLookClient().doGetYoungestRevision(destination);
I got FileNotFoundException telling me that 'format' file is not found under destination path, ex /path/to/working/copy/format
I can see 2 troubles:
- It tries to access 'format' file right in working directory root, while this file supposed to be in .svn folder
There is not file in this .svn folder.
Any clues? Thank you!
Seems to be it should be done through SVNStatusClient:
clientManager.getStatusClient().doStatus(destination, false).getRevision().getNumber();
Though still don't understand what's the problem with doGetYoungestRevision(destination)...
It is not working because doGetYoungestRevision is for a repository, not for a working copy.
I was hoping to achieve the same things as you, and found out that when destination is pointing to a repository, it returns the last revision number of the repository.
The class in itself is for working with repository, you can look at the documentation here.

Categories

Resources