A conversion from Java to Kotlin in Android Studio 2.3.2 (in 3.0 the same behaviour) creates a new file and deletes previous. So Git doesn't know anything about this conversion. And the git history doesn't save. In Intellij Idea everything's fine. IDE just renames file and git saves the history. How can do the same in Android Studio.
-
This answer stackoverflow.com/a/39152021/70930 explains how Git identifies file renames and that there is no way to tell Git explicitly about file being renamed.– PaulJun 1, 2017 at 21:42
-
Thanks, but what's the difference between Android Studio and Idea. Why Git in one case can track the content of file and in another case can't?– Eugene VerichevJun 1, 2017 at 23:10
-
Is it the same file you are converting in Android Studio and Idea? If the files are different, then maybe the amount of changes after the conversion is different, so in one case renaming is detected and in the other case it is not.– PaulJun 2, 2017 at 0:37
-
I use just empty new classes for experiments. They are same. Even generated comments are the same. May be there are a difference in spaces and tabulations.– Eugene VerichevJun 2, 2017 at 8:24
5 Answers
Git guesses renames from added/removed file pairs, but only if these files are close enough, i.e. if the file was renamed with no or small amount of changes.
When you apply java-to-kotlin conversion usually every line of a file changes, so that git cannot find that these old and new files somehow relate to each other.
You can use the following two-stage approach instead:
- just change the extension of
.java
file to.kt
and commit it; - rename it back, apply the conversion and commit the modified
.kt
file.
-
1I used this approach. The first commit is OK and indicates a move. Then I apply the conversion and commit the .kt file using the "Amend" option (to keep only one commit). Then, the final commit does show a "rename" but a deletion and an addition. I guess this is because of the reasons mentioned above. Jul 25, 2017 at 11:37
-
If I create two commits (one for the rename java --> kt, one for the actual conversion), then it works: the link is maintained. However, this makes the history a bit awkward since the first comment points to a totally invalid code... Jul 25, 2017 at 11:45
-
Of course the first commit would be weird. None of the options are ideal but you have to choose between the two. Do you want to loose your history on the file or not? Nov 3, 2017 at 22:26
-
2I created an IntelliJ IDEA / Android Studio plugin that automatically takes care of renaming and committing the file(s) during conversion : github.com/denis-colliot/vcs-kotlin-converter Jul 4, 2018 at 8:16
In case this might help a future reader:
If you use the Git commit dialog integrated with IntelliJ (Commit via Ctrl+K), there is a checkbox on the right in recent versions: ☑ Extra commit for .java > .kt renames
Submitting the dialog this way will create two commits, the first one being just .java
files renamed into .kt
files with no content changes. This helps Git track the content.
-
1This option is not there any more but you can use this plugin: github.com/denis-colliot/vcs-kotlin-converter But be aware that it generates the commit right when you convert it to kotlin. Nov 13, 2019 at 20:39
As mentioned in the other answers, git tracks the contents of the file, not its renames. When git log
is run with --follow
option, it shows history beyond renames, however it considers a file to be renamed only if the previous and current file contents have a similarity index of 50% or more, i.e. less than half the lines of the file have changed.
For this case, where most of the lines have changed, you may set a lower bar for the similarity index using the -M
option:
git log -M20% --follow -- /path/to/file
Depending on the case, you may need to go even lower than 20%.
-
Is there a GIT config to change the similarity index throughout your repo? E.g. I want the default value for my project to be 20%– VeskoApr 12, 2018 at 14:34
-
-
1I asked for a global config flag, as I'm assuming (not sure though!) that various git clients like Android Studio, GitHub, SourceTree, etc use the default similarity index of 50% to display changes. So if we could change this globally, all these tools would show the diff accordingly, based on the desired value.– VeskoApr 16, 2018 at 8:41
Android Studio now has an option: Extra commit for java > kt renames
which is enabled by default and fixes git blame
. But this creates an invalid commit in the history.
If this change is rebased, then you keep git blame
, but lose git bisect
, and the opposite is true if you squash merge.
I've created a small (MIT) gradle script and instructions to get the best of both worlds (TL;DR: perform a rename, and in the same commit, flag to the compiler that the renamed .kt
file should be run via javac
):
https://github.com/ankidroid/Anki-Android/blob/bffd3c60c00649cdc75f35293a85f7e1ef064b53/docs/kotlin-migration.md https://github.com/ankidroid/Anki-Android/blob/1ee5f65b87a8fe544193d928d64dd8395513c9f0/AnkiDroid/kotlinMigration.gradle
The above could further be productionized in an IntelliJ plugin.
Git doesn't actually track renames directly; it infers them based on file add/delete pairs. I assume that Idea is running a git add
when it renames, whereas Android Studio is just deleting the old files. Try running git add
yourself on the new files and a git rm
on the old files and Git should show them as renames.
-
Actually I can't control this all manually. I just use standard command "Convert java file to kotlin file" As result I get deleted old file and created new file (already added to git). My guess is that IDE doesn't use mv or git mv commands. So I see staged for commit two files: removed Sample.java, and added Sample.kt. The only thing I can do to save a history is to reverse Sample.java and save the content of Sample.kt to Sample.java. Then delete Sample.kt and rename Sample.java to Sample.kt. It's a pain! Jun 1, 2017 at 22:54
-
Git has no internal concept of renames. If you remove one file and add another file with the same contents, it will heuristically determine that it's a rename. You can't tell it that it's a rename. It sounds to me like you're viewing a potential commit from some interface that doesn't heuristically determine renames. Can you type
git status
before and after your copying/renaming steps that you do? The status before and after will be the same.– adzenithJun 4, 2017 at 17:09