forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitWrite.scala
121 lines (93 loc) · 3.45 KB
/
GitWrite.scala
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
package lila.i18n
import java.io.File
import scala.collection.JavaConversions._
import scala.concurrent.Future
import akka.actor._
import akka.pattern.ask
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import makeTimeout.veryLarge
private[i18n] final class GitWrite(
transRelPath: String,
repoPath: String,
system: ActorSystem) {
private val repo = (new FileRepositoryBuilder())
.setGitDir(new File(repoPath + "/.git"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build()
private val git = new Git(repo, debug = true)
def apply(translations: List[Translation]): Funit =
fuloginfo("Working on " + repoPath) >>
git.currentBranch flatMap { currentBranch =>
loginfo("Current branch is " + currentBranch)
(translations map gitActor.?).sequenceFu >>
(gitActor ? currentBranch mapTo manifest[Unit])
}
private lazy val gitActor = system.actorOf(Props(new Actor {
def receive = {
case branch: String => {
loginfo("Checkout " + branch)
git checkout branch
sender ! ()
}
case translation: Translation => {
val branch = "t/" + translation.id
val code = translation.code
val name = (LangList name code) err "Lang does not exist: " + code
val commitMsg = commitMessage(translation, name)
sender ! (git branchExists branch flatMap {
_.fold(
fuloginfo("! Branch already exists: " + branch),
git.checkout(branch, true) >>
writeMessages(translation) >>
fuloginfo("Add " + relFileOf(translation)) >>
(git add relFileOf(translation)) >>
fuloginfo("- " + commitMsg) >>
(git commit commitMsg).void
)
}).await
}
}
}))
private def writeMessages(translation: Translation) =
fuloginfo("Write messages to " + absFileOf(translation)) >>
printToFile(absFileOf(translation)) { writer =>
translation.lines foreach writer.println
}
private def relFileOf(translation: Translation) =
"%s/messages.%s".format(transRelPath, translation.code)
private def absFileOf(translation: Translation) =
repoPath + "/" + relFileOf(translation)
private def commitMessage(translation: Translation, name: String) =
"""%s "%s" translation #%d. Author: %s. %s""".format(
translation.code,
name,
translation.id,
translation.author | "Anonymous",
translation.comment | "")
final class Git(repo: Repository, debug: Boolean = false) {
import org.eclipse.jgit.api._
val api = new org.eclipse.jgit.api.Git(repo)
def currentBranch: Fu[String] = Future {
cleanupBranch(repo.getFullBranch)
}
def branchList = Future {
api.branchList.call map (_.getName) map cleanupBranch
}
def branchExists(branch: String) =
branchList map (_ contains branch)
def checkout(branch: String, create: Boolean = false) = Future {
api.checkout.setName(branch).setCreateBranch(create).call
}
def add(pattern: String) = Future {
api.add.addFilepattern(pattern).call
}
def commit(message: String) = Future {
api.commit.setMessage(message).call
}
private def cleanupBranch(branch: String) =
branch.replace("refs/heads/", "")
private def log(msg: => Any) = debug.fold(fuloginfo(msg.toString), funit)
}
}