annotate src/org/tmatesoft/hg/core/HgCloneCommand.java @ 512:10ca3ede8367

Issue 39: Progress and Cancel support for Clone command
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 14 Dec 2012 20:10:15 +0100
parents b3c16d1aede0
children a41d955dc360
rev   line source
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
2 * Copyright (c) 2011-2012 TMate Software Ltd
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.core;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.core.Nodeid.NULL;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import static org.tmatesoft.hg.internal.RequiresFile.*;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.ByteArrayOutputStream;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.File;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.io.FileOutputStream;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.io.IOException;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.nio.ByteBuffer;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.ArrayList;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import java.util.Collections;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import java.util.LinkedList;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import java.util.TreeMap;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 import java.util.zip.DeflaterOutputStream;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 import org.tmatesoft.hg.internal.ByteArrayDataAccess;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 import org.tmatesoft.hg.internal.DataAccess;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 import org.tmatesoft.hg.internal.DigestHelper;
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
36 import org.tmatesoft.hg.internal.Lifecycle;
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 431
diff changeset
37 import org.tmatesoft.hg.internal.RepoInitializer;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 import org.tmatesoft.hg.repo.HgBundle;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 import org.tmatesoft.hg.repo.HgBundle.GroupElement;
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
40 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
41 import org.tmatesoft.hg.repo.HgInvalidFileException;
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
42 import org.tmatesoft.hg.repo.HgInvalidStateException;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 import org.tmatesoft.hg.repo.HgLookup;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 import org.tmatesoft.hg.repo.HgRemoteRepository;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 import org.tmatesoft.hg.repo.HgRepository;
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
46 import org.tmatesoft.hg.repo.HgRuntimeException;
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
47 import org.tmatesoft.hg.util.CancelSupport;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 import org.tmatesoft.hg.util.CancelledException;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 import org.tmatesoft.hg.util.PathRewrite;
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
50 import org.tmatesoft.hg.util.ProgressSupport;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 /**
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 * WORK IN PROGRESS, DO NOT USE
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 *
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 * @author Artem Tikhomirov
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 * @author TMate Software Ltd.
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 */
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
58 public class HgCloneCommand extends HgAbstractCommand<HgCloneCommand> {
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 private File destination;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 private HgRemoteRepository srcRepo;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 public HgCloneCommand() {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65
204
883f1efbcf27 Add clone,incoming and outging tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 202
diff changeset
66 /**
883f1efbcf27 Add clone,incoming and outging tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 202
diff changeset
67 * @param folder location to become root of the repository (i.e. where <em>.hg</em> folder would reside). Either
883f1efbcf27 Add clone,incoming and outging tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 202
diff changeset
68 * shall not exist or be empty otherwise.
883f1efbcf27 Add clone,incoming and outging tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 202
diff changeset
69 * @return <code>this</code> for convenience
883f1efbcf27 Add clone,incoming and outging tests
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 202
diff changeset
70 */
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 public HgCloneCommand destination(File folder) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 destination = folder;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 return this;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 public HgCloneCommand source(HgRemoteRepository hgRemote) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 srcRepo = hgRemote;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 return this;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
81 /**
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
82 *
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
83 * @return
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
84 * @throws HgBadArgumentException
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
85 * @throws HgRemoteConnectionException
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
86 * @throws HgRepositoryNotFoundException
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
87 * @throws HgException
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
88 * @throws CancelledException
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
89 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
90 */
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
91 public HgRepository execute() throws HgException, CancelledException {
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 if (destination == null) {
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
93 throw new IllegalArgumentException("Destination not set", null);
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 if (srcRepo == null || srcRepo.isInvalid()) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 throw new HgBadArgumentException("Bad source repository", null);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 if (destination.exists()) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 if (!destination.isDirectory()) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 throw new HgBadArgumentException(String.format("%s is not a directory", destination), null);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 } else if (destination.list().length > 0) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 throw new HgBadArgumentException(String.format("% shall be empty", destination), null);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 } else {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 destination.mkdirs();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 }
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
107 ProgressSupport progress = getProgressSupport(null);
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
108 CancelSupport cancel = getCancelSupport(null, true);
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
109 cancel.checkCancelled();
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 // if cloning remote repo, which can stream and no revision is specified -
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 // can use 'stream_out' wireproto
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 //
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 // pull all changes from the very beginning
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
114 // XXX consult getContext() if by any chance has a bundle ready, if not, then read and register
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 HgBundle completeChanges = srcRepo.getChanges(Collections.singletonList(NULL));
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
116 cancel.checkCancelled();
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
117 WriteDownMate mate = new WriteDownMate(srcRepo.getSessionContext(), destination, progress, cancel);
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 // instantiate new repo in the destdir
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 mate.initEmptyRepository();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 // pull changes
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 completeChanges.inspectAll(mate);
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
123 mate.checkFailure();
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 mate.complete();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 } catch (IOException ex) {
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
126 throw new HgInvalidFileException(getClass().getName(), ex);
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 } finally {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 completeChanges.unlink();
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
129 progress.done();
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 return new HgLookup().detect(destination);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 // 1. process changelog, memorize nodeids to index
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
136 // 2. process manifest, using map from step 3, collect manifest nodeids
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137 // 3. process every file, using map from 3, and consult set from step 4 to ensure repo is correct
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
138 private static class WriteDownMate implements HgBundle.Inspector, Lifecycle {
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 private final File hgDir;
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
140 private final PathRewrite storagePathHelper;
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
141 private final ProgressSupport progressSupport;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
142 private final CancelSupport cancelSupport;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 private FileOutputStream indexFile;
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
144 private String filename; // human-readable name of the file being written, for log/exception purposes
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 private final TreeMap<Nodeid, Integer> changelogIndexes = new TreeMap<Nodeid, Integer>();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 private boolean collectChangelogIndexes = false;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 private int base = -1;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 private long offset = 0;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 private DataAccess prevRevContent;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 private final DigestHelper dh = new DigestHelper();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 private final ArrayList<Nodeid> revisionSequence = new ArrayList<Nodeid>(); // last visited nodes first
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 private final LinkedList<String> fncacheFiles = new LinkedList<String>();
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 431
diff changeset
156 private RepoInitializer repoInit;
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
157 private Lifecycle.Callback lifecycleCallback;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
158 private CancelledException cancelException;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
160 public WriteDownMate(SessionContext ctx, File destDir, ProgressSupport progress, CancelSupport cancel) {
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 hgDir = new File(destDir, ".hg");
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 431
diff changeset
162 repoInit = new RepoInitializer();
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 431
diff changeset
163 repoInit.setRequires(STORE | FNCACHE | DOTENCODE);
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 431
diff changeset
164 storagePathHelper = repoInit.buildDataFilesHelper(ctx);
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
165 progressSupport = progress;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
166 cancelSupport = cancel;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 public void initEmptyRepository() throws IOException {
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 431
diff changeset
170 repoInit.initEmptyRepository(hgDir);
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 public void complete() throws IOException {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
174 FileOutputStream fncacheFile = new FileOutputStream(new File(hgDir, "store/fncache"));
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
175 for (String s : fncacheFiles) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176 fncacheFile.write(s.getBytes());
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 fncacheFile.write(0x0A); // http://mercurial.selenic.com/wiki/fncacheRepoFormat
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 fncacheFile.close();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 public void changelogStart() {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
184 base = -1;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
185 offset = 0;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
186 revisionSequence.clear();
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
187 indexFile = new FileOutputStream(new File(hgDir, filename = "store/00changelog.i"));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
188 collectChangelogIndexes = true;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
189 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
190 throw new HgInvalidControlFileException("Failed to write changelog", ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
191 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
192 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
193
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 public void changelogEnd() {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
196 if (prevRevContent != null) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
197 prevRevContent.done();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
198 prevRevContent = null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
199 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
200 collectChangelogIndexes = false;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
201 indexFile.close();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
202 indexFile = null;
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
203 filename = null;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
204 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
205 throw new HgInvalidControlFileException("Failed to write changelog", ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 public void manifestStart() {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
210 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
211 base = -1;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
212 offset = 0;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
213 revisionSequence.clear();
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
214 indexFile = new FileOutputStream(new File(hgDir, filename = "store/00manifest.i"));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
215 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
216 throw new HgInvalidControlFileException("Failed to write manifest", ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
217 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
218 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
219
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
220 public void manifestEnd() {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
221 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
222 if (prevRevContent != null) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
223 prevRevContent.done();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
224 prevRevContent = null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
226 indexFile.close();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
227 indexFile = null;
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
228 filename = null;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
229 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
230 throw new HgInvalidControlFileException("Failed to write changelog", ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
231 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
232 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
233
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
234 public void fileStart(String name) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
235 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
236 base = -1;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
237 offset = 0;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
238 revisionSequence.clear();
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 390
diff changeset
239 fncacheFiles.add("data/" + name + ".i"); // TODO post-1.0 this is pure guess,
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
240 // need to investigate more how filenames are kept in fncache
292
a415fe296a50 Refactor PathRewrite to accept any char sequence, not only string
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 274
diff changeset
241 File file = new File(hgDir, filename = storagePathHelper.rewrite(name).toString());
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
242 file.getParentFile().mkdirs();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
243 indexFile = new FileOutputStream(file);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
244 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
245 String m = String.format("Failed to write file %s", filename);
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
246 throw new HgInvalidControlFileException(m, ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
247 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
248 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
249
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
250 public void fileEnd(String name) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
251 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
252 if (prevRevContent != null) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
253 prevRevContent.done();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
254 prevRevContent = null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
255 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256 indexFile.close();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
257 indexFile = null;
190
9b99d27aeddc More debug printouts
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 186
diff changeset
258 filename = null;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
259 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
260 String m = String.format("Failed to write file %s", filename);
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
261 throw new HgInvalidControlFileException(m, ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
262 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
263 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
264
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
265 private int knownRevision(Nodeid p) {
274
9fb50c04f03c Use Nodeid.isNull check instead of NULL.equals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 204
diff changeset
266 if (p.isNull()) {
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
267 return -1;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
268 } else {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
269 for (int i = revisionSequence.size() - 1; i >= 0; i--) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
270 if (revisionSequence.get(i).equals(p)) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
271 return i;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
272 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
273 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
274 }
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
275 String m = String.format("Can't find index of %s for file %s", p.shortNotation(), filename);
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
276 throw new HgInvalidControlFileException(m, null, null).setRevision(p);
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
277 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
278
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
279 public boolean element(GroupElement ge) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
280 try {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
281 assert indexFile != null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
282 boolean writeComplete = false;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
283 Nodeid p1 = ge.firstParent();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
284 Nodeid p2 = ge.secondParent();
274
9fb50c04f03c Use Nodeid.isNull check instead of NULL.equals
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 204
diff changeset
285 if (p1.isNull() && p2.isNull() /* or forced flag, does REVIDX_PUNCHED_FLAG indicate that? */) {
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
286 prevRevContent = new ByteArrayDataAccess(new byte[0]);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
287 writeComplete = true;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
288 }
358
fc8bc2f1edbe Clean internal classes from public API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
289 byte[] content = ge.apply(prevRevContent.byteArray());
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
290 byte[] calculated = dh.sha1(p1, p2, content).asBinary();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
291 final Nodeid node = ge.node();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
292 if (!node.equalsTo(calculated)) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
293 // TODO post-1.0 custom exception ChecksumCalculationFailed?
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
294 throw new HgInvalidStateException(String.format("Checksum failed: expected %s, calculated %s. File %s", node, calculated, filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
295 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
296 final int link;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
297 if (collectChangelogIndexes) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
298 changelogIndexes.put(node, revisionSequence.size());
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
299 link = revisionSequence.size();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
300 } else {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
301 Integer csRev = changelogIndexes.get(ge.cset());
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
302 if (csRev == null) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
303 throw new HgInvalidStateException(String.format("Changelog doesn't contain revision %s of %s", ge.cset().shortNotation(), filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
304 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
305 link = csRev.intValue();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
306 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
307 final int p1Rev = knownRevision(p1), p2Rev = knownRevision(p2);
358
fc8bc2f1edbe Clean internal classes from public API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
308 byte[] patchContent = ge.rawDataByteArray();
fc8bc2f1edbe Clean internal classes from public API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
309 writeComplete = writeComplete || patchContent.length >= (/* 3/4 of actual */content.length - (content.length >>> 2));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
310 if (writeComplete) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
311 base = revisionSequence.size();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
312 }
358
fc8bc2f1edbe Clean internal classes from public API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
313 final byte[] sourceData = writeComplete ? content : patchContent;
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
314 final byte[] data;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
315 ByteArrayOutputStream bos = new ByteArrayOutputStream(content.length);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
316 DeflaterOutputStream dos = new DeflaterOutputStream(bos);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
317 dos.write(sourceData);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
318 dos.close();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
319 final byte[] compressedData = bos.toByteArray();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
320 dos = null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
321 bos = null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
322 final Byte dataPrefix;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
323 if (compressedData.length >= (sourceData.length - (sourceData.length >>> 2))) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
324 // compression wasn't too effective,
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
325 data = sourceData;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
326 dataPrefix = 'u';
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
327 } else {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
328 data = compressedData;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
329 dataPrefix = null;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
330 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
331
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
332 ByteBuffer header = ByteBuffer.allocate(64 /* REVLOGV1_RECORD_SIZE */);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
333 if (offset == 0) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
334 final int INLINEDATA = 1 << 16;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
335 header.putInt(1 /* RevlogNG */ | INLINEDATA);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
336 header.putInt(0);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
337 } else {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
338 header.putLong(offset << 16);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
339 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
340 final int compressedLen = data.length + (dataPrefix == null ? 0 : 1);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
341 header.putInt(compressedLen);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
342 header.putInt(content.length);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
343 header.putInt(base);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
344 header.putInt(link);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
345 header.putInt(p1Rev);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
346 header.putInt(p2Rev);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
347 header.put(node.toByteArray());
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
348 // assume 12 bytes left are zeros
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
349 indexFile.write(header.array());
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
350 if (dataPrefix != null) {
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
351 indexFile.write(dataPrefix.byteValue());
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
352 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
353 indexFile.write(data);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
354 //
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
355 offset += compressedLen;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
356 revisionSequence.add(node);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
357 prevRevContent.done();
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
358 prevRevContent = new ByteArrayDataAccess(content);
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
359 } catch (IOException ex) {
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
360 String m = String.format("Failed to write revision %s of file %s", ge.node().shortNotation(), filename);
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
361 throw new HgInvalidControlFileException(m, ex, new File(filename));
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
362 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
363 return true;
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
364 }
512
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
365
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
366 public void start(int count, Callback callback, Object token) {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
367 progressSupport.start(count);
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
368 lifecycleCallback = callback;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
369 }
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
370
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
371 public void finish(Object token) {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
372 progressSupport.done();
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
373 lifecycleCallback = null;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
374 }
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
375
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
376 public void checkFailure() throws CancelledException {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
377 if (cancelException != null) {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
378 throw cancelException;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
379 }
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
380 }
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
381
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
382 private void stopIfCancelled() {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
383 try {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
384 cancelSupport.checkCancelled();
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
385 return;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
386 } catch (CancelledException ex) {
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
387 cancelException = ex;
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
388 lifecycleCallback.stop();
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
389 }
10ca3ede8367 Issue 39: Progress and Cancel support for Clone command
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 490
diff changeset
390 }
186
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
391 }
44a34baabea0 Clone refactored into a command. HgBundle needs means to control its lifecycle, to be deleted when no longer needed
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
392 }