Create NoWorkTreeException for bare repositories

Using a custom exception type makes it easire for an application
developer to understand why an exception was thrown out of a method
we declare.  To remain compatiable with existing callers, we still
extend off IllegalStateException.

Change-Id: Ideeef2399b11ca460a2dbb3cd80eb76aa0a025ba
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-06-30 09:48:36 -07:00
parent 515deaf7e5
commit cb9d8285ba
4 changed files with 87 additions and 30 deletions

View File

@ -48,6 +48,7 @@
import java.io.IOException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
@ -136,8 +137,8 @@ public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
File gitDir = getFile("workdir");
try {
new FileRepository(gitDir).getWorkTree();
fail("Expected IllegalStateException missing");
} catch (IllegalStateException e) {
fail("Expected NoWorkTreeException missing");
} catch (NoWorkTreeException e) {
// expected
}
}
@ -146,8 +147,8 @@ public void testExceptionThrown_BareRepoGetIndex() throws Exception {
File gitDir = getFile("workdir");
try {
new FileRepository(gitDir).getIndex();
fail("Expected IllegalStateException missing");
} catch (IllegalStateException e) {
fail("Expected NoWorkTreeException missing");
} catch (NoWorkTreeException e) {
// expected
}
}
@ -156,8 +157,8 @@ public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
File gitDir = getFile("workdir");
try {
new FileRepository(gitDir).getIndexFile();
fail("Expected Exception missing");
} catch (IllegalStateException e) {
fail("Expected NoWorkTreeException missing");
} catch (NoWorkTreeException e) {
// expected
}
}

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
@ -168,7 +169,7 @@ public static DirCache read(final File indexLocation)
* repository the caller wants to read the default index of.
* @return a cache representing the contents of the specified index file (if
* it exists) or an empty cache if the file does not exist.
* @throws IllegalStateException
* @throws NoWorkTreeException
* if the repository is bare (lacks a working directory).
* @throws IOException
* the index file is present but could not be read.
@ -177,7 +178,7 @@ public static DirCache read(final File indexLocation)
* library does not support.
*/
public static DirCache read(final Repository db)
throws CorruptObjectException, IOException {
throws NoWorkTreeException, CorruptObjectException, IOException {
return read(db.getIndexFile());
}
@ -233,7 +234,7 @@ public static DirCache lock(final File indexLocation)
* repository the caller wants to read the default index of.
* @return a cache representing the contents of the specified index file (if
* it exists) or an empty cache if the file does not exist.
* @throws IllegalStateException
* @throws NoWorkTreeException
* if the repository is bare (lacks a working directory).
* @throws IOException
* the index file is present but could not be read, or the lock
@ -243,7 +244,7 @@ public static DirCache lock(final File indexLocation)
* library does not support.
*/
public static DirCache lock(final Repository db)
throws CorruptObjectException, IOException {
throws NoWorkTreeException, CorruptObjectException, IOException {
return lock(db.getIndexFile());
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (C) 2010, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.errors;
import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.lib.Repository;
/**
* Indicates a {@link Repository} has no working directory, and is thus bare.
*/
public class NoWorkTreeException extends IllegalStateException {
private static final long serialVersionUID = 1L;
/** Creates an exception indicating there is no work tree for a repository. */
public NoWorkTreeException() {
super(JGitText.get().bareRepositoryNoWorkdirAndIndex);
}
}

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.errors.RevisionSyntaxException;
import org.eclipse.jgit.events.ListenerList;
import org.eclipse.jgit.events.RepositoryEvent;
@ -860,13 +861,12 @@ public Map<AnyObjectId, Set<Ref>> getAllRefsByPeeledObjectId() {
* {@link Repository}
* @throws IOException
* if the index can not be read
* @throws IllegalStateException
* @throws NoWorkTreeException
* if this is bare (see {@link #isBare()})
*/
public GitIndex getIndex() throws IOException, IllegalStateException {
public GitIndex getIndex() throws IOException, NoWorkTreeException {
if (isBare())
throw new IllegalStateException(
JGitText.get().bareRepositoryNoWorkdirAndIndex);
throw new NoWorkTreeException();
if (index == null) {
index = new GitIndex(this);
index.read();
@ -878,13 +878,12 @@ public GitIndex getIndex() throws IOException, IllegalStateException {
/**
* @return the index file location
* @throws IllegalStateException
* @throws NoWorkTreeException
* if this is bare (see {@link #isBare()})
*/
public File getIndexFile() throws IllegalStateException {
public File getIndexFile() throws NoWorkTreeException {
if (isBare())
throw new IllegalStateException(
JGitText.get().bareRepositoryNoWorkdirAndIndex);
throw new NoWorkTreeException();
return indexFile;
}
@ -1036,13 +1035,12 @@ public boolean isBare() {
/**
* @return the root directory of the working tree, where files are checked
* out for viewing and editing.
* @throws IllegalStateException
* @throws NoWorkTreeException
* if the repository is bare and has no working directory.
*/
public File getWorkTree() throws IllegalStateException {
public File getWorkTree() throws NoWorkTreeException {
if (isBare())
throw new IllegalStateException(
JGitText.get().bareRepositoryNoWorkdirAndIndex);
throw new NoWorkTreeException();
return workTree;
}
@ -1085,13 +1083,12 @@ public abstract ReflogReader getReflogReader(String refName)
* @return a String containing the content of the MERGE_MSG file or
* {@code null} if this file doesn't exist
* @throws IOException
* @throws IllegalStateException
* @throws NoWorkTreeException
* if the repository is "bare"
*/
public String readMergeCommitMsg() throws IOException {
public String readMergeCommitMsg() throws IOException, NoWorkTreeException {
if (isBare() || getDirectory() == null)
throw new IllegalStateException(
JGitText.get().bareRepositoryNoWorkdirAndIndex);
throw new NoWorkTreeException();
File mergeMsgFile = new File(getDirectory(), Constants.MERGE_MSG);
try {
@ -1112,13 +1109,12 @@ public String readMergeCommitMsg() throws IOException {
* file or {@code null} if this file doesn't exist. Also if the file
* exists but is empty {@code null} will be returned
* @throws IOException
* @throws IllegalStateException
* @throws NoWorkTreeException
* if the repository is "bare"
*/
public List<ObjectId> readMergeHeads() throws IOException {
public List<ObjectId> readMergeHeads() throws IOException, NoWorkTreeException {
if (isBare() || getDirectory() == null)
throw new IllegalStateException(
JGitText.get().bareRepositoryNoWorkdirAndIndex);
throw new NoWorkTreeException();
File mergeHeadFile = new File(getDirectory(), Constants.MERGE_HEAD);
byte[] raw;