annotate src/org/tmatesoft/hg/internal/FileHistory.java @ 629:5f52074707b2

Diff/blame methods as command, their residence in HgDataFile was a mistake
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 22 May 2013 16:46:15 +0200
parents 6526d8adbc0f
children 72fc7774b87e
rev   line source
596
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.core.HgIterateDirection.NewToOld;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.util.Collections;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.LinkedList;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import org.tmatesoft.hg.core.HgIterateDirection;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.tmatesoft.hg.core.Nodeid;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.repo.HgDataFile;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.repo.HgRepository;
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
28 import org.tmatesoft.hg.repo.HgRuntimeException;
596
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 /**
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * History of a file, with copy/renames, and corresponding revision information.
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * Facility for file history iteration.
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 *
624
507602cb4fb3 FIXMEs and TODOs: pay some technical debt
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 596
diff changeset
34 * TODO [post-1.1] Utilize in HgLogCommand and anywhere else we need to follow file history
596
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 *
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author Artem Tikhomirov
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 * @author TMate Software Ltd.
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 */
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 public class FileHistory {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private LinkedList<FileRevisionHistoryChunk> fileCompleteHistory = new LinkedList<FileRevisionHistoryChunk>();
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 private final HgDataFile df;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 private final int csetTo;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 private final int csetFrom;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 public FileHistory(HgDataFile file, int fromChangeset, int toChangeset) {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 df = file;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 csetFrom = fromChangeset;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 csetTo = toChangeset;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 public int getStartChangeset() {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 return csetFrom;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 public int getEndChangeset() {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 return csetTo;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
60 public void build() throws HgRuntimeException {
596
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 assert fileCompleteHistory.isEmpty();
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 HgDataFile currentFile = df;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 final int changelogRevIndexEnd = csetTo;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 final int changelogRevIndexStart = csetFrom;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 int fileLastClogRevIndex = changelogRevIndexEnd;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 FileRevisionHistoryChunk nextChunk = null;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 fileCompleteHistory.clear(); // just in case, #build() is not expected to be called more than once
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 do {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 FileRevisionHistoryChunk fileHistory = new FileRevisionHistoryChunk(currentFile);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 fileHistory.init(fileLastClogRevIndex);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 fileHistory.linkTo(nextChunk);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 fileCompleteHistory.addFirst(fileHistory); // to get the list in old-to-new order
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 nextChunk = fileHistory;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 if (fileHistory.changeset(0) > changelogRevIndexStart && currentFile.isCopy()) {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 // fileHistory.changeset(0) is the earliest revision we know about so far,
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 // once we get to revisions earlier than the requested start, stop digging.
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 // The reason there's NO == (i.e. not >=) because:
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 // (easy): once it's equal, we've reached our intended start
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 // (hard): if changelogRevIndexStart happens to be exact start of one of renames in the
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 // chain of renames (test-annotate2 repository, file1->file1a->file1b, i.e. points
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 // to the very start of file1a or file1 history), presence of == would get us to the next
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 // chunk and hence changed parents of present chunk's first element. Our annotate alg
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 // relies on parents only (i.e. knows nothing about 'last iteration element') to find out
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 // what to compare, and hence won't report all lines of 'last iteration element' (which is the
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 // first revision of the renamed file) as "added in this revision", leaving gaps in annotate
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 HgRepository repo = currentFile.getRepo();
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 Nodeid originLastRev = currentFile.getCopySourceRevision();
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 currentFile = repo.getFileNode(currentFile.getCopySourceName());
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 fileLastClogRevIndex = currentFile.getChangesetRevisionIndex(currentFile.getRevisionIndex(originLastRev));
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 // XXX perhaps, shall fail with meaningful exception if new file doesn't exist (.i/.d not found for whatever reason)
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 // or source revision is missing?
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 } else {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 fileHistory.chopAtChangeset(changelogRevIndexStart);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 currentFile = null; // stop iterating
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 } while (currentFile != null && fileLastClogRevIndex > changelogRevIndexStart);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 // fileCompleteHistory is in (origin, intermediate target, ultimate target) order
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 public Iterable<FileRevisionHistoryChunk> iterate(HgIterateDirection order) {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 if (order == NewToOld) {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 return ReverseIterator.reversed(fileCompleteHistory);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 return Collections.unmodifiableList(fileCompleteHistory);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 }