View Javadoc
1   /*
2    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> 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.junit.Assert.assertNotNull;
13  import static org.junit.Assert.assertNull;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.File;
17  import java.io.IOException;
18  
19  import org.eclipse.jgit.api.errors.GitAPIException;
20  import org.eclipse.jgit.api.errors.JGitInternalException;
21  import org.eclipse.jgit.errors.NoWorkTreeException;
22  import org.eclipse.jgit.junit.MockSystemReader;
23  import org.eclipse.jgit.junit.RepositoryTestCase;
24  import org.eclipse.jgit.lib.Constants;
25  import org.eclipse.jgit.lib.Repository;
26  import org.eclipse.jgit.util.SystemReader;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  public class InitCommandTest extends RepositoryTestCase {
31  
32  	@Override
33  	@Before
34  	public void setUp() throws Exception {
35  		super.setUp();
36  	}
37  
38  	@Test
39  	public void testInitRepository()
40  			throws IOException, JGitInternalException, GitAPIException {
41  		File directory = createTempDirectory("testInitRepository");
42  		InitCommand command = new InitCommand();
43  		command.setDirectory(directory);
44  		try (Git git = command.call()) {
45  			assertNotNull(git.getRepository());
46  		}
47  	}
48  
49  	@Test
50  	public void testInitNonEmptyRepository() throws IOException,
51  			JGitInternalException, GitAPIException {
52  		File directory = createTempDirectory("testInitRepository2");
53  		File someFile = new File(directory, "someFile");
54  		someFile.createNewFile();
55  		assertTrue(someFile.exists());
56  		assertTrue(directory.listFiles().length > 0);
57  		InitCommand command = new InitCommand();
58  		command.setDirectory(directory);
59  		try (Git git = command.call()) {
60  			assertNotNull(git.getRepository());
61  		}
62  	}
63  
64  	@Test
65  	public void testInitBareRepository() throws IOException,
66  			JGitInternalException, GitAPIException {
67  		File directory = createTempDirectory("testInitBareRepository");
68  		InitCommand command = new InitCommand();
69  		command.setDirectory(directory);
70  		command.setBare(true);
71  		try (Git git = command.call()) {
72  			Repository repository = git.getRepository();
73  			assertNotNull(repository);
74  			assertTrue(repository.isBare());
75  		}
76  	}
77  
78  	// non-bare repos where gitDir and directory is set. Same as
79  	// "git init --separate-git-dir /tmp/a /tmp/b"
80  	@Test
81  	public void testInitWithExplicitGitDir() throws IOException,
82  			JGitInternalException, GitAPIException {
83  		File wt = createTempDirectory("testInitRepositoryWT");
84  		File gitDir = createTempDirectory("testInitRepositoryGIT");
85  		InitCommand command = new InitCommand();
86  		command.setDirectory(wt);
87  		command.setGitDir(gitDir);
88  		try (Git git = command.call()) {
89  			Repository repository = git.getRepository();
90  			assertNotNull(repository);
91  			assertEqualsFile(wt, repository.getWorkTree());
92  			assertEqualsFile(gitDir, repository.getDirectory());
93  		}
94  	}
95  
96  	// non-bare repos where only gitDir is set. Same as
97  	// "git init --separate-git-dir /tmp/a"
98  	@Test
99  	public void testInitWithOnlyExplicitGitDir() throws IOException,
100 			JGitInternalException, GitAPIException {
101 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
102 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
103 				.getAbsolutePath());
104 		File gitDir = createTempDirectory("testInitRepository/.git");
105 		InitCommand command = new InitCommand();
106 		command.setGitDir(gitDir);
107 		try (Git git = command.call()) {
108 			Repository repository = git.getRepository();
109 			assertNotNull(repository);
110 			assertEqualsFile(gitDir, repository.getDirectory());
111 			assertEqualsFile(new File(reader.getProperty("user.dir")),
112 					repository.getWorkTree());
113 		}
114 	}
115 
116 	// Bare repos where gitDir and directory is set will only work if gitDir and
117 	// directory is pointing to same dir. Same as
118 	// "git init --bare --separate-git-dir /tmp/a /tmp/b"
119 	// (works in native git but I guess that's more a bug)
120 	@Test(expected = IllegalStateException.class)
121 	public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
122 			JGitInternalException, GitAPIException {
123 		File gitDir = createTempDirectory("testInitRepository.git");
124 		InitCommand command = new InitCommand();
125 		command.setBare(true);
126 		command.setDirectory(gitDir);
127 		command.setGitDir(new File(gitDir, ".."));
128 		command.call();
129 	}
130 
131 	// If neither directory nor gitDir is set in a non-bare repo make sure
132 	// worktree and gitDir are set correctly. Standard case. Same as
133 	// "git init"
134 	@Test
135 	public void testInitWithDefaultsNonBare() throws JGitInternalException,
136 			GitAPIException, IOException {
137 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
138 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
139 				.getAbsolutePath());
140 		InitCommand command = new InitCommand();
141 		command.setBare(false);
142 		try (Git git = command.call()) {
143 			Repository repository = git.getRepository();
144 			assertNotNull(repository);
145 			assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
146 					repository.getDirectory());
147 			assertEqualsFile(new File(reader.getProperty("user.dir")),
148 					repository.getWorkTree());
149 		}
150 	}
151 
152 	// If neither directory nor gitDir is set in a bare repo make sure
153 	// worktree and gitDir are set correctly. Standard case. Same as
154 	// "git init --bare"
155 	@Test(expected = NoWorkTreeException.class)
156 	public void testInitWithDefaultsBare() throws JGitInternalException,
157 			GitAPIException, IOException {
158 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
159 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
160 				.getAbsolutePath());
161 		InitCommand command = new InitCommand();
162 		command.setBare(true);
163 		try (Git git = command.call()) {
164 			Repository repository = git.getRepository();
165 			assertNotNull(repository);
166 			assertEqualsFile(new File(reader.getProperty("user.dir")),
167 					repository.getDirectory());
168 			assertNull(repository.getWorkTree());
169 		}
170 	}
171 
172 	// In a non-bare repo when directory and gitDir is set then they shouldn't
173 	// point to the same dir. Same as
174 	// "git init --separate-git-dir /tmp/a /tmp/a"
175 	// (works in native git but I guess that's more a bug)
176 	@Test(expected = IllegalStateException.class)
177 	public void testInitNonBare_GitdirAndDirShouldntBeSame()
178 			throws JGitInternalException, GitAPIException, IOException {
179 		File gitDir = createTempDirectory("testInitRepository.git");
180 		InitCommand command = new InitCommand();
181 		command.setBare(false);
182 		command.setGitDir(gitDir);
183 		command.setDirectory(gitDir);
184 		command.call().getRepository();
185 	}
186 }