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
|
/*
* Copyright (C) 2021, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.gitrepo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jgit.gitrepo.BareSuperprojectWriter.BareWriterConfig;
import org.eclipse.jgit.gitrepo.RepoCommand.RemoteReader;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
public class BareSuperprojectWriterTest extends RepositoryTestCase {
private static final String SHA1_A = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
@Override
public void setUp() throws Exception {
super.setUp();
}
@Test
public void write_setGitModulesContents() throws Exception {
try (Repository bareRepo = createBareRepository()) {
RepoProject repoProject = new RepoProject("subprojectX", "path/to",
"refs/heads/branch-x", "remote", "");
repoProject.setUrl("http://example.com/a");
RemoteReader mockRemoteReader = mock(RemoteReader.class);
when(mockRemoteReader.sha1("http://example.com/a",
"refs/heads/branch-x"))
.thenReturn(ObjectId.fromString(SHA1_A));
BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo,
null, "refs/heads/master", author, mockRemoteReader,
BareWriterConfig.getDefault(), List.of());
RevCommit commit = w.write(Arrays.asList(repoProject));
String contents = readContents(bareRepo, commit, ".gitmodules");
List<String> contentLines = Arrays
.asList(contents.split("\n"));
assertThat(contentLines.get(0),
is("[submodule \"subprojectX\"]"));
assertThat(contentLines.subList(1, contentLines.size()),
containsInAnyOrder(is("\tbranch = refs/heads/branch-x"),
is("\tpath = path/to"),
is("\turl = http://example.com/a")));
}
}
@Test
public void write_setGitModulesContents_pinned() throws Exception {
try (Repository bareRepo = createBareRepository()) {
RepoProject pinWithUpstream = new RepoProject("pinWithUpstream",
"path/x", "cbc0fae7e1911d27e1de37d364698dba4411c78b",
"remote", "");
pinWithUpstream.setUrl("http://example.com/a");
pinWithUpstream.setUpstream("branchX");
RepoProject pinWithoutUpstream = new RepoProject(
"pinWithoutUpstream", "path/y",
"cbc0fae7e1911d27e1de37d364698dba4411c78b", "remote", "");
pinWithoutUpstream.setUrl("http://example.com/b");
RemoteReader mockRemoteReader = mock(RemoteReader.class);
BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo,
null, "refs/heads/master", author, mockRemoteReader,
BareWriterConfig.getDefault(), List.of());
RevCommit commit = w
.write(Arrays.asList(pinWithUpstream, pinWithoutUpstream));
String contents = readContents(bareRepo, commit, ".gitmodules");
Config cfg = new Config();
cfg.fromText(contents);
assertThat(cfg.getString("submodule", "pinWithUpstream", "path"),
is("path/x"));
assertThat(cfg.getString("submodule", "pinWithUpstream", "url"),
is("http://example.com/a"));
assertThat(cfg.getString("submodule", "pinWithUpstream", "ref"),
is("branchX"));
assertThat(cfg.getString("submodule", "pinWithoutUpstream", "path"),
is("path/y"));
assertThat(cfg.getString("submodule", "pinWithoutUpstream", "url"),
is("http://example.com/b"));
assertThat(cfg.getString("submodule", "pinWithoutUpstream", "ref"),
nullValue());
}
}
@Test
public void write_setExtraContents() throws Exception {
try (Repository bareRepo = createBareRepository()) {
RepoProject repoProject = new RepoProject("subprojectX", "path/to",
"refs/heads/branch-x", "remote", "");
repoProject.setUrl("http://example.com/a");
RemoteReader mockRemoteReader = mock(RemoteReader.class);
when(mockRemoteReader.sha1("http://example.com/a",
"refs/heads/branch-x"))
.thenReturn(ObjectId.fromString(SHA1_A));
BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo,
null, "refs/heads/master", author, mockRemoteReader,
BareWriterConfig.getDefault(),
List.of(new BareSuperprojectWriter.ExtraContent("x",
"extra-content")));
RevCommit commit = w.write(Arrays.asList(repoProject));
String contents = readContents(bareRepo, commit, "x");
assertThat(contents, is("extra-content"));
}
}
private String readContents(Repository repo, RevCommit commit,
String path) throws Exception {
String idStr = commit.getId().name() + ":" + path;
ObjectId modId = repo.resolve(idStr);
try (ObjectReader reader = repo.newObjectReader()) {
return new String(
reader.open(modId).getCachedBytes(Integer.MAX_VALUE),
StandardCharsets.UTF_8);
}
}
}
|