Rigth way to get current file revision (git)
Hi,
I'm looking for a right API method to get the current git revision (revision number, date, etc.) for a modified file.
It seems that either Change.getBeforeRevision.getRevisionNumber() or historySession.getCurrentRevisionNumber() should provide the right revision number; then the date can be taken from the history by the revision number. However, the I'm getting strange results for git; tried idea 10.0.2 and 10.5:
Example:
I have a project with two files; files were edited and committed separately; they have different current revisions.
test_idea_git_jp/readme.txt
test_idea_git_jp/readme1.txt
Ran the following code:
<get changes from ChangeListManager>
for (final Change change : changes) {
System.out.println("file status: " + change.getFileStatus());
final ContentRevision beforeRevision = change.getBeforeRevision();
if (beforeRevision != null) {
final FilePath filePath = beforeRevision.getFile();
System.out.println("beforeRevision.file: " + filePath);
System.out.println("beforeRevision.number: " + beforeRevision.getRevisionNumber());
final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(filePath);
if (vcs != null) {
final VcsHistorySession historySession = vcs.getVcsHistoryProvider().createSessionFor(filePath);
final VcsRevisionNumber currentRevisionNumber = historySession.getCurrentRevisionNumber();
System.out.println("history.currentRevision: " + currentRevisionNumber);
List<VcsFileRevision> revisions = historySession.getRevisionList();
for (VcsFileRevision revision : revisions) {
System.out.println();
System.out.println("history # " + revision.getRevisionNumber());
Date date = revision.getRevisionDate();
System.out.println(" date: " + (date != null ? date : "NULL"));
}
}
}
System.out.println();
}
1) Output from idea-IC-99.32 (10.0.2)
file status: MODIFIED
beforeRevision.file: FilePath[/space/test/idea_git_kobold/new_git/test_idea_git_jp/readme.txt]
beforeRevision.number: d81bcc54b430e511fafa11bb98d8669423fbf71c
history.currentRevision: 9bce7b07574687511087a77dc16fb1262a2f6e52
history # 9bce7b07574687511087a77dc16fb1262a2f6e52
date: Tue May 24 13:56:50 EDT 2011
file status: MODIFIED
beforeRevision.file: FilePath[/space/test/idea_git_kobold/new_git/test_idea_git_jp/readme1.txt]
beforeRevision.number: d81bcc54b430e511fafa11bb98d8669423fbf71c
history.currentRevision: d81bcc54b430e511fafa11bb98d8669423fbf71c
history # d81bcc54b430e511fafa11bb98d8669423fbf71c
date: Thu May 26 15:03:35 EDT 2011
history # 9ad0aac60fe655a6be49059b9dfac15e3c04808d
date: Thu May 26 15:01:50 EDT 2011
2) Output from idea 10.5 (idea-IC-107.105)
file status: MODIFIED
beforeRevision.file: FilePath[/space/test/idea_git_kobold/new_git/test_idea_git_jp/readme.txt]
beforeRevision.number: d81bcc54b430e511fafa11bb98d8669423fbf71c
history.currentRevision: null
history # 9bce7b07574687511087a77dc16fb1262a2f6e52
date: Tue May 24 13:56:50 EDT 2011
file status: MODIFIED
beforeRevision.file: FilePath[/space/test/idea_git_kobold/new_git/test_idea_git_jp/readme1.txt]
beforeRevision.number: d81bcc54b430e511fafa11bb98d8669423fbf71c
history.currentRevision: null
history # d81bcc54b430e511fafa11bb98d8669423fbf71c
date: Thu May 26 15:03:35 EDT 2011
history # 9ad0aac60fe655a6be49059b9dfac15e3c04808d
date: Thu May 26 15:01:50 EDT 2011
- - -
What's strange. For the file "readme.txt", I expect to get revision "9bce7b0..." (which is visible in history list); but:
- change.getBeforeRevision().getRevisionNumber() does not return _file_ revision. Instead, it returns the last revision for the folder or project or the second file ("d81bcc5...");
- historySession.getCurrentRevisionNumber() works as expected revision in 10.0.2; but in 10.5, null is returned.
Now, the questions:
- what is the right behaviour for beforeRevision.getRevisionNumber()? Should it return last file revision, or last project revision?
- which method should I use to get the file revision (is there another API method?)
thanks,
Sergey
Please sign in to leave a comment.
Maybe the problem is here that in Git there is no such thing as a file revision.
Git doesn't track files, but code.
Because of that it also doesn't explicitly track moves or renames but determines those on-the-fly and also doesn't track folders.
One revision in Git never contains certain files, but one revisions contains the state the whole source tree is in as well as the complete history behind the current state.
If you manipulate the history of a file which is easily possible with Git, you also modify the "revision number" of the current state, even if the resulting current source tree is the same.
Maybe this different architecture is hitting you here.
well, the file history contains only reveisions where the file was modified
tested both IDEA UI and result of this call:
vcs.getVcsHistoryProvider().createSessionFor(filePath).getRevisionList()
I wonder why historySession.getCurrentRevisionNumber() and beforeRevision.getRevisionNumber() are not consistent.
Hello Sergey,
The fact that historySession.getCurrentRevisionNumber() returns null is a
bug. We'll see if we can fix it in the 10.5.1 update.
The behavior of getBeforeRevision() for Git changes is as expected. Getting
the last revision when a file was last changed for every modified file under
Git would be time-consuming and completely unnecessary.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Thanks, Dmitry.
I re-tested it using 10.5.1, getCurrentRevisionNumber() still returns null.
I sent a bug report - IDEA-71995.