Skip to content

Commit 7277260

Browse files
committed
setFileContent() using VCSChangeListNode
1 parent 3ee988b commit 7277260

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

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

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -331,54 +331,56 @@ public String getFileContent(String branchName, String fileRelativePath, String
331331
throw new RuntimeException(e);
332332
}
333333
}
334-
335-
public VCSCommit setFilesContent(String branchName, List<String> filePathes, List<String> contents, String commitMessage) {
336-
if (filePathes.isEmpty()) {
334+
335+
@Override
336+
public VCSCommit setFileContent(String branchName, List<VCSChangeListNode> vcsChangeList) {
337+
if (vcsChangeList.isEmpty()) {
337338
return null;
338339
}
339340
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
340341
Git git = getLocalGit(wc);
341342
Repository gitRepo = git.getRepository()) {
342343

343-
checkout(git, gitRepo, branchName, null);
344-
CommitCommand commitCommand = git.commit();
345-
int contentId = 0;
346-
for (int filePathId = 0; filePathId < filePathes.size(); filePathId++) {
347-
String filePath = filePathes.get(filePathId);
348-
File file = new File(wc.getFolder(), filePath);
349-
if (!file.exists()) {
350-
FileUtils.forceMkdir(file.getParentFile());
351-
file.createNewFile();
352-
git
353-
.add()
354-
.addFilepattern(filePath)
355-
.call();
356-
}
357-
358-
try (FileWriter fw = new FileWriter(file, false)) {
359-
fw.write(contents.get(contentId));
360-
}
361-
contentId++;
362-
commitCommand.setOnly(filePath);
344+
checkout(git, gitRepo, branchName, null);
345+
CommitCommand commitCommand = git.commit();
346+
StringBuilder commitMessageSB = new StringBuilder();
347+
for (VCSChangeListNode vcsChangeListNode : vcsChangeList) {
348+
String filePath = vcsChangeListNode.getFilePath();
349+
File file = new File(wc.getFolder(), filePath);
350+
if (!file.exists()) {
351+
FileUtils.forceMkdir(file.getParentFile());
352+
file.createNewFile();
353+
git
354+
.add()
355+
.addFilepattern(filePath)
356+
.call();
363357
}
364-
RevCommit newCommit = commitCommand
365-
.setMessage(commitMessage)
366-
.call();
367358

368-
String bn = getRealBranchName(branchName);
369-
RefSpec refSpec = new RefSpec(bn + ":" + bn);
370-
push(git, refSpec);
371-
return getVCSCommit(newCommit);
372-
} catch (GitAPIException e) {
373-
throw new EVCSException(e);
374-
} catch (Exception e) {
375-
throw new RuntimeException(e);
359+
try (FileWriter fw = new FileWriter(file, false)) {
360+
fw.write(vcsChangeListNode.getContent());
361+
}
362+
commitCommand.setOnly(filePath);
363+
commitMessageSB.append(vcsChangeListNode.getLogMessage() + VCSChangeListNode.COMMIT_MESSAGES_SEPARATOR);
376364
}
365+
commitMessageSB.setLength(commitMessageSB.length() - VCSChangeListNode.COMMIT_MESSAGES_SEPARATOR.length());
366+
RevCommit newCommit = commitCommand
367+
.setMessage(commitMessageSB.toString())
368+
.call();
369+
370+
String bn = getRealBranchName(branchName);
371+
RefSpec refSpec = new RefSpec(bn + ":" + bn);
372+
push(git, refSpec);
373+
return getVCSCommit(newCommit);
374+
} catch (GitAPIException e) {
375+
throw new EVCSException(e);
376+
} catch (Exception e) {
377+
throw new RuntimeException(e);
378+
}
377379
}
378380

379381
@Override
380382
public VCSCommit setFileContent(String branchName, String filePath, String content, String commitMessage) {
381-
return setFilesContent(branchName, Collections.singletonList(filePath), Collections.singletonList(content), commitMessage);
383+
return setFileContent(branchName, Collections.singletonList(new VCSChangeListNode(filePath, content, commitMessage)));
382384
}
383385

384386
private void checkout(Git git, Repository gitRepo, String branchName, String revision) throws Exception {
@@ -389,7 +391,6 @@ private void checkout(Git git, Repository gitRepo, String branchName, String rev
389391
.setCredentialsProvider(credentials)
390392
.call();
391393
if (revision == null) {
392-
393394
cmd
394395
.setStartPoint("origin/" + bn)
395396
.setCreateBranch(gitRepo.exactRef("refs/heads/" + bn) == null)

0 commit comments

Comments
 (0)