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