annotate src/org/tmatesoft/hg/internal/RevlogStreamWriter.java @ 669:b1a3a056d7e6

Pull: sample command-line utility
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 11 Jul 2013 20:49:33 +0200
parents ae2d439fbed3
children 545b1d4cc11d
rev   line source
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.internal.Internals.REVLOGV1_RECORD_SIZE;
663
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
21 import static org.tmatesoft.hg.repo.HgRepository.NO_REVISION;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.IOException;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.nio.ByteBuffer;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25
617
65c01508f002 Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 608
diff changeset
26 import org.tmatesoft.hg.core.HgIOException;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.core.Nodeid;
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
28 import org.tmatesoft.hg.core.SessionContext;
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
29 import org.tmatesoft.hg.internal.DataSerializer.ByteArrayDataSource;
645
14dac192aa26 Push: phase2 - upload bundle with changes to remote server
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 628
diff changeset
30 import org.tmatesoft.hg.internal.DataSerializer.ByteArraySerializer;
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
31 import org.tmatesoft.hg.internal.DataSerializer.DataSource;
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
32 import org.tmatesoft.hg.repo.HgBundle.GroupElement;
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
33 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
664
ae2d439fbed3 Utilize transaction when writing fncache. Better HgIOException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 663
diff changeset
34 import org.tmatesoft.hg.repo.HgInvalidDataFormatException;
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 618
diff changeset
35 import org.tmatesoft.hg.repo.HgInvalidRevisionException;
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
36 import org.tmatesoft.hg.repo.HgInvalidStateException;
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
37 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: 618
diff changeset
38 import org.tmatesoft.hg.repo.HgRuntimeException;
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
39 import org.tmatesoft.hg.util.Pair;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 /**
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 *
608
e1b29756f901 Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 591
diff changeset
43 * TODO [post-1.1] separate operation to check if index is too big and split into index+data
532
688c1ab113bb Introduce explicit reference to base patch in bundle's group element, use it when cloning to fix defect when few revisions list null,null parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 530
diff changeset
44 *
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 * @author Artem Tikhomirov
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 * @author TMate Software Ltd.
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 */
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 public class RevlogStreamWriter {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
50 private final DigestHelper dh = new DigestHelper();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
51 private final RevlogCompressor revlogDataZip;
617
65c01508f002 Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 608
diff changeset
52 private final Transaction transaction;
663
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
53 // init with illegal values
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
54 private int lastEntryBase = BAD_REVISION, lastEntryIndex = BAD_REVISION, lastEntryActualLen = -1;
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
55 // record revision and its full content
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
56 // the name might be misleading, it does not necessarily match lastEntryIndex
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
57 private Pair<Integer, byte[]> lastFullContent;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
58 private Nodeid lastEntryRevision;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
59 private IntMap<Nodeid> revisionCache = new IntMap<Nodeid>(32);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
60 private RevlogStream revlogStream;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61
617
65c01508f002 Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 608
diff changeset
62 public RevlogStreamWriter(SessionContext.Source ctxSource, RevlogStream stream, Transaction tr) {
591
e447384f3771 CommitFacility as internal class; refactored infrastructure around internals (access to RevlogStream)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 544
diff changeset
63 assert ctxSource != null;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
64 assert stream != null;
617
65c01508f002 Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 608
diff changeset
65 assert tr != null;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
66
591
e447384f3771 CommitFacility as internal class; refactored infrastructure around internals (access to RevlogStream)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 544
diff changeset
67 revlogDataZip = new RevlogCompressor(ctxSource.getSessionContext());
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
68 revlogStream = stream;
617
65c01508f002 Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 608
diff changeset
69 transaction = tr;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
70 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
71
663
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
72 public RevlogStream getRevlogStream() {
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
73 return revlogStream;
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
74 }
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
75
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
76 public Pair<Integer,Nodeid> addPatchRevision(GroupElement ge, RevisionToIndexMap clogRevs, RevisionToIndexMap revlogRevs) throws HgIOException, HgRuntimeException {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
77 populateLastEntryIndex();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
78 //
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
79 final Nodeid nodeRev = ge.node();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
80 final Nodeid csetRev = ge.cset();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
81 int linkRev;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
82 if (nodeRev.equals(csetRev)) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
83 linkRev = lastEntryIndex+1;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
84 } else {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
85 linkRev = clogRevs.revisionIndex(csetRev);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
86 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
87 assert linkRev >= 0;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
88 final Nodeid p1Rev = ge.firstParent();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
89 int p1 = p1Rev.isNull() ? NO_REVISION : revlogRevs.revisionIndex(p1Rev);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
90 final Nodeid p2Rev = ge.secondParent();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
91 int p2 = p2Rev.isNull() ? NO_REVISION : revlogRevs.revisionIndex(p2Rev);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
92 Patch p = new Patch();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
93 final byte[] patchBytes;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
94 try {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
95 // XXX there's ge.rawData(), to avoid extra array wrap
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
96 patchBytes = ge.rawDataByteArray();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
97 p.read(new ByteArrayDataAccess(patchBytes));
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
98 } catch (IOException ex) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
99 throw new HgIOException("Failed to read patch information", ex, null);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
100 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
101 //
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
102 final Nodeid patchBase = ge.patchBase();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
103 int patchBaseRev = patchBase.isNull() ? NO_REVISION : revlogRevs.revisionIndex(patchBase);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
104 int baseRev = lastEntryIndex == NO_REVISION ? 0 : revlogStream.baseRevision(patchBaseRev);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
105 int revLen;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
106 DataSource ds;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
107 byte[] complete = null;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
108 if (patchBaseRev == lastEntryIndex && lastEntryIndex != NO_REVISION) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
109 // we may write patch from GroupElement as is
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
110 int patchBaseLen = dataLength(patchBaseRev);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
111 revLen = patchBaseLen + p.patchSizeDelta();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
112 ds = new ByteArrayDataSource(patchBytes);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
113 } else {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
114 // read baseRev, unless it's the pull to empty repository
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
115 try {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
116 if (lastEntryIndex == NO_REVISION) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
117 complete = p.apply(new ByteArrayDataAccess(new byte[0]), -1);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
118 baseRev = 0; // it's done above, but doesn't hurt
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
119 } else {
663
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
120 assert patchBaseRev != NO_REVISION;
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
121 ReadContentInspector insp = new ReadContentInspector().read(revlogStream, patchBaseRev);
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
122 complete = p.apply(new ByteArrayDataAccess(insp.content), -1);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
123 baseRev = lastEntryIndex + 1;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
124 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
125 ds = new ByteArrayDataSource(complete);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
126 revLen = complete.length;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
127 } catch (IOException ex) {
663
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
128 // unlikely to happen, as ByteArrayDataSource throws IOException only in case of programming errors
664
ae2d439fbed3 Utilize transaction when writing fncache. Better HgIOException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 663
diff changeset
129 // hence, throwing rt exception here makes more sense here than HgIOException (even that latter is in throws)
ae2d439fbed3 Utilize transaction when writing fncache. Better HgIOException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 663
diff changeset
130 throw new HgInvalidDataFormatException("Failed to reconstruct revision", ex);
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
131 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
132 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
133 doAdd(nodeRev, p1, p2, linkRev, baseRev, revLen, ds);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
134 if (complete != null) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
135 lastFullContent = new Pair<Integer, byte[]>(lastEntryIndex, complete);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
136 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
137 return new Pair<Integer, Nodeid>(lastEntryIndex, lastEntryRevision);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
138 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
139
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
140 /**
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
141 * @return nodeid of added revision
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 618
diff changeset
142 * @throws HgRuntimeException
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
143 */
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
144 public Pair<Integer,Nodeid> addRevision(DataSource content, int linkRevision, int p1, int p2) throws HgIOException, HgRuntimeException {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
145 populateLastEntryIndex();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
146 populateLastEntryContent();
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
147 //
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
148 byte[] contentByteArray = toByteArray(content);
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
149 Patch patch = GeneratePatchInspector.delta(lastFullContent.second(), contentByteArray);
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
150 int patchSerializedLength = patch.serializedLength();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
151
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
152 final boolean writeComplete = preferCompleteOverPatch(patchSerializedLength, contentByteArray.length);
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
153 DataSerializer.DataSource dataSource = writeComplete ? new ByteArrayDataSource(contentByteArray) : patch.new PatchDataSource();
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
154 //
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
155 Nodeid p1Rev = revision(p1);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
156 Nodeid p2Rev = revision(p2);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
157 Nodeid newRev = Nodeid.fromBinary(dh.sha1(p1Rev, p2Rev, contentByteArray).asBinary(), 0);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
158 doAdd(newRev, p1, p2, linkRevision, writeComplete ? lastEntryIndex+1 : lastEntryBase, contentByteArray.length, dataSource);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
159 lastFullContent = new Pair<Integer, byte[]>(lastEntryIndex, contentByteArray);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
160 return new Pair<Integer, Nodeid>(lastEntryIndex, lastEntryRevision);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
161 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
162
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
163 private Nodeid doAdd(Nodeid rev, int p1, int p2, int linkRevision, int baseRevision, int revLen, DataSerializer.DataSource dataSource) throws HgIOException, HgRuntimeException {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
164 assert linkRevision >= 0;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
165 assert baseRevision >= 0;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
166 assert p1 == NO_REVISION || p1 >= 0;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
167 assert p2 == NO_REVISION || p2 >= 0;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
168 assert !rev.isNull();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
169 assert revLen >= 0;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
170 revlogDataZip.reset(dataSource);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
171 final int compressedLen;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
172 final boolean useCompressedData = preferCompressedOverComplete(revlogDataZip.getCompressedLength(), dataSource.serializeLength());
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
173 if (useCompressedData) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
174 compressedLen= revlogDataZip.getCompressedLength();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
175 } else {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
176 // compression wasn't too effective,
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
177 compressedLen = dataSource.serializeLength() + 1 /*1 byte for 'u' - uncompressed prefix byte*/;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
178 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
179 //
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
180 DataSerializer indexFile, dataFile;
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
181 indexFile = dataFile = null;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
182 try {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
183 //
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
184 indexFile = revlogStream.getIndexStreamWriter(transaction);
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
185 final boolean isInlineData = revlogStream.isInlineData();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
186 HeaderWriter revlogHeader = new HeaderWriter(isInlineData);
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
187 revlogHeader.length(revLen, compressedLen);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
188 revlogHeader.nodeid(rev.toByteArray());
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
189 revlogHeader.linkRevision(linkRevision);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
190 revlogHeader.parents(p1, p2);
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
191 revlogHeader.baseRevision(baseRevision);
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
192 long lastEntryOffset = revlogStream.newEntryOffset();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
193 revlogHeader.offset(lastEntryOffset);
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
194 //
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
195 revlogHeader.serialize(indexFile);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
196
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
197 if (isInlineData) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
198 dataFile = indexFile;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
199 } else {
617
65c01508f002 Rollback support for commands that modify repository. Strategy to keep complete copy of a file being changed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 608
diff changeset
200 dataFile = revlogStream.getDataStreamWriter(transaction);
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
201 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
202 if (useCompressedData) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
203 int actualCompressedLenWritten = revlogDataZip.writeCompressedData(dataFile);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
204 if (actualCompressedLenWritten != compressedLen) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
205 throw new HgInvalidStateException(String.format("Expected %d bytes of compressed data, but actually wrote %d in %s", compressedLen, actualCompressedLenWritten, revlogStream.getDataFileName()));
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
206 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
207 } else {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
208 dataFile.writeByte((byte) 'u');
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
209 dataSource.serialize(dataFile);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
210 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
211
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
212 lastEntryBase = revlogHeader.baseRevision();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
213 lastEntryIndex++;
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
214 lastEntryActualLen = revLen;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
215 lastEntryRevision = rev;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
216 revisionCache.put(lastEntryIndex, lastEntryRevision);
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
217
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
218 revlogStream.revisionAdded(lastEntryIndex, lastEntryRevision, lastEntryBase, lastEntryOffset);
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
219 } finally {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
220 indexFile.done();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
221 if (dataFile != null && dataFile != indexFile) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
222 dataFile.done();
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
223 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
224 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
225 return lastEntryRevision;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
226 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
227
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 618
diff changeset
228 private byte[] toByteArray(DataSource content) throws HgIOException, HgRuntimeException {
645
14dac192aa26 Push: phase2 - upload bundle with changes to remote server
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 628
diff changeset
229 ByteArraySerializer ba = new ByteArraySerializer();
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
230 content.serialize(ba);
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
231 return ba.toByteArray();
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
232 }
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
233
628
6526d8adbc0f Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 618
diff changeset
234 private Nodeid revision(int revisionIndex) throws HgInvalidControlFileException, HgInvalidRevisionException {
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
235 if (revisionIndex == NO_REVISION) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
236 return Nodeid.NULL;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
237 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
238 Nodeid n = revisionCache.get(revisionIndex);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
239 if (n == null) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
240 n = Nodeid.fromBinary(revlogStream.nodeid(revisionIndex), 0);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
241 revisionCache.put(revisionIndex, n);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
242 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
243 return n;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
244 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
245
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
246 private int dataLength(int revisionIndex) throws HgInvalidControlFileException, HgInvalidRevisionException {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
247 assert revisionIndex >= 0;
663
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
248 if (revisionIndex == lastEntryIndex && lastEntryActualLen >= 0) {
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
249 // if the last entry is the one we've just written, we know its actual len.
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
250 // it's possible, however, that revisionIndex == lastEntryIndex just
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
251 // because revision being added comes right after last locally known one
46b56864b483 Pull: phase2 - update phases from remote, fncache with added files. Tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 660
diff changeset
252 // and lastEntryActualLen is not set
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
253 return lastEntryActualLen;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
254 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
255 if (lastFullContent != null && lastFullContent.first() == revisionIndex) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
256 return lastFullContent.second().length;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
257 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
258 return revlogStream.dataLength(revisionIndex);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
259 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
260
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
261 private void populateLastEntryIndex() throws HgRuntimeException {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
262 int revCount = revlogStream.revisionCount();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
263 lastEntryIndex = revCount == 0 ? NO_REVISION : revCount - 1;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
264 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
265
664
ae2d439fbed3 Utilize transaction when writing fncache. Better HgIOException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 663
diff changeset
266 private void populateLastEntryContent() throws HgIOException, HgRuntimeException {
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
267 if (lastFullContent != null && lastFullContent.first() == lastEntryIndex) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
268 // we have last entry cached
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
269 return;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
270 }
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
271 lastEntryRevision = Nodeid.NULL;
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
272 if (lastEntryIndex != NO_REVISION) {
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
273 ReadContentInspector insp = new ReadContentInspector().read(revlogStream, lastEntryIndex);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
274 lastEntryBase = insp.baseRev;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
275 lastEntryRevision = insp.rev;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
276 lastFullContent = new Pair<Integer, byte[]>(lastEntryIndex, insp.content);
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
277 } else {
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
278 lastFullContent = new Pair<Integer, byte[]>(lastEntryIndex, new byte[0]);
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
279 }
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
280 assert lastFullContent.first() == lastEntryIndex;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
281 assert lastFullContent.second() != null;
538
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
282 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
283
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
284 public static boolean preferCompleteOverPatch(int patchLength, int fullContentLength) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
285 return !decideWorthEffort(patchLength, fullContentLength);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
286 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
287
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
288 public static boolean preferCompressedOverComplete(int compressedLen, int fullContentLength) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
289 if (compressedLen <= 0) { // just in case, meaningless otherwise
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
290 return false;
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
291 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
292 return decideWorthEffort(compressedLen, fullContentLength);
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
293 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
294
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
295 // true if length obtained with effort is worth it
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
296 private static boolean decideWorthEffort(int lengthWithExtraEffort, int lengthWithoutEffort) {
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
297 return lengthWithExtraEffort < (/* 3/4 of original */lengthWithoutEffort - (lengthWithoutEffort >>> 2));
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
298 }
dd4f6311af52 Commit: first working version
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 534
diff changeset
299
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
300 /*XXX public because HgCloneCommand uses it*/
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
301 public static class HeaderWriter implements DataSerializer.DataSource {
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
302 private final ByteBuffer header;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
303 private final boolean isInline;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
304 private long offset;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
305 private int length, compressedLength;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
306 private int baseRev, linkRev, p1, p2;
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
307 private byte[] nodeid;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
308
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
309 public HeaderWriter(boolean inline) {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
310 isInline = inline;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
311 header = ByteBuffer.allocate(REVLOGV1_RECORD_SIZE);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
312 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
313
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
314 public HeaderWriter offset(long offset) {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
315 this.offset = offset;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
316 return this;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
317 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
318
532
688c1ab113bb Introduce explicit reference to base patch in bundle's group element, use it when cloning to fix defect when few revisions list null,null parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 530
diff changeset
319 public int baseRevision() {
688c1ab113bb Introduce explicit reference to base patch in bundle's group element, use it when cloning to fix defect when few revisions list null,null parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 530
diff changeset
320 return baseRev;
688c1ab113bb Introduce explicit reference to base patch in bundle's group element, use it when cloning to fix defect when few revisions list null,null parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 530
diff changeset
321 }
688c1ab113bb Introduce explicit reference to base patch in bundle's group element, use it when cloning to fix defect when few revisions list null,null parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 530
diff changeset
322
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
323 public HeaderWriter baseRevision(int baseRevision) {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
324 this.baseRev = baseRevision;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
325 return this;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
326 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
327
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
328 public HeaderWriter length(int len, int compressedLen) {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
329 this.length = len;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
330 this.compressedLength = compressedLen;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
331 return this;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
332 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
333
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
334 public HeaderWriter parents(int parent1, int parent2) {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
335 p1 = parent1;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
336 p2 = parent2;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
337 return this;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
338 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
339
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
340 public HeaderWriter linkRevision(int linkRevision) {
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
341 linkRev = linkRevision;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
342 return this;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
343 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
344
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
345 public HeaderWriter nodeid(Nodeid n) {
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
346 nodeid = n.toByteArray();
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
347 return this;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
348 }
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
349
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
350 public HeaderWriter nodeid(byte[] nodeidBytes) {
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
351 nodeid = nodeidBytes;
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
352 return this;
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
353 }
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
354
618
7c0d2ce340b8 Refactor approach how content finds it way down to a commit revision
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 617
diff changeset
355 public void serialize(DataSerializer out) throws HgIOException {
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
356 header.clear();
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
357 if (offset == 0) {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
358 int version = 1 /* RevlogNG */;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
359 if (isInline) {
608
e1b29756f901 Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 591
diff changeset
360 version |= RevlogStream.INLINEDATA;
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
361 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
362 header.putInt(version);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
363 header.putInt(0);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
364 } else {
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
365 header.putLong(offset << 16);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
366 }
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
367 header.putInt(compressedLength);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
368 header.putInt(length);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
369 header.putInt(baseRev);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
370 header.putInt(linkRev);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
371 header.putInt(p1);
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
372 header.putInt(p2);
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
373 header.put(nodeid);
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
374 // assume 12 bytes left are zeros
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
375 out.write(header.array(), 0, header.capacity());
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
376
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
377 // regardless whether it's inline or separate data,
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
378 // offset field always represent cumulative compressedLength
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 538
diff changeset
379 // (while physical offset in the index file with inline==true differs by n*sizeof(header), where n is entry's position in the file)
530
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
380 offset += compressedLength;
0f6fa88e2162 Towards commit command: refactor clone, extract pieces to reuse. Describe a defect discovered when bundle has few patches with 0,0 parents
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
381 }
534
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
382
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
383 public int serializeLength() {
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
384 return header.capacity();
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
385 }
243202f1bda5 Commit: refactor revision creation code from clone command to work separately, fit into existing library structure
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 533
diff changeset
386 }
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
387
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
388 // XXX part of HgRevisionMap contract, need public counterparts (along with IndexToRevisionMap)
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
389 public interface RevisionToIndexMap {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
390
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
391 /**
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
392 * @return {@link HgRepository#NO_REVISION} if unknown revision
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
393 */
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
394 int revisionIndex(Nodeid revision);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
395 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
396
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
397 private static class ReadContentInspector implements RevlogStream.Inspector {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
398 public int baseRev;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
399 public Nodeid rev;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
400 public byte[] content;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
401 private IOException failure;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
402
664
ae2d439fbed3 Utilize transaction when writing fncache. Better HgIOException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 663
diff changeset
403 public ReadContentInspector read(RevlogStream rs, int revIndex) throws HgIOException, HgRuntimeException {
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
404 assert revIndex >= 0;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
405 rs.iterate(revIndex, revIndex, true, this);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
406 if (failure != null) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
407 String m = String.format("Failed to get content of revision %d", revIndex);
664
ae2d439fbed3 Utilize transaction when writing fncache. Better HgIOException
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 663
diff changeset
408 throw rs.initWithIndexFile(new HgIOException(m, failure, null));
660
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
409 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
410 return this;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
411 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
412
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
413 public void next(int revisionIndex, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess data) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
414 try {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
415 baseRev = baseRevision;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
416 rev = Nodeid.fromBinary(nodeid, 0);
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
417 content = data.byteArray();
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
418 } catch (IOException ex) {
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
419 failure = ex;
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
420 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
421 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
422 }
4fd317a2fecf Pull: phase1 get remote changes and add local revisions
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 645
diff changeset
423 }