Skip to content

Commit

Permalink
improved reading of files from git #20
Browse files Browse the repository at this point in the history
  • Loading branch information
manonthegithub committed Nov 16, 2023
1 parent 246178f commit ab9dc64
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/main/scala/org/dbpedia/databus/GitClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import java.util.concurrent.ConcurrentHashMap

import org.dbpedia.databus.RemoteGitlabHttpClient.{CreateFile, DeleteFile, FileAction, UpdateFile}
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.{Constants, Repository}
import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.treewalk.TreeWalk
import org.eclipse.jgit.treewalk.filter.PathFilter
import sttp.client3._
import sttp.model.Uri
import org.json4s._
Expand Down Expand Up @@ -56,7 +59,24 @@ class LocalGitClient(rootPath: Path) extends GitClient {

override def readFile(projectName: String, name: String): Try[Array[Byte]] =
wrapWithSync(projectName) {
Try(Files.readAllBytes(getFilePath(projectName, name)))
Try {
val repository = Git.open(getRepoPathFromUsername(projectName).toFile).getRepository
// find the HEAD
val lastCommitId = repository.resolve(Constants.HEAD)
// now we have to get the commit
val revWalk = new RevWalk(repository)
val commit = revWalk.parseCommit(lastCommitId)
// and using commit's tree find the path
val tree = commit.getTree
val treeWalk = new TreeWalk(repository)
treeWalk.addTree(tree)
treeWalk.setRecursive(true)
treeWalk.setFilter(PathFilter.create(getRelativeFilePath(name).toString))
treeWalk.next()
val objectId = treeWalk.getObjectId(0)
val loader = repository.open(objectId)
loader.getBytes
}
}

override def commitSeveralFiles(projectName: String, filenameAndData: Map[String, Array[Byte]]): Try[String] =
Expand All @@ -75,12 +95,11 @@ class LocalGitClient(rootPath: Path) extends GitClient {
}
add.call()

val commit = git.commit()
val ref = commit
git.commit()
.setCommitter("databus", "[email protected]")
.setMessage(s"$projectName: committed ${filenameAndData.keys}")
.call()
ref.getName
.getName
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/scala/org/dbpedia/databus/LocalGitTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ class LocalGitTest extends FlatSpec with Matchers with BeforeAndAfter {

val rer = cli.readFile(pn, "haha.txt")
rer.isSuccess should be(true)
rer.get should be("lol".getBytes())
val rer2 = cli.readFile(pn, "hahah.txt")
rer2.isSuccess should be(false)
val rer3 = cli.readFile(pn, "/loldata/lol.txt")
rer3.isSuccess should be(true)
rer3.get should be("haha".getBytes())

val files = Set(
"haha.txt",
Expand Down

0 comments on commit ab9dc64

Please sign in to comment.