Browse Source

Merge branch 'stable-5.3'

* stable-5.3:
  Reduce contention on PackFile.idx() function.
  Use SystemReader in JSchConfigSessionFactoryTest
  Avoid NPE in ObjectId.isId()

Change-Id: I1d13f6fb705258ae6d6e5fa5e733bfacd4f3d0e3
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-5.4
Matthias Sohn 6 years ago
parent
commit
51f2979f13
  1. 12
      org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/JschConfigSessionFactoryTest.java
  2. 61
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackFile.java
  3. 6
      org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java

12
org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/JschConfigSessionFactoryTest.java

@ -49,8 +49,11 @@ import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
@ -67,8 +70,14 @@ public class JschConfigSessionFactoryTest {
DefaultSshSessionFactory factory = new DefaultSshSessionFactory(); DefaultSshSessionFactory factory = new DefaultSshSessionFactory();
@Before
public void setup() {
SystemReader.setInstance(new MockSystemReader());
}
@After @After
public void removeTmpConfig() { public void removeTmpConfig() {
SystemReader.setInstance(null);
if (tmpConfigFile == null) { if (tmpConfigFile == null) {
return; return;
} }
@ -87,7 +96,8 @@ public class JschConfigSessionFactoryTest {
Session session = createSession("ssh://egit/egit/egit"); Session session = createSession("ssh://egit/egit/egit");
assertEquals("egit", session.getHost()); assertEquals("egit", session.getHost());
// No user in URI, none in ssh config: default is OS user name // No user in URI, none in ssh config: default is OS user name
assertEquals(System.getProperty("user.name"), session.getUserName()); assertEquals(SystemReader.getInstance().getProperty("user.name"),
session.getUserName());
assertEquals(22, session.getPort()); assertEquals(22, session.getPort());
} }

61
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackFile.java

@ -139,7 +139,7 @@ public class PackFile implements Iterable<PackIndex.MutableEntry> {
private byte[] packChecksum; private byte[] packChecksum;
private PackIndex loadedIdx; private volatile PackIndex loadedIdx;
private PackReverseIndex reverseIdx; private PackReverseIndex reverseIdx;
@ -174,35 +174,44 @@ public class PackFile implements Iterable<PackIndex.MutableEntry> {
length = Long.MAX_VALUE; length = Long.MAX_VALUE;
} }
private synchronized PackIndex idx() throws IOException { private PackIndex idx() throws IOException {
if (loadedIdx == null) { PackIndex idx = loadedIdx;
if (invalid) if (idx == null) {
throw new PackInvalidException(packFile); synchronized (this) {
idx = loadedIdx;
try { if (idx == null) {
final PackIndex idx = PackIndex.open(extFile(INDEX)); if (invalid) {
throw new PackInvalidException(packFile);
if (packChecksum == null) { }
packChecksum = idx.packChecksum; try {
} else if (!Arrays.equals(packChecksum, idx.packChecksum)) { idx = PackIndex.open(extFile(INDEX));
throw new PackMismatchException(MessageFormat.format(
JGitText.get().packChecksumMismatch, if (packChecksum == null) {
packFile.getPath(), packChecksum = idx.packChecksum;
ObjectId.fromRaw(packChecksum).name(), } else if (!Arrays.equals(packChecksum,
ObjectId.fromRaw(idx.packChecksum).name())); idx.packChecksum)) {
throw new PackMismatchException(MessageFormat
.format(JGitText.get().packChecksumMismatch,
packFile.getPath(),
ObjectId.fromRaw(packChecksum)
.name(),
ObjectId.fromRaw(idx.packChecksum)
.name()));
}
loadedIdx = idx;
} catch (InterruptedIOException e) {
// don't invalidate the pack, we are interrupted from
// another thread
throw e;
} catch (IOException e) {
invalid = true;
throw e;
}
} }
loadedIdx = idx;
} catch (InterruptedIOException e) {
// don't invalidate the pack, we are interrupted from another thread
throw e;
} catch (IOException e) {
invalid = true;
throw e;
} }
} }
return loadedIdx; return idx;
} }
/** /**
* Get the File object which locates this pack on disk. * Get the File object which locates this pack on disk.
* *

6
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectId.java

@ -49,6 +49,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.InvalidObjectIdException; import org.eclipse.jgit.errors.InvalidObjectIdException;
import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.NB;
import org.eclipse.jgit.util.RawParseUtils; import org.eclipse.jgit.util.RawParseUtils;
@ -86,7 +87,10 @@ public class ObjectId extends AnyObjectId implements Serializable {
* the string to test. * the string to test.
* @return true if the string can converted into an ObjectId. * @return true if the string can converted into an ObjectId.
*/ */
public static final boolean isId(String id) { public static final boolean isId(@Nullable String id) {
if (id == null) {
return false;
}
if (id.length() != Constants.OBJECT_ID_STRING_LENGTH) if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
return false; return false;
try { try {

Loading…
Cancel
Save