Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d9267344f24a9ffec69e03520a9a1eab8168146 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (C) 2011, Stefan Lay <stefan.lay@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.egit.core.test.op;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.core.op.CloneOperation;
import org.eclipse.egit.core.op.CloneOperation.PostCloneTask;
import org.eclipse.egit.core.op.ConfigureFetchAfterCloneTask;
import org.eclipse.egit.core.op.ConfigurePushAfterCloneTask;
import org.eclipse.egit.core.test.DualRepositoryTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Before;
import org.junit.Test;

public class CloneOperationTest extends DualRepositoryTestCase {

	File workdir;

	File workdir2;

	@Before
	public void setUp() throws Exception {
		workdir = testUtils.createTempDir("Repository1");
		workdir2 = testUtils.createTempDir("Repository2");

		repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT));

		File file = new File(workdir, "file1.txt");
		FileUtils.createNewFile(file);
		try (Git git = new Git(repository1.getRepository())) {
			git.add().addFilepattern("file1.txt").call();

			git.commit().setMessage("first commit").call();
			git.tag().setName("tag").call();

			file = new File(workdir, "file2.txt");
			FileUtils.createNewFile(file);
			git.add().addFilepattern("file2.txt").call();
			git.commit().setMessage("second commit").call();
			git.branchCreate().setName("dev").call();

			file = new File(workdir, "file3.txt");
			FileUtils.createNewFile(file);
			git.add().addFilepattern("file3.txt").call();
			git.commit().setMessage("third commit").call();
		}
	}

	@Test
	public void testClone() throws Exception {
		String fullRefName = "refs/heads/master";
		cloneAndAssert(fullRefName);

		assertTrue(new File(workdir2, "file1.txt").exists());
		assertTrue(new File(workdir2, "file2.txt").exists());
		assertTrue(new File(workdir2, "file3.txt").exists());
	}

	@Test
	public void testCloneBranch() throws Exception {
		String branchName = "dev";
		cloneAndAssert(branchName);

		assertTrue(new File(workdir2, "file1.txt").exists());
		assertTrue(new File(workdir2, "file2.txt").exists());
		assertFalse(new File(workdir2, "file3.txt").exists());
	}

	@Test
	public void testCloneTag() throws Exception {
		String tagName = "tag";
		cloneAndAssert(tagName);

		assertTrue(new File(workdir2, "file1.txt").exists());
		assertFalse(new File(workdir2, "file2.txt").exists());
		assertFalse(new File(workdir2, "file3.txt").exists());
	}

	private void cloneAndAssert(String refName) throws Exception {
		URIish uri = new URIish("file:///"
				+ repository1.getRepository().getDirectory().toString());
		CloneOperation clop = new CloneOperation(uri, true, null, workdir2,
				refName, "origin", 0);
		clop.run(null);

		Repository clonedRepo = FileRepositoryBuilder.create(new File(workdir2,
				Constants.DOT_GIT));
		assertEquals(
				"",
				uri.toString(),
				clonedRepo.getConfig().getString(
						ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "url"));
		assertEquals(
				"",
				"+refs/heads/*:refs/remotes/origin/*",
				clonedRepo.getConfig().getString(
						ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
						"fetch"));
	}

	@Test
	public void testSimplePostCloneTask() throws Exception {
		URIish uri = new URIish("file:///"
				+ repository1.getRepository().getDirectory().toString());
		CloneOperation clop = new CloneOperation(uri, true, null, workdir2,
				"refs/heads/master", "origin", 0);

		final File[] repoDir = new File[1];
		clop.addPostCloneTask(new PostCloneTask() {

			@Override
			public void execute(Repository repository, IProgressMonitor monitor)
					throws CoreException {
				repoDir[0] = repository.getDirectory();

			}
		});
		clop.run(null);
		File newRepoDir = new File(workdir2, Constants.DOT_GIT);
		assertEquals(newRepoDir, repoDir[0]);
	}

	@Test
	public void testConfigurePushAfterCloneTask() throws Exception {
		URIish uri = new URIish("file:///"
				+ repository1.getRepository().getDirectory().toString());
		CloneOperation clop = new CloneOperation(uri, true, null, workdir2,
				"refs/heads/master", "origin", 0);

		clop.addPostCloneTask(new ConfigurePushAfterCloneTask("origin",
				"HEAD:refs/for/master", new URIish("file:///pushtarget")));
		clop.run(null);
		Repository clonedRepo = FileRepositoryBuilder.create(new File(workdir2,
				Constants.DOT_GIT));
		assertEquals(
				"",
				"HEAD:refs/for/master",
				clonedRepo.getConfig()
				.getString(ConfigConstants.CONFIG_REMOTE_SECTION,
						"origin", "push"));
		assertEquals(
				"",
				"file:///pushtarget",
				clonedRepo.getConfig().getString(
						ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
						"pushurl"));
	}

	@Test
	public void testConfigureFetchAfterCloneTask() throws Exception {
		createNoteInOrigin();

		URIish uri = new URIish("file:///"
				+ repository1.getRepository().getDirectory().toString());
		CloneOperation clop = new CloneOperation(uri, true, null, workdir2,
				"refs/heads/master", "origin", 0);

		clop.addPostCloneTask(new ConfigureFetchAfterCloneTask("origin",
				"refs/notes/review:refs/notes/review"));
		clop.run(null);
		Repository clonedRepo = FileRepositoryBuilder.create(new File(workdir2,
				Constants.DOT_GIT));
		assertTrue(
				clonedRepo.getConfig()
				.getStringList(ConfigConstants.CONFIG_REMOTE_SECTION,
						"origin", "fetch")[1].equals("refs/notes/review:refs/notes/review"));
		Git clonedGit = new Git(clonedRepo);
		assertEquals(1, clonedGit.notesList().setNotesRef("refs/notes/review").call().size());
		clonedGit.close();
	}

	protected void createNoteInOrigin() throws GitAPIException {
		try (Git git = new Git(repository1.getRepository())) {
			git.add().addFilepattern("file.txt").call();
			RevCommit commit = git.commit().setMessage("Initial commit").call();
			git.notesAdd().setNotesRef("refs/notes/review").setObjectId(commit)
					.setMessage("text").call();
		}
	}

}

Back to the top