Skip to content

Commit 44f16de

Browse files
committed
setFilesContent() implementation
1 parent ef26916 commit 44f16de

File tree

1 file changed

+5
-36
lines changed

1 file changed

+5
-36
lines changed

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

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,17 @@ public String getFileContent(String branchName, String fileRelativePath, String
331331
}
332332

333333
public VCSCommit setFilesContent(String branchName, List<String> filePathes, List<String> contents, String commitMessage) {
334+
if (filePathes.isEmpty()) {
335+
return null;
336+
}
334337
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
335338
Git git = getLocalGit(wc);
336339
Repository gitRepo = git.getRepository()) {
337340

338341
checkout(git, gitRepo, branchName, null);
339342
CommitCommand commitCommand = git.commit();
340343
int contentId = 0;
341-
for (int filePathId = 0; filePathId <= filePathes.size(); filePathId++) {
344+
for (int filePathId = 0; filePathId < filePathes.size(); filePathId++) {
342345
String filePath = filePathes.get(filePathId);
343346
File file = new File(wc.getFolder(), filePath);
344347
if (!file.exists()) {
@@ -373,41 +376,7 @@ public VCSCommit setFilesContent(String branchName, List<String> filePathes, Lis
373376

374377
@Override
375378
public VCSCommit setFileContent(String branchName, String filePath, String content, String commitMessage) {
376-
try (IVCSLockedWorkingCopy wc = repo.getVCSLockedWorkingCopy();
377-
Git git = getLocalGit(wc);
378-
Repository gitRepo = git.getRepository()) {
379-
380-
checkout(git, gitRepo, branchName, null);
381-
382-
File file = new File(wc.getFolder(), filePath);
383-
if (!file.exists()) {
384-
FileUtils.forceMkdir(file.getParentFile());
385-
file.createNewFile();
386-
git
387-
.add()
388-
.addFilepattern(filePath)
389-
.call();
390-
}
391-
392-
try (FileWriter fw = new FileWriter(file, false)) {
393-
fw.write(content);
394-
}
395-
396-
RevCommit newCommit = git
397-
.commit()
398-
.setOnly(filePath)
399-
.setMessage(commitMessage)
400-
.call();
401-
402-
String bn = getRealBranchName(branchName);
403-
RefSpec refSpec = new RefSpec(bn + ":" + bn);
404-
push(git, refSpec);
405-
return getVCSCommit(newCommit);
406-
} catch (GitAPIException e) {
407-
throw new EVCSException(e);
408-
} catch (Exception e) {
409-
throw new RuntimeException(e);
410-
}
379+
return setFilesContent(branchName, Collections.singletonList(filePath), Collections.singletonList(content), commitMessage);
411380
}
412381

413382
private void checkout(Git git, Repository gitRepo, String branchName, String revision) throws Exception {

0 commit comments

Comments
 (0)