annotate src/org/tmatesoft/hg/internal/FileHistory.java @ 596:43cfa08ff3fd

HgBlameFacility refactoring: extract code to build file history that spans renames
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 02 May 2013 19:23:53 +0200
parents
children 507602cb4fb3
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;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
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 * 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
31 * 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
32 *
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * FIXME [post-1.1] Utilize in HgLogCommand and anywhere else we need to follow file history
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 *
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @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
37 */
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public class FileHistory {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 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
41 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
42 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
43 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
44
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 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
46 df = file;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 csetFrom = fromChangeset;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 csetTo = toChangeset;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 }
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 public int getStartChangeset() {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 return csetFrom;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
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 public int getEndChangeset() {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 return csetTo;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 }
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 public void build() {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 assert fileCompleteHistory.isEmpty();
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 HgDataFile currentFile = df;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 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
63 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
64 int fileLastClogRevIndex = changelogRevIndexEnd;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 FileRevisionHistoryChunk nextChunk = null;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 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
67 do {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 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
69 fileHistory.init(fileLastClogRevIndex);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 fileHistory.linkTo(nextChunk);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 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
72 nextChunk = fileHistory;
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 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
74 // 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
75 // 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
76 // 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
77 // (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
78 // (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
79 // 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
80 // 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
81 // 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
82 // 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
83 // 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
84 // 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
85 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
86 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
87 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
88 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
89 // 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
90 // 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
91 } else {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 fileHistory.chopAtChangeset(changelogRevIndexStart);
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 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
94 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 } 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
96 // 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
97 }
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 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
100 if (order == NewToOld) {
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 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
102 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 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
104 }
43cfa08ff3fd HgBlameFacility refactoring: extract code to build file history that spans renames
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 }