Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/DirstateBuilder.java @ 525:0be5be8d57e9
Repository checkout support, first iteration
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 11 Jan 2013 18:12:39 +0100 |
parents | |
children | 2f9ed6bcefa2 |
rev | line source |
---|---|
525
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012-2013 TMate Software Ltd |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.internal; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import java.io.IOException; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import java.nio.ByteBuffer; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 import java.nio.channels.WritableByteChannel; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.util.ArrayList; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23 import java.util.List; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import org.tmatesoft.hg.core.Nodeid; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import org.tmatesoft.hg.repo.HgDirstate; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 import org.tmatesoft.hg.repo.HgManifest.Flags; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import org.tmatesoft.hg.util.Path; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 /** |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 * Facility to build a dirstate file as described in {@linkplain http://mercurial.selenic.com/wiki/DirState} |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 * |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 * @see http://mercurial.selenic.com/wiki/DirState |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 * @see HgDirstate |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35 * @author Artem Tikhomirov |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 * @author TMate Software Ltd. |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 */ |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 public class DirstateBuilder { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 private List<HgDirstate.Record> normal = new ArrayList<HgDirstate.Record>(); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 private Nodeid parent1, parent2; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 private final EncodingHelper encodingHelper; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 public DirstateBuilder(EncodingHelper encHelper) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 encodingHelper = encHelper; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 public void parents(Nodeid p1, Nodeid p2) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 parent1 = p1 == null ? Nodeid.NULL : p1; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 parent2 = p2 == null ? Nodeid.NULL : p2; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 public void recordNormal(Path fname, Flags flags, int bytesWritten) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 // Mercurial seems to write "n 0 -1 unset fname" on `hg --clean co -rev <earlier rev>` |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 // and the reason for 'force lookup' I suspect is a slight chance of simultaneous modification |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 // of the file by user that doesn't alter its size the very second dirstate is being written |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 // (or the file is being updated and the update brought in changes that didn't alter the file size - |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 // with size and timestamp set, later `hg status` won't notice these changes) |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 // However, as long as we use this class to write clean copies of the files, we can put all the fields |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 // right away. |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 int fmode = flags == Flags.RegularFile ? 0666 : 0777; // FIXME actual unix flags |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 int mtime = (int) (System.currentTimeMillis() / 1000); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 normal.add(new HgDirstate.Record(fmode, bytesWritten, mtime,fname, null)); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 public void serialize(WritableByteChannel dest) throws IOException { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 assert parent1 != null : "Parent(s) of the working directory shall be set first"; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 ByteBuffer bb = ByteBuffer.allocate(256); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 bb.put(parent1.toByteArray()); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 bb.put(parent2.toByteArray()); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 bb.flip(); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 // header |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 int written = dest.write(bb); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 if (written != bb.limit()) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 throw new IOException("Incomplete write"); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 bb.clear(); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 // entries |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 for (HgDirstate.Record r : normal) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 // normal entry is 1+4+4+4+4+fname.length bytes |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 byte[] fname = encodingHelper.toDirstate(r.name().toString()); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 bb = ensureCapacity(bb, 17 + fname.length); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 bb.put((byte) 'n'); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 bb.putInt(r.mode()); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 bb.putInt(r.size()); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 bb.putInt(r.modificationTime()); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 bb.putInt(fname.length); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 bb.put(fname); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 bb.flip(); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 written = dest.write(bb); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 if (written != bb.limit()) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 throw new IOException("Incomplete write"); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 bb.clear(); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 private static ByteBuffer ensureCapacity(ByteBuffer buf, int cap) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 if (buf.capacity() >= cap) { |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
101 return buf; |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 return ByteBuffer.allocate(cap); |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 } |
0be5be8d57e9
Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 } |