|
| 1 | +/* |
| 2 | + * Copyright (C) 2024, Broadcom 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.api; |
| 11 | + |
| 12 | +import static org.eclipse.jgit.lib.Constants.HEAD; |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertNotNull; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +import java.io.ByteArrayInputStream; |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Iterator; |
| 23 | + |
| 24 | +import org.eclipse.jgit.internal.storage.file.FileRepository; |
| 25 | +import org.eclipse.jgit.junit.JGitTestUtil; |
| 26 | +import org.eclipse.jgit.junit.RepositoryTestCase; |
| 27 | +import org.eclipse.jgit.lib.ObjectId; |
| 28 | +import org.eclipse.jgit.lib.ReflogEntry; |
| 29 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 30 | +import org.eclipse.jgit.util.FS; |
| 31 | +import org.eclipse.jgit.util.FS.ExecutionResult; |
| 32 | +import org.eclipse.jgit.util.RawParseUtils; |
| 33 | +import org.eclipse.jgit.util.TemporaryBuffer; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +public class LinkedWorktreeTest extends RepositoryTestCase { |
| 37 | + |
| 38 | + @Override |
| 39 | + public void setUp() throws Exception { |
| 40 | + super.setUp(); |
| 41 | + |
| 42 | + try (Git git = new Git(db)) { |
| 43 | + git.commit().setMessage("Initial commit").call(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testWeCanReadFromLinkedWorktreeFromBare() throws Exception { |
| 49 | + FS fs = db.getFS(); |
| 50 | + File directory = trash.getParentFile(); |
| 51 | + String dbDirName = db.getWorkTree().getName(); |
| 52 | + cloneBare(fs, directory, dbDirName, "bare"); |
| 53 | + File bareDirectory = new File(directory, "bare"); |
| 54 | + worktreeAddExisting(fs, bareDirectory, "master"); |
| 55 | + |
| 56 | + File worktreesDir = new File(bareDirectory, "worktrees"); |
| 57 | + File masterWorktreesDir = new File(worktreesDir, "master"); |
| 58 | + |
| 59 | + FileRepository repository = new FileRepository(masterWorktreesDir); |
| 60 | + try (Git git = new Git(repository)) { |
| 61 | + ObjectId objectId = repository.resolve(HEAD); |
| 62 | + assertNotNull(objectId); |
| 63 | + |
| 64 | + Iterator<RevCommit> log = git.log().all().call().iterator(); |
| 65 | + assertTrue(log.hasNext()); |
| 66 | + assertTrue("Initial commit".equals(log.next().getShortMessage())); |
| 67 | + |
| 68 | + // we have reflog entry |
| 69 | + // depending on git version we either have one or |
| 70 | + // two entries where extra is zeroid entry with |
| 71 | + // same message or no message |
| 72 | + Collection<ReflogEntry> reflog = git.reflog().call(); |
| 73 | + assertNotNull(reflog); |
| 74 | + assertTrue(reflog.size() > 0); |
| 75 | + ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]); |
| 76 | + assertEquals(reflogs[reflogs.length - 1].getComment(), |
| 77 | + "reset: moving to HEAD"); |
| 78 | + |
| 79 | + // index works with file changes |
| 80 | + File masterDir = new File(directory, "master"); |
| 81 | + File testFile = new File(masterDir, "test"); |
| 82 | + |
| 83 | + Status status = git.status().call(); |
| 84 | + assertTrue(status.getUncommittedChanges().size() == 0); |
| 85 | + assertTrue(status.getUntracked().size() == 0); |
| 86 | + |
| 87 | + JGitTestUtil.write(testFile, "test"); |
| 88 | + status = git.status().call(); |
| 89 | + assertTrue(status.getUncommittedChanges().size() == 0); |
| 90 | + assertTrue(status.getUntracked().size() == 1); |
| 91 | + |
| 92 | + git.add().addFilepattern("test").call(); |
| 93 | + status = git.status().call(); |
| 94 | + assertTrue(status.getUncommittedChanges().size() == 1); |
| 95 | + assertTrue(status.getUntracked().size() == 0); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testWeCanReadFromLinkedWorktreeFromNonBare() throws Exception { |
| 101 | + FS fs = db.getFS(); |
| 102 | + worktreeAddNew(fs, db.getWorkTree(), "wt"); |
| 103 | + |
| 104 | + File worktreesDir = new File(db.getDirectory(), "worktrees"); |
| 105 | + File masterWorktreesDir = new File(worktreesDir, "wt"); |
| 106 | + |
| 107 | + FileRepository repository = new FileRepository(masterWorktreesDir); |
| 108 | + try (Git git = new Git(repository)) { |
| 109 | + ObjectId objectId = repository.resolve(HEAD); |
| 110 | + assertNotNull(objectId); |
| 111 | + |
| 112 | + Iterator<RevCommit> log = git.log().all().call().iterator(); |
| 113 | + assertTrue(log.hasNext()); |
| 114 | + assertTrue("Initial commit".equals(log.next().getShortMessage())); |
| 115 | + |
| 116 | + // we have reflog entry |
| 117 | + Collection<ReflogEntry> reflog = git.reflog().call(); |
| 118 | + assertNotNull(reflog); |
| 119 | + assertTrue(reflog.size() > 0); |
| 120 | + ReflogEntry[] reflogs = reflog.toArray(new ReflogEntry[0]); |
| 121 | + assertEquals(reflogs[reflogs.length - 1].getComment(), |
| 122 | + "reset: moving to HEAD"); |
| 123 | + |
| 124 | + // index works with file changes |
| 125 | + File directory = trash.getParentFile(); |
| 126 | + File wtDir = new File(directory, "wt"); |
| 127 | + File testFile = new File(wtDir, "test"); |
| 128 | + |
| 129 | + Status status = git.status().call(); |
| 130 | + assertTrue(status.getUncommittedChanges().size() == 0); |
| 131 | + assertTrue(status.getUntracked().size() == 0); |
| 132 | + |
| 133 | + JGitTestUtil.write(testFile, "test"); |
| 134 | + status = git.status().call(); |
| 135 | + assertTrue(status.getUncommittedChanges().size() == 0); |
| 136 | + assertTrue(status.getUntracked().size() == 1); |
| 137 | + |
| 138 | + git.add().addFilepattern("test").call(); |
| 139 | + status = git.status().call(); |
| 140 | + assertTrue(status.getUncommittedChanges().size() == 1); |
| 141 | + assertTrue(status.getUntracked().size() == 0); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + private static void cloneBare(FS fs, File directory, String from, String to) throws IOException, InterruptedException { |
| 147 | + ProcessBuilder builder = fs.runInShell("git", |
| 148 | + new String[] { "clone", "--bare", from, to }); |
| 149 | + builder.directory(directory); |
| 150 | + builder.environment().put("HOME", fs.userHome().getAbsolutePath()); |
| 151 | + StringBuilder input = new StringBuilder(); |
| 152 | + ExecutionResult result = fs.execute(builder, new ByteArrayInputStream( |
| 153 | + input.toString().getBytes(StandardCharsets.UTF_8))); |
| 154 | + String stdOut = toString(result.getStdout()); |
| 155 | + String errorOut = toString(result.getStderr()); |
| 156 | + assertNotNull(stdOut); |
| 157 | + assertNotNull(errorOut); |
| 158 | + } |
| 159 | + |
| 160 | + private static void worktreeAddExisting(FS fs, File directory, String name) throws IOException, InterruptedException { |
| 161 | + ProcessBuilder builder = fs.runInShell("git", |
| 162 | + new String[] { "worktree", "add", "../" + name, name }); |
| 163 | + builder.directory(directory); |
| 164 | + builder.environment().put("HOME", fs.userHome().getAbsolutePath()); |
| 165 | + StringBuilder input = new StringBuilder(); |
| 166 | + ExecutionResult result = fs.execute(builder, new ByteArrayInputStream( |
| 167 | + input.toString().getBytes(StandardCharsets.UTF_8))); |
| 168 | + String stdOut = toString(result.getStdout()); |
| 169 | + String errorOut = toString(result.getStderr()); |
| 170 | + assertNotNull(stdOut); |
| 171 | + assertNotNull(errorOut); |
| 172 | + } |
| 173 | + |
| 174 | + private static void worktreeAddNew(FS fs, File directory, String name) throws IOException, InterruptedException { |
| 175 | + ProcessBuilder builder = fs.runInShell("git", |
| 176 | + new String[] { "worktree", "add", "-b", name, "../" + name, "master"}); |
| 177 | + builder.directory(directory); |
| 178 | + builder.environment().put("HOME", fs.userHome().getAbsolutePath()); |
| 179 | + StringBuilder input = new StringBuilder(); |
| 180 | + ExecutionResult result = fs.execute(builder, new ByteArrayInputStream( |
| 181 | + input.toString().getBytes(StandardCharsets.UTF_8))); |
| 182 | + String stdOut = toString(result.getStdout()); |
| 183 | + String errorOut = toString(result.getStderr()); |
| 184 | + assertNotNull(stdOut); |
| 185 | + assertNotNull(errorOut); |
| 186 | + } |
| 187 | + |
| 188 | + private static String toString(TemporaryBuffer b) throws IOException { |
| 189 | + return RawParseUtils.decode(b.toByteArray()); |
| 190 | + } |
| 191 | + |
| 192 | +} |