Skip to content

Commit 5156ccc

Browse files
Preserve consistency file in non-merge regenerate
BUG=474560185 GWSQ_IGNORE: joshgoldman@google.com PiperOrigin-RevId: 871966509 Change-Id: Ia77dcd43203a56c02dc8d312a076ae62646ecd51
1 parent 71175f0 commit 5156ccc

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

java/com/google/copybara/regenerate/Regenerate.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,39 @@ public void regenerate() throws ValidationException, RepoException, IOException
186186
}
187187
}
188188

189+
if (workflow.getConsistencyFilePath() != null && !workflow.isConsistencyFileMergeImport()) {
190+
console.info("Preserving consistency file in workdir for non-merge regenerate...");
191+
Path workdirConsistencyFile = nextPath.resolve(workflow.getConsistencyFilePath());
192+
193+
// Only copy if it's not already in the workdir. There shouldn't be local changes to keep
194+
// track of in the non-merge regenerate case, but adding this check to be robust
195+
if (!Files.exists(workdirConsistencyFile)) {
196+
try {
197+
DestinationReader destReader =
198+
destinationWriter.getDestinationReader(console, regenTarget, workdir);
199+
200+
if (destReader.exists(workflow.getConsistencyFilePath())) {
201+
console.info("Consistency file found in destination, copying to workdir.");
202+
if (!Files.exists(workdirConsistencyFile.getParent())) {
203+
Files.createDirectories(workdirConsistencyFile.getParent());
204+
}
205+
String content = destReader.readFile(workflow.getConsistencyFilePath());
206+
Files.writeString(workdirConsistencyFile, content);
207+
console.info(
208+
"Copied consistency file from destination to workdir: " + workdirConsistencyFile);
209+
} else {
210+
console.warn(
211+
"Consistency file not found in destination: " + workflow.getConsistencyFilePath());
212+
}
213+
} catch (IOException | ValidationException | RepoException e) {
214+
throw new RepoException("Error preserving consistency file in workdir", e);
215+
}
216+
} else {
217+
console.info(
218+
"Consistency file already exists in workdir, not copying: " + workdirConsistencyFile);
219+
}
220+
}
221+
189222
if (autopatchConfig != null) {
190223
// generate new autopatch files in the target directory
191224
try {

javatests/com/google/copybara/regenerate/RegenerateCmdTest.java

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.copybara.regenerate;
1717

18+
import static com.google.common.base.Strings.nullToEmpty;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static com.google.copybara.testing.FileSubjects.assertThatPath;
2021
import static org.junit.Assert.assertThrows;
@@ -113,18 +114,19 @@ public DestinationReader getDestinationReader(
113114
return new DestinationReader() {
114115
@Override
115116
public String readFile(String path) throws RepoException {
116-
throw new RepoException("not implemented");
117-
}
118-
119-
@Override
120-
public void copyDestinationFiles(Object glob, Object path)
121-
throws RepoException, ValidationException {
122-
throw new RepoException("not implemented");
117+
String destinationSubdir = nullToEmpty(baseline);
118+
try {
119+
return Files.readString(
120+
destinationRoot.resolve(destinationSubdir).resolve(path));
121+
} catch (IOException e) {
122+
throw new RepoException("Error reading file in test", e);
123+
}
123124
}
124125

125126
@Override
126127
public void copyDestinationFilesToDirectory(Glob glob, Path directory) {
127-
Path sourceDirectory = destinationRoot.resolve(baseline);
128+
String destinationSubdir = nullToEmpty(baseline);
129+
Path sourceDirectory = destinationRoot.resolve(destinationSubdir);
128130
try {
129131
FileUtil.copyFilesRecursively(
130132
sourceDirectory,
@@ -138,7 +140,15 @@ public void copyDestinationFilesToDirectory(Glob glob, Path directory) {
138140

139141
@Override
140142
public boolean exists(String path) {
141-
return Files.exists(destinationRoot.resolve(baseline).resolve(path));
143+
// In this test, 'baseline' can be the destination subdir (e.g., "target")
144+
String destinationSubdir = nullToEmpty(baseline);
145+
return Files.exists(destinationRoot.resolve(destinationSubdir).resolve(path));
146+
}
147+
148+
@Override
149+
public void copyDestinationFiles(Object glob, Object path)
150+
throws RepoException, ValidationException {
151+
throw new RepoException("copyDestinationFiles(Object, Object) not implemented");
142152
}
143153
};
144154
}
@@ -936,4 +946,47 @@ private void writeDestination(String name, String filepath, byte[] bytes) throws
936946
private Path destinationPath(String name) {
937947
return destinationRoot.resolve(name);
938948
}
949+
950+
@Test
951+
public void testRegenerate_nonMergeImport_preservesConsistencyFile() throws Exception {
952+
setupFooOriginImport(); // This creates "foo.txt" in origin
953+
when(patchRegenerator.inferImportBaseline(any(), any()))
954+
.thenReturn(Optional.of(origin.getLatestChange().asString()));
955+
String destRef = "target";
956+
setupTarget(destRef);
957+
// Initial state with a consistency file
958+
String consistencyContent = "===BASELINE===";
959+
// Path relative to destination root for this test
960+
String consistencyFilePath = CONSISTENCY_FILE_PATH;
961+
writeDestination(destRef, consistencyFilePath, consistencyContent);
962+
// Simulate foo.txt being in the destination from a previous import
963+
writeDestination(destRef, "foo.txt", "foo");
964+
String config =
965+
"""
966+
core.workflow(
967+
name = 'default',
968+
origin = testing.origin(),
969+
origin_files = glob(['**']),
970+
destination = testing.destination(),
971+
mode = 'SQUASH',
972+
authoring = authoring.pass_thru('example <example@example.com>'),
973+
consistency_file_path = '%s'
974+
)
975+
"""
976+
.formatted(consistencyFilePath);
977+
RegenerateCmd cmd = getCmd(config);
978+
CommandEnv commandEnv =
979+
prepAndGetCommandEnv(ImmutableList.of(testRoot.resolve("copy.bara.sky").toString()), cmd);
980+
981+
ExitCode exitCode = cmd.run(commandEnv);
982+
983+
assertThat(exitCode).isEqualTo(ExitCode.SUCCESS);
984+
// Verify the consistency file was copied into the workdir passed to updateChange
985+
ArgumentCaptor<Path> pathArg = ArgumentCaptor.forClass(Path.class);
986+
verify(patchRegenerator).updateChange(any(), pathArg.capture(), any(), eq(destRef));
987+
// Assert content in the workdir
988+
assertThat(Files.readString(pathArg.getValue().resolve(consistencyFilePath)))
989+
.isEqualTo(consistencyContent);
990+
assertThat(Files.readString(pathArg.getValue().resolve("foo.txt"))).isEqualTo("foo");
991+
}
939992
}

0 commit comments

Comments
 (0)