From 2785d2d8723fe337de131788b86da4c7b92aacd8 Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Sun, 13 Apr 2008 03:07:20 +0200 Subject: [PATCH] Make revision parser more string and manage more cases Signed-off-by: Robin Rosenberg --- .../org/spearce/jgit/lib/T0008_testparserev.java | 181 +++++++++++---------- .../jgit/errors/RevisionSyntaxException.java | 55 +++++++ .../src/org/spearce/jgit/lib/Repository.java | 96 ++++++++--- 3 files changed, 222 insertions(+), 110 deletions(-) rewrite org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0008_testparserev.java (80%) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/RevisionSyntaxException.java diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0008_testparserev.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0008_testparserev.java dissimilarity index 80% index d15ae6f1..b5e84e6c 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0008_testparserev.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0008_testparserev.java @@ -1,85 +1,96 @@ -/* - * Copyright (C) 2007 Robin Rosenberg - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License, version 2, as published by the Free Software Foundation. - * - * This library 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. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 - */ -package org.spearce.jgit.lib; - -import java.io.IOException; - -public class T0008_testparserev extends RepositoryTestCase { - - ObjectId resolve(String in) { - try { - return db.resolve(in); - } catch (IOException e) { - throw new Error(e); - } - } - - public void testObjectId_existing() { - assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0").toString()); - } - - public void testObjectId_nonexisting() { - assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c1",resolve("49322bb17d3acc9146f98c97d078513228bbf3c1").toString()); - } - - public void testObjectId_objectid_implicit_firstparent() { - assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^").toString()); - assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^").toString()); - assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^^").toString()); - } - - public void testObjectId_objectid_self() { - assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0").toString()); - assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0^0").toString()); - assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0^0^0").toString()); - } - - public void testObjectId_objectid_explicit_firstparent() { - assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1").toString()); - assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1^1").toString()); - assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1^1^1").toString()); - } - - public void testObjectId_objectid_explicit_otherparents() { - assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1").toString()); - assertEquals("f73b95671f326616d66b2afb3bdfcdbbce110b44",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^2").toString()); - assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^3").toString()); - assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^03").toString()); - } - - public void testRef_refname() { - assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",resolve("master^0").toString()); - assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",resolve("master^").toString()); - assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",resolve("refs/heads/master^1").toString()); - } - - public void testDistance() { - assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~0").toString()); - assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~1").toString()); - assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~2").toString()); - assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~02").toString()); - } - - public void testTree() { - assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{tree}").toString()); - assertEquals("02ba32d3649e510002c21651936b7077aa75ffa9",resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^{tree}").toString()); - } - - public void testHEAD() { - assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",resolve("HEAD^{tree}").toString()); - } -} +/* + * Copyright (C) 2007 Robin Rosenberg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This library 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. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + */ +package org.spearce.jgit.lib; + +import java.io.IOException; + +public class T0008_testparserev extends RepositoryTestCase { + + public void testObjectId_existing() throws IOException { + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0").toString()); + } + + public void testObjectId_nonexisting() throws IOException { + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c1",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c1").toString()); + } + + public void testObjectId_objectid_implicit_firstparent() throws IOException { + assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^").toString()); + assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^").toString()); + assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^^").toString()); + } + + public void testObjectId_objectid_self() throws IOException { + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0").toString()); + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0^0").toString()); + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0^0^0").toString()); + } + + public void testObjectId_objectid_explicit_firstparent() throws IOException { + assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1").toString()); + assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1^1").toString()); + assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1^1^1").toString()); + } + + public void testObjectId_objectid_explicit_otherparents() throws IOException { + assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1").toString()); + assertEquals("f73b95671f326616d66b2afb3bdfcdbbce110b44",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^2").toString()); + assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^3").toString()); + assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^03").toString()); + } + + public void testRef_refname() throws IOException { + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("master^0").toString()); + assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("master^").toString()); + assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("refs/heads/master^1").toString()); + } + + public void testDistance() throws IOException { + assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~0").toString()); + assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~1").toString()); + assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~2").toString()); + assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~02").toString()); + } + + public void testTree() throws IOException { + assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{tree}").toString()); + assertEquals("02ba32d3649e510002c21651936b7077aa75ffa9",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^{tree}").toString()); + } + + public void testHEAD() throws IOException { + assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("HEAD^{tree}").toString()); + } + + public void testDerefCommit() throws IOException { + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{}").toString()); + assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{commit}").toString()); + // double deref + assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{commit}^{tree}").toString()); + } + + public void testDerefTag() throws IOException { + assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("spearce-gpg-pub^{}").toString()); + assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("spearce-gpg-pub^{blob}").toString()); + } + + public void testDerefBlob() throws IOException { + assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("fd608fbe625a2b456d9f15c2b1dc41f252057dd7^{}").toString()); + assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("fd608fbe625a2b456d9f15c2b1dc41f252057dd7^{blob}").toString()); + } + + // TODO: ^{tree} for a tag pointing to a tag +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/RevisionSyntaxException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/RevisionSyntaxException.java new file mode 100644 index 00000000..e8498df9 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/errors/RevisionSyntaxException.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2008 Robin Rosenberg + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This library 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. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + */ +package org.spearce.jgit.errors; + +import java.io.IOException; + +/** + * This signals a revision or object reference was not + * properly formatted. + */ +public class RevisionSyntaxException extends IOException { + + private final String revstr; + + /** + * Construct a RevisionSyntaxException indicating a syntax problem with a + * revision (or object) string. + * + * @param revstr The problematic revision string + */ + public RevisionSyntaxException(String revstr) { + this.revstr = revstr; + } + + /** + * Construct a RevisionSyntaxException indicating a syntax problem with a + * revision (or object) string. + * + * @param message a specific reason + * @param revstr The problematic revision string + */ + public RevisionSyntaxException(String message, String revstr) { + super(message); + this.revstr = revstr; + } + + @Override + public String toString() { + return super.toString() + ":" + revstr; + } +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java index e2fd0f3f..455a68e2 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java @@ -30,6 +30,7 @@ import java.util.Set; import org.spearce.jgit.errors.IncorrectObjectTypeException; import org.spearce.jgit.errors.ObjectWritingException; +import org.spearce.jgit.errors.RevisionSyntaxException; import org.spearce.jgit.stgit.StGitPatch; import org.spearce.jgit.util.FS; @@ -496,17 +497,16 @@ public class Repository { */ public ObjectId resolve(final String revstr) throws IOException { char[] rev = revstr.toCharArray(); - ObjectId ret = null; - Commit ref = null; + Object ref = null; + ObjectId refId = null; for (int i = 0; i < rev.length; ++i) { switch (rev[i]) { case '^': - if (ref == null) { + if (refId == null) { String refstr = new String(rev,0,i); - ObjectId refId = resolveSimple(refstr); + refId = resolveSimple(refstr); if (refId == null) return null; - ref = mapCommit(refId); } if (i + 1 < rev.length) { switch (rev[i + 1]) { @@ -521,6 +521,9 @@ public class Repository { case '8': case '9': int j; + ref = mapObject(refId, null); + if (!(ref instanceof Commit)) + throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT); for (j=i+1; j= 0) { - ref = mapCommit(ref.getParentIds()[0]); + refId = ((Commit)ref).getParentIds()[0]; + ref = mapCommit(refId); --dist; } i = l - 1; @@ -587,20 +636,17 @@ public class Repository { } } if (time != null) - throw new IllegalArgumentException("reflogs not yet supprted"); + throw new RevisionSyntaxException("reflogs not yet supported by revision parser yet", revstr); i = m - 1; break; default: - if (ref != null) - return null; // cannot parse, return null + if (refId != null) + throw new RevisionSyntaxException(revstr); } } - if (ret == null) - if (ref != null) - ret = ref.getCommitId(); - else - ret = resolveSimple(revstr); - return ret; + if (refId == null) + refId = resolveSimple(revstr); + return refId; } private ObjectId resolveSimple(final String revstr) throws IOException { -- 2.11.4.GIT