annotate src/org/tmatesoft/hg/internal/FNCacheFile.java @ 546:cd78e8b9d7bc

File annotate test. Refactored FileAnnotation as standalone class, introduced LineInspector to make line offset calc code shared
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 18 Feb 2013 19:19:48 +0100
parents 9edfd5a223b8
children 6ca3d0c5b4bc
rev   line source
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.File;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.FileOutputStream;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.nio.charset.Charset;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.ArrayList;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.tmatesoft.hg.util.Path;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 /**
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 * <blockquote>
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 * The fncache file contains the paths of all filelog files in the store as encoded by mercurial.filelog.encodedir. The paths are separated by '\n' (LF).
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 * </blockquote>
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * @author Artem Tikhomirov
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * @author TMate Software Ltd.
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 */
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 public class FNCacheFile {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 private final Internals repo;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 private final ArrayList<Path> files;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 public FNCacheFile(Internals internalRepo) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 repo = internalRepo;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 files = new ArrayList<Path>();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 public void read(Path.Source pathFactory) throws IOException {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 File f = fncacheFile();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 files.clear();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 if (!f.exists()) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 return;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 ArrayList<String> entries = new ArrayList<String>();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 // names in fncache are in local encoding, shall translate to unicode
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 new LineReader(f, repo.getSessionContext().getLog(), repo.getFilenameEncoding()).read(new LineReader.SimpleLineCollector(), entries);
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 for (String e : entries) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 files.add(pathFactory.path(e));
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 public void write() throws IOException {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 if (files.isEmpty()) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 return;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 File f = fncacheFile();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 f.getParentFile().mkdirs();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 final Charset filenameEncoding = repo.getFilenameEncoding();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 FileOutputStream fncacheFile = new FileOutputStream(f);
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 for (Path p : files) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 String s = "data/" + p.toString() + ".i"; // TODO post-1.0 this is plain wrong. (a) likely need .d files, too; (b) what about dh/ location?
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 fncacheFile.write(s.getBytes(filenameEncoding));
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 fncacheFile.write(0x0A); // http://mercurial.selenic.com/wiki/fncacheRepoFormat
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 fncacheFile.close();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 public void add(Path p) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 files.add(p);
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 private File fncacheFile() {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 return repo.getFileFromStoreDir("fncache");
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }