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

View File

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