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