forked from fsprojects/FAKE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInformation.fs
97 lines (77 loc) · 3.47 KB
/
Information.fs
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
[<AutoOpen>]
/// Contains helper functions which can be used to retrieve status information from git.
module Fake.Git.Information
open Fake
open System
open System.IO
/// Gets the git version
let getVersion repositoryDir =
let ok,msg,errors = runGitCommand repositoryDir "--version"
msg |> separated ""
let isVersionHigherOrEqual currentVersion referenceVersion =
parseVersion currentVersion >= parseVersion referenceVersion
let isGitVersionHigherOrEqual referenceVersion =
let currentVersion = getVersion "."
let versionParts = currentVersion.Replace("git version ","")
isVersionHigherOrEqual versionParts referenceVersion
/// Gets the git branch name
let getBranchName repositoryDir =
try
let ok,msg,errors = runGitCommand repositoryDir "status"
let s = msg |> Seq.head
let mutable replaceBranchString = "On branch "
let mutable replaceNoBranchString = "Not currently on any branch."
let noBranch = "NoBranch"
if isGitVersionHigherOrEqual "1.9" then replaceNoBranchString <- "HEAD detached"
if not <| isGitVersionHigherOrEqual "1.9" then replaceBranchString <- "# " + replaceBranchString
if startsWith replaceNoBranchString s then noBranch else s.Replace(replaceBranchString,"")
with _ when (repositoryDir = "" || repositoryDir = ".") && buildServer = TeamFoundation ->
match environVarOrNone "BUILD_SOURCEBRANCHNAME" with
| None -> reraise()
| Some s -> s
/// Returns the SHA1 of the current HEAD
let getCurrentSHA1 repositoryDir =
try
getSHA1 repositoryDir "HEAD"
with _ when (repositoryDir = "" || repositoryDir = ".") && buildServer = TeamFoundation ->
match environVarOrNone "BUILD_SOURCEVERSION" with
| None -> reraise()
| Some s -> s
/// Shows the git status
let showStatus repositoryDir = showGitCommand repositoryDir "status"
/// Checks if the working copy is clean
let isCleanWorkingCopy repositoryDir =
let ok,msg,errors = runGitCommand repositoryDir "status"
msg |> Seq.fold (fun acc s -> acc || "nothing to commit" <* s) false
/// Returns a friendly name from a SHA1
let showName repositoryDir sha1 =
let ok,msg,errors = runGitCommand repositoryDir <| sprintf "name-rev %s" sha1
if msg.Count = 0 then sha1 else msg.[0]
/// Returns true if rev1 is ahead of rev2
let isAheadOf repositoryDir rev1 rev2 =
if rev1 = rev2 then false else
findMergeBase repositoryDir rev1 rev2 = rev2
/// Gets the last git tag by calling git describe
let describe repositoryDir =
let _,msg,error = runGitCommand repositoryDir "describe"
if error <> "" then failwithf "git describe failed: %s" error
msg |> Seq.head
/// Gets the git log in one line
let shortlog repositoryDir =
let _,msg,error = runGitCommand repositoryDir "log --oneline -1"
if error <> "" then failwithf "git log --oneline failed: %s" error
msg |> Seq.head
/// Gets the last git tag of the current repository by calling git describe
let getLastTag() = (describe "").Split('-') |> Seq.head
/// Gets the current hash of the current repository
let getCurrentHash() =
try
let tmp =
(shortlog "").Split(' ')
|> Seq.head
|> fun s -> s.Split('m')
if tmp |> Array.length > 2 then tmp.[1].Substring(0,6) else tmp.[0].Substring(0,6)
with _ when buildServer = TeamFoundation ->
match environVarOrNone "BUILD_SOURCEVERSION" with
| None -> reraise()
| Some s -> s