You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CGitLockFileTest.java 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (C) 2021 SAP SE and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.junit.Assert.assertNotEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import org.eclipse.jgit.api.Git;
  15. import org.eclipse.jgit.junit.RepositoryTestCase;
  16. import org.eclipse.jgit.revwalk.RevCommit;
  17. import org.eclipse.jgit.util.FS;
  18. import org.eclipse.jgit.util.FS.ExecutionResult;
  19. import org.junit.Test;
  20. /**
  21. * Unit tests of {@link LockFile} testing interoperability with C git
  22. */
  23. public class CGitLockFileTest extends RepositoryTestCase {
  24. @Test
  25. public void testLockedTwiceFails() throws Exception {
  26. try (Git git = new Git(db)) {
  27. writeTrashFile("file.txt", "content");
  28. git.add().addFilepattern("file.txt").call();
  29. RevCommit commit1 = git.commit().setMessage("create file").call();
  30. assertNotNull(commit1);
  31. writeTrashFile("file.txt", "content2");
  32. git.add().addFilepattern("file.txt").call();
  33. assertNotNull(git.commit().setMessage("edit file").call());
  34. LockFile lf = new LockFile(db.getIndexFile());
  35. assertTrue(lf.lock());
  36. try {
  37. String[] command = new String[] { "git", "checkout",
  38. commit1.name() };
  39. ProcessBuilder pb = new ProcessBuilder(command);
  40. pb.directory(db.getWorkTree());
  41. ExecutionResult result = FS.DETECTED.execute(pb, null);
  42. assertNotEquals(0, result.getRc());
  43. String err = result.getStderr().toString().split("\\R")[0];
  44. assertTrue(err.matches(
  45. "fatal: Unable to create .*/\\.git/index\\.lock': File exists\\."));
  46. } finally {
  47. lf.unlock();
  48. }
  49. }
  50. }
  51. }