# HG changeset patch # User Artem Tikhomirov # Date 1360076077 -3600 # Node ID d9c07e1432c48aa81ba7c3c0e7354beac4125712 # Parent 243202f1bda5654f7930142f19f2075f756d881f Issue 42: tolerate lines in .hgtags that do not conform to its format specification diff -r 243202f1bda5 -r d9c07e1432c4 src/org/tmatesoft/hg/core/Nodeid.java --- a/src/org/tmatesoft/hg/core/Nodeid.java Mon Feb 04 18:00:55 2013 +0100 +++ b/src/org/tmatesoft/hg/core/Nodeid.java Tue Feb 05 15:54:37 2013 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2011 TMate Software Ltd + * Copyright (c) 2010-2013 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 @@ -166,7 +166,7 @@ * @return object representation * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when argument doesn't match encoded form of 20-bytes sha1 digest. */ - public static Nodeid fromAscii(String asciiRepresentation) { + public static Nodeid fromAscii(String asciiRepresentation) throws HgBadNodeidFormatException { if (asciiRepresentation.length() != 40) { throw new HgBadNodeidFormatException(String.format("Bad value: %s", asciiRepresentation)); } @@ -178,9 +178,9 @@ * Parse encoded representation. Similar to {@link #fromAscii(String)}. * @throws HgBadNodeidFormatException custom {@link IllegalArgumentException} subclass when bytes are not hex digits or number of bytes != 40 (160 bits) */ - public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) { + public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) throws HgBadNodeidFormatException { if (length != 40) { - throw new IllegalArgumentException(); + throw new HgBadNodeidFormatException(String.format("Expected 40 hex characters for nodeid, not %d", length)); } try { byte[] data = new byte[20]; @@ -189,6 +189,8 @@ return NULL; } return new Nodeid(data, false); + } catch (HgBadNodeidFormatException ex) { + throw ex; } catch (IllegalArgumentException ex) { throw new HgBadNodeidFormatException(ex.getMessage()); } diff -r 243202f1bda5 -r d9c07e1432c4 src/org/tmatesoft/hg/repo/HgTags.java --- a/src/org/tmatesoft/hg/repo/HgTags.java Mon Feb 04 18:00:55 2013 +0100 +++ b/src/org/tmatesoft/hg/repo/HgTags.java Tue Feb 05 15:54:37 2013 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2012 TMate Software Ltd + * Copyright (c) 2011-2013 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 @@ -16,6 +16,7 @@ */ package org.tmatesoft.hg.repo; +import static org.tmatesoft.hg.util.LogFacility.Severity.Error; import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; import java.io.BufferedReader; @@ -31,6 +32,7 @@ import java.util.Map; import java.util.TreeMap; +import org.tmatesoft.hg.core.HgBadNodeidFormatException; import org.tmatesoft.hg.core.Nodeid; /** @@ -108,12 +110,12 @@ if (line.length() == 0) { continue; } - if (line.length() < 40+2 /*nodeid, space and at least single-char tagname*/) { + final int spacePos = line.indexOf(' '); + if (line.length() < 40+2 /*nodeid, space and at least single-char tagname*/ || spacePos != 40) { repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad tags line: %s", line); continue; } - int spacePos = line.indexOf(' '); - if (spacePos != -1) { + try { assert spacePos == 40; final byte[] nodeidBytes = line.substring(0, spacePos).getBytes(); Nodeid nid = Nodeid.fromAscii(nodeidBytes, 0, nodeidBytes.length); @@ -151,9 +153,8 @@ // !contains because we don't care about order of the tags per revision revTags.add(tagName); } - - } else { - repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad tags line: %s", line); + } catch (HgBadNodeidFormatException ex) { + repo.getSessionContext().getLog().dump(getClass(), Error, "Bad revision '%s' in line '%s':%s", line.substring(0, spacePos), line, ex.getMessage()); } } }