comparison test/org/tmatesoft/hg/tools/ChangelogEntryBuilder.java @ 384:4b97847d0b2d

Auxilary builders for manifest and changelog to create test repositories
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 13 Feb 2012 13:20:06 +0100
parents
children 73e875154afb
comparison
equal deleted inserted replaced
383:994b5813a925 384:4b97847d0b2d
1 /*
2 * Copyright (c) 2012 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.tools;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.TimeZone;
26 import java.util.Map.Entry;
27
28 import org.tmatesoft.hg.core.Nodeid;
29
30 /**
31 *
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public class ChangelogEntryBuilder {
36
37 private String user;
38 private List<String> modifiedFiles;
39 private final Map<String, String> extrasMap = new LinkedHashMap<String, String>();
40 private Integer tzOffset;
41 private Long csetTime;
42
43 public ChangelogEntryBuilder user(String username) {
44 user = username;
45 return this;
46 }
47
48 public String user() {
49 if (user == null) {
50 // for our testing purposes anything but null is ok. no reason to follow Hg username lookup conventions
51 user = System.getProperty("user.name");
52 }
53 return user;
54 }
55
56 public ChangelogEntryBuilder setModified(List<String> files) {
57 modifiedFiles = new ArrayList<String>(files == null ? Collections.<String>emptyList() : files);
58 return this;
59 }
60
61 public ChangelogEntryBuilder addModified(List<String> files) {
62 if (modifiedFiles == null) {
63 return setModified(files);
64 }
65 modifiedFiles.addAll(files);
66 return this;
67 }
68
69 public ChangelogEntryBuilder branch(String branchName) {
70 if (branchName == null) {
71 extrasMap.remove("branch");
72 } else {
73 extrasMap.put("branch", branchName);
74 }
75 return this;
76 }
77
78 public ChangelogEntryBuilder extras(Map<String, String> extras) {
79 extrasMap.clear();
80 extrasMap.putAll(extras);
81 return this;
82 }
83
84 public ChangelogEntryBuilder date(long seconds, int timezoneOffset) {
85 csetTime = seconds;
86 tzOffset = timezoneOffset;
87 return this;
88 }
89
90 private long csetTime() {
91 if (csetTime != null) {
92 return csetTime;
93 }
94 return System.currentTimeMillis() / 1000;
95 }
96
97 private int csetTimezone(long time) {
98 if (tzOffset != null) {
99 return tzOffset;
100 }
101 return -(TimeZone.getDefault().getOffset(time) / 1000);
102 }
103
104 public byte[] build(Nodeid manifestRevision, String comment) {
105 String f = "%s\n%s\n%d %d %s\n%s\n\n%s";
106 StringBuilder extras = new StringBuilder();
107 for (Iterator<Entry<String, String>> it = extrasMap.entrySet().iterator(); it.hasNext();) {
108 final Entry<String, String> next = it.next();
109 extras.append(encodeExtrasPair(next.getKey()));
110 extras.append(':');
111 extras.append(encodeExtrasPair(next.getValue()));
112 if (it.hasNext()) {
113 extras.append('\00');
114 }
115 }
116 StringBuilder files = new StringBuilder();
117 for (Iterator<String> it = modifiedFiles.iterator(); it.hasNext(); ) {
118 files.append(it.next());
119 if (it.hasNext()) {
120 files.append('\n');
121 }
122 }
123 final long date = csetTime();
124 final int tz = csetTimezone(date);
125 return String.format(f, manifestRevision.toString(), user, date, tz, extras, files, comment).getBytes();
126 }
127
128 private final static CharSequence encodeExtrasPair(String s) {
129 if (s != null) {
130 return s.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r").replace("\00", "\\0");
131 }
132 return s;
133 }
134 }