View Javadoc
1   /*
2    * Copyright (C) 2013, Chris Aniszczyk <zx@twitter.com> and others. 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.pgm;
11  
12  import static org.junit.Assert.assertEquals;
13  
14  import org.eclipse.jgit.api.Git;
15  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
16  import org.eclipse.jgit.lib.Repository;
17  import org.eclipse.jgit.lib.StoredConfig;
18  import org.eclipse.jgit.transport.RemoteConfig;
19  import org.eclipse.jgit.transport.URIish;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  public class FetchTest extends CLIRepositoryTestCase {
24  	private Git git;
25  
26  	private Git remoteGit;
27  
28  	@Override
29  	@Before
30  	public void setUp() throws Exception {
31  		super.setUp();
32  		git = new Git(db);
33  		git.commit().setMessage("initial commit").call();
34  
35  		Repository remoteRepository = createWorkRepository();
36  		addRepoToClose(remoteRepository);
37  		remoteGit = new Git(remoteRepository);
38  
39  		// setup the first repository to fetch from the second repository
40  		final StoredConfig config = db.getConfig();
41  		RemoteConfig remoteConfig = new RemoteConfig(config, "test");
42  		URIish uri = new URIish(remoteRepository.getDirectory().toURI().toURL());
43  		remoteConfig.addURI(uri);
44  		remoteConfig.update(config);
45  		config.save();
46  
47  		remoteGit.commit().setMessage("initial commit").call();
48  		remoteGit.tag().setName("tag").call();
49  		remoteGit.checkout().setName("other").setCreateBranch(true).call();
50  		remoteGit.commit().setMessage("commit2").call();
51  		remoteGit.tag().setName("foo").call();
52  	}
53  
54  	@Test
55  	public void testFetchDefault() throws Exception {
56  		String[] result = execute("git fetch test refs/heads/master:refs/remotes/origin/master");
57  		assertEquals(" * [new branch]      master     -> origin/master",
58  				result[1]);
59  		assertEquals(" * [new tag]         tag        -> tag", result[2]);
60  	}
61  
62  	@Test
63  	public void testFetchForceUpdate() throws Exception {
64  		String[] result = execute(
65  				"git fetch test refs/heads/master:refs/remotes/origin/master");
66  		assertEquals(" * [new branch]      master     -> origin/master",
67  				result[1]);
68  		assertEquals(" * [new tag]         tag        -> tag", result[2]);
69  		remoteGit.commit().setAmend(true).setMessage("amended").call();
70  		result = execute(
71  				"git fetch -f test refs/heads/master:refs/remotes/origin/master");
72  		assertEquals("", result[0]);
73  	}
74  
75  	@Test
76  	public void testFetchNoTags() throws Exception {
77  		String[] result = execute("git fetch --no-tags test refs/heads/master:refs/remotes/origin/master");
78  		assertEquals(" * [new branch]      master     -> origin/master",
79  				result[1]);
80  		assertEquals("", result[2]);
81  	}
82  
83  	@Test
84  	public void testFetchAllTags() throws Exception {
85  		String[] result = execute("git fetch --tags test refs/heads/master:refs/remotes/origin/master");
86  		assertEquals(" * [new branch]      master     -> origin/master",
87  				result[1]);
88  		assertEquals(" * [new tag]         foo        -> foo", result[2]);
89  		assertEquals(" * [new tag]         tag        -> tag", result[3]);
90  	}
91  }