aboutsummaryrefslogtreecommitdiff
path: root/lib/bisect.nim
blob: ea4063ca81def3c112cc7d441cd327afe2ac00cf (plain) (blame)
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
import future, osproc, strutils

{.passL: "-lalpm".}

proc vercmp(a: cstring, b: cstring): cint
  {.cdecl, importc: "alpm_pkg_vercmp".}

proc getSourceVersion(relativePath: string): seq[string] =
  let lines = execProcess("/bin/bash", ["-c",
    """source "$1/PKGBUILD" && echo "$epoch" && echo "$pkgver" && echo "$pkgrel"""",
    "bash", relativePath], options = {}).split("\n")
  if lines.len == 4:
    lines[0 .. 2]
  else:
    @[]

proc handleBisect*(params: seq[string]): int =
  let compareMethod = params[0]
  let relativePath = params[1]
  let compareVersion = params[2]

  let (currentVersion, supported) = if compareMethod == "source":
      (getSourceVersion(relativePath), true)
    else:
      (@[], false)

  if not supported:
    255
  elif currentVersion.len != 3:
    125
  else:
    let epoch = currentVersion[0].strip
    let pkgver = currentVersion[1].strip
    let pkgrel = currentVersion[2].strip

    if pkgver.len == 0 or pkgrel.len == 0:
      125
    else:
      let version = if epoch.len > 0:
          epoch & ":" & pkgver & "-" & pkgrel
        else:
          pkgver & "-" & pkgrel

      # this output is required by main program
      echo(version)
      if vercmp(compareVersion, version) > 0: 0 else: 1