Skip to content

Commit 9f6bdbd

Browse files
committed
implementing setFilesContent
1 parent 310aff4 commit 9f6bdbd

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/main/java/org/scm4j/vcs/GitVCS.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,47 @@ public String getFileContent(String branchName, String fileRelativePath, String
328328
throw new RuntimeException(e);
329329
}
330330
}
331+
332+
public VCSCommit setFilesContent(String branchName, List<String> filePathes, List<String> contents, String commitMessage) {
333+
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
334+
Git git = getLocalGit(wc);
335+
Repository gitRepo = git.getRepository()) {
336+
337+
checkout(git, gitRepo, branchName, null);
338+
CommitCommand commitCommand = git.commit();
339+
int contentId = 0;
340+
for (int filePathId = 0; filePathId <= filePathes.size(); filePathId++) {
341+
String filePath = filePathes.get(filePathId);
342+
File file = new File(wc.getFolder(), filePath);
343+
if (!file.exists()) {
344+
FileUtils.forceMkdir(file.getParentFile());
345+
file.createNewFile();
346+
git
347+
.add()
348+
.addFilepattern(filePath)
349+
.call();
350+
}
351+
352+
try (FileWriter fw = new FileWriter(file, false)) {
353+
fw.write(contents.get(contentId));
354+
}
355+
contentId++;
356+
commitCommand.setOnly(filePath);
357+
}
358+
RevCommit newCommit = commitCommand
359+
.setMessage(commitMessage)
360+
.call();
361+
362+
String bn = getRealBranchName(branchName);
363+
RefSpec refSpec = new RefSpec(bn + ":" + bn);
364+
push(git, refSpec);
365+
return getVCSCommit(newCommit);
366+
} catch (GitAPIException e) {
367+
throw new EVCSException(e);
368+
} catch (Exception e) {
369+
throw new RuntimeException(e);
370+
}
371+
}
331372

332373
@Override
333374
public VCSCommit setFileContent(String branchName, String filePath, String content, String commitMessage) {

0 commit comments

Comments
 (0)