Skip to content

Commit

Permalink
-- GitApi
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Sep 26, 2023
1 parent d252064 commit 3252a0f
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 943 deletions.
69 changes: 69 additions & 0 deletions ce3/src/main/scala/git_api/GitApi.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package git_api

import java.io.File
import java.time.{LocalDateTime, OffsetDateTime}
import sys.process.Process
import sys.process.ProcessBuilder

class Git(baseDir: File) {
import Git._

type P = ProcessBuilder
val cmdName = "git"

def exeName(command: String): String = {
val maybeOsName = sys.props.get("os.name").map(_.toLowerCase)
val maybeIsWindows = maybeOsName.filter(_.contains("windows"))
maybeIsWindows.map(_ => command + ".exe").getOrElse(command)
}

private lazy val exec = exeName(cmdName)

def cmd(args: Any*): P = Process(exec +: args.map(_.toString), baseDir)

def add(files: Seq[String]): P = cmd(("add" +: files): _*)

def add(file: String, files: String*): P = add(file +: files)

def revParse(name: String): String = cmd("rev-parse", name).!!.trim

def currentBranch: String = cmd("symbolic-ref", "HEAD").!!.trim.stripPrefix("refs/heads/")

def currentHash: String = revParse("HEAD")

def currentHash(len: Int): String = currentHash.take(len)


def commitList(): LazyList[Commit] =
cmd("log", "--pretty=format:%h : %an : %ae : %cI : %s", "--date=iso8601").lazyLines
.map(_.split(" : ").map(_.trim))
.collect { case Array(a, b, c, d, e) => Commit(a, b, c, OffsetDateTime.parse(d), e) }

def commitList(n: Int): LazyList[Commit] = commitList.take(n)

def tags: Iterable[String] = ???

def commit(message: String) = ???
}

object Git {

case class Commit(hash: String, user: String, email: String, date: OffsetDateTime, comment: String)

def isRepository(dir: File): Option[File] = Option.when(new File(dir, ".git").exists)(dir)
def detect(dir: File): Option[Git] = isRepository(dir).map(apply)
def detectUnsafe(dir: File): Git = detect(dir).getOrElse(sys.error("Git Repository expected"))

def apply(baseDir: File): Git = new Git(baseDir)
def apply(baseDir: String): Git = apply(new File(baseDir))
def apply(): Git = apply(".")
}

object GitApi extends App {
val git = Git()
println(git.currentBranch)
println(git.currentHash(8))
git.commitList(5)
.foreach(pprint.pprintln(_))

}
1 change: 0 additions & 1 deletion plain2/src/main/resources/1.json

This file was deleted.

4 changes: 0 additions & 4 deletions plain2/src/main/resources/2.json

This file was deleted.

264 changes: 0 additions & 264 deletions plain2/src/main/resources/jar_analyze/jars_oozie_lib.txt

This file was deleted.

137 changes: 0 additions & 137 deletions plain2/src/main/resources/jar_analyze/jars_oozie_libext.txt

This file was deleted.

Loading

0 comments on commit 3252a0f

Please sign in to comment.