Can Eclipse's Refactor > Move be integrated with Git? - java

One of the great things about using an IDE for Java is the automated refactorings you get. The problem I'm having is that after using Refactor > Move to move a class into a different package (which moves the file itself in the filesystem), git status shows that the file in the old location has been deleted, and the one in the new location has been added.
The workaround I've found is clunky:
mv src/com/example/newpackage/Foo.java src/com/example/oldpackage/Foo.java
git mv src/com/example/oldpackage/Foo.java src/com/example/newpackage/Foo.java
Is there any way (when using the Git plugin for Eclipse) to have the refactoring do a git mv instead of a naive filesystem move?

That's the way how Git works with renames/moves (delete old file and add new file). It then detects the contents of the file, and recognizes a rename based on an algorithm. So even it shows you delete and add, if you commit and then do a "git log --follow movedfilename", it should show you the whole history, even the history before the rename.

Related

Intellij - Git status shows files have been changed when they have not

I am working on a Java project in Intellij that uses git. Quite a few files are blue (to show that changes have been made), however when I right click them and click on "Git -> Compare with Latest Repository Version" it says that the contents are identical. Anyone know why this happens? It only seems to happen to files that I've opened to look at but haven't changed. Could it happen if I accidentally added extra white space and then deleted it or something? Or just extra whitespace in general?
This is how GIT is different from SVN. GIT's change detection algorithm does not depend only on the content of the file but the meta data (timestamp last modified, etc) of the file as well. So even if you are adding just one space and removing it later on; if you save it, it modifies the metadata of the file.
For more details, you can have a look at: What algorithm does git use to detect changes on your working tree?

When do I commit when moving files in a git? (Jgit)

I am implementing a bot that performs scheduled backups.
from a front-end a user will be able to change the folder names the backups are stored in.
according to:
What's the purpose of git-mv?
mv oldname newname
git add newname
git rm oldname
is what I want to do when a folder or file name is to be changed.
so I move the files using Java FileUtils,
add the new file/folder and remove the old file/folder using:
git.add().addFilepattern(newName).call();
git.rm().addFilepattern(oldName).call();
git.commit().setAll(true).setMessage("Renamed group "+oldName+ " to " +newName).call();
The main goal being: to preserve the history of the files being moved.
Should I commit after adding the 'new' file before removing the 'old'?
Is my current order of operations fine and committing after both operations should preserve the change history?
I am still new to Git and how the logging works, in TortoiseGit it shows files added and removed, would it show up as a move in the log if the process worked?
Thank you for your time.
Git does not actually record history of individual files in the repository; it records the history of the entire repository as a single unit. There's nothing in a commit that explicitly says that the foo.txt in revision 2 is a continuation of the bar.txt in revision 1. Instead, renames are inferred by tools that examine the repository — after the changes have been committed — using the heuristic that if a commit removes a file and also creates another file with similar contents, the old file was renamed to the new one.
This heuristic only recognizes a rename if both changes occur in the same commit. If you remove a file, commit, then add the file back with a different name and commit again, Git will see that as separate deletion and addition of unrelated files.
Note that rename detection is optional and tools may not do it by default. With git log you need to use the -M option, for example, or do git config --bool diff.renames true.
I'm not familiar with JGit, but your Java code should probably mirror what Git is actually doing beneath the interface when you run your command. Since you are already doing this, I don't see any problem. I would make sure that the entire renaming operation appears in a single commit. There are several reasons for wanting to do this. You may want to revert the renaming at some point. If you have a single commit, it would be easy to do this via git revert.
With regard to preserving the history, renaming a file makes it harder to track the history, but not impossible, e.g.
git log --follow ./path/to/file

Eclipse can not commit all files to Git

I have project in eclipse. I was developing it. Everything was fine. I use git to commit changes.
But starting from some point I noticed that not all files in git repository are committed.
When I do commit, git just do not show it under list of available files. I have tried to commit each file - no result, tried "add to index" - no result.
Does somebody know what can be the reason? I have such problem first time.
And no tracking symbol ">" appears.
In order to see if there is an issue with a .gitignore, switch back to the command line, and type:
git check-ignore -v -- yourFile
You will immediately see if one of the .gitignore rules applies to it or not.

My java source files cannot be commited

i am creating a small Java project and wish to put in on GitHub using eclipse.
Everything is working fine until i go to Team -> Commit, my source files aren't there.
I don't know what to do so i could commit my .java files, any ideas?
Here is a screenshot:
As you can see, there were about 4 files in the Files section, but none of them were the java files.
It is possible you skipped the "add to the index" part: you need to add files, before committing.
See Track Changes.
Click Team > Add to Index on the project node. (This menu item is named Add on older versions of Egit.)
If you don't add anything, the commit will be empty.
See also "Git Basics - Recording Changes to the Repository".

Removing an unwanted directory folder from netbeans and fixing a "is already under version control"

I have two issues i would like to get fixed, yet in my attempt to fix the issues i have managed to descend down to an angry primate screaming and shouting at my pc.
Here are my issues (some of which cannot be solved by an online community),
Earlier today i thought it would be a great idea to create a folder called "res", res would be used to store images and wav files for a game i am making. Now, if the wav file i am testing is not inside this folder and the directory to the wav file is right it works fine, it looks like this ".getResource(fileName);". But when i do have the wav file in the "res" folder, i changed the directory to the following .getResource("res/"+fileName); i get the following
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
as well as a nullPointerException pointing to all the stuff relating to sound.
So after being incredibly frustrated, i removed "res" folder, then the project stopped compiling as it cannot find "res", so i recreated res folder, keeping the wav file out of it which worked just fine. I then felt happy about it and thought, ya i'm done for a while, lets commit this to SVN.
On attempt to commit my work i get a "is already under version control" error for the "res" folder
so to recap this
i created a folder called "res" in my project folder (via netbeans)
i put a wav file into "res" and found it threw errors
i removed the wav file from "res" and it worked just fine
i deleted res folder and project refused to compile without this very important folder
i recreated res folder and then tried to commit my work to SVN and SVN doesn't want my children anymore.
I am now left with children i cannot put somewhere safe and an unwanted ghost in my house (folder in my project folder that wont do anything)
assistance, as always is greatfully accepted and welcomed.
With respect to the SVN problem:
NB tends to add or delete directories on SVN automatically (but not commit) and it might be confused after the delete/recreation.
I'd suggest using a different tool (SVN command line or TortoiseSVN, for example) to verify which operations does SVN are pending and try reverting operations on the res directory. If SVN marks res as existing then you might try veryfing that the directory is not already on the repository.
Also you might remove the SVN cache from your Netbeans directory (previously called %HOME%\.netbeans\cache\svn and now (NB 7.2) changed to %HOME%\Netbeans\Cache\7.2\svncache.
An extreme solution would be to make a new checkout and copy your changes from the old working copy to the new one.

Categories