# HG changeset patch # User Artem Tikhomirov # Date 1329135606 -3600 # Node ID 4b97847d0b2d648cd2995a573c2421df4b52a51d # Parent 994b5813a92548597eb863ae8cdfbf3a9400dfc0 Auxilary builders for manifest and changelog to create test repositories diff -r 994b5813a925 -r 4b97847d0b2d test/org/tmatesoft/hg/tools/ChangelogEntryBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/tools/ChangelogEntryBuilder.java Mon Feb 13 13:20:06 2012 +0100 @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2012 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@hg4j.com + */ +package org.tmatesoft.hg.tools; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; +import java.util.Map.Entry; + +import org.tmatesoft.hg.core.Nodeid; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class ChangelogEntryBuilder { + + private String user; + private List modifiedFiles; + private final Map extrasMap = new LinkedHashMap(); + private Integer tzOffset; + private Long csetTime; + + public ChangelogEntryBuilder user(String username) { + user = username; + return this; + } + + public String user() { + if (user == null) { + // for our testing purposes anything but null is ok. no reason to follow Hg username lookup conventions + user = System.getProperty("user.name"); + } + return user; + } + + public ChangelogEntryBuilder setModified(List files) { + modifiedFiles = new ArrayList(files == null ? Collections.emptyList() : files); + return this; + } + + public ChangelogEntryBuilder addModified(List files) { + if (modifiedFiles == null) { + return setModified(files); + } + modifiedFiles.addAll(files); + return this; + } + + public ChangelogEntryBuilder branch(String branchName) { + if (branchName == null) { + extrasMap.remove("branch"); + } else { + extrasMap.put("branch", branchName); + } + return this; + } + + public ChangelogEntryBuilder extras(Map extras) { + extrasMap.clear(); + extrasMap.putAll(extras); + return this; + } + + public ChangelogEntryBuilder date(long seconds, int timezoneOffset) { + csetTime = seconds; + tzOffset = timezoneOffset; + return this; + } + + private long csetTime() { + if (csetTime != null) { + return csetTime; + } + return System.currentTimeMillis() / 1000; + } + + private int csetTimezone(long time) { + if (tzOffset != null) { + return tzOffset; + } + return -(TimeZone.getDefault().getOffset(time) / 1000); + } + + public byte[] build(Nodeid manifestRevision, String comment) { + String f = "%s\n%s\n%d %d %s\n%s\n\n%s"; + StringBuilder extras = new StringBuilder(); + for (Iterator> it = extrasMap.entrySet().iterator(); it.hasNext();) { + final Entry next = it.next(); + extras.append(encodeExtrasPair(next.getKey())); + extras.append(':'); + extras.append(encodeExtrasPair(next.getValue())); + if (it.hasNext()) { + extras.append('\00'); + } + } + StringBuilder files = new StringBuilder(); + for (Iterator it = modifiedFiles.iterator(); it.hasNext(); ) { + files.append(it.next()); + if (it.hasNext()) { + files.append('\n'); + } + } + final long date = csetTime(); + final int tz = csetTimezone(date); + return String.format(f, manifestRevision.toString(), user, date, tz, extras, files, comment).getBytes(); + } + + private final static CharSequence encodeExtrasPair(String s) { + if (s != null) { + return s.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r").replace("\00", "\\0"); + } + return s; + } +} diff -r 994b5813a925 -r 4b97847d0b2d test/org/tmatesoft/hg/tools/ManifestEntryBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/tools/ManifestEntryBuilder.java Mon Feb 13 13:20:06 2012 +0100 @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2012 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@hg4j.com + */ +package org.tmatesoft.hg.tools; + +import java.io.ByteArrayOutputStream; + +import org.tmatesoft.hg.core.Nodeid; + +/** + * Create binary manifest entry ready to write down into 00manifest.i + *

Usage: + *

+ *   ManifestEntryBuilder mb = new ManifestEntryBuilder();
+ *   mb.reset().add("file1", file1.getRevision(r1));
+ *   mb.add("file2", file2.getRevision(r2));
+ *   byte[] manifestRecordData = mb.build();
+ *   byte[] manifestRevlogHeader = buildRevlogHeader(..., sha1(parents, manifestRecordData), manifestRecordData.length);
+ *   manifestIndexOutputStream.write(manifestRevlogHeader);
+ *   manifestIndexOutputStream.write(manifestRecordData);
+ * 
+ * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class ManifestEntryBuilder { + private ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + + public ManifestEntryBuilder reset() { + buffer.reset(); + return this; + } + public ManifestEntryBuilder add(String fname, Nodeid revision) { + byte[] b = fname.getBytes(); + buffer.write(b, 0, b.length); + buffer.write('\0'); + b = revision.toString().getBytes(); + buffer.write(b, 0, b.length); + buffer.write('\n'); + return this; + } + + public byte[] build() { + return buffer.toByteArray(); + } + +}