A bit of professionalisation (#1)

This commit is contained in:
Patrick Stevens
2022-09-02 22:56:06 +01:00
committed by GitHub
parent e533ab1bc5
commit 7a593dd97a
29 changed files with 662 additions and 385 deletions

View File

@@ -14,6 +14,7 @@ type SymbolicRef =
| OrigHead
// TODO - determine how an arbitrary symbolicref actually behaves
| Verbatim of string
override this.ToString () : string =
match this with
| CherryPickHead -> "CHERRY_PICK_HEAD"
@@ -28,7 +29,9 @@ type SymbolicRef =
module SymbolicRef =
let getFile (r : Repository) (name : SymbolicRef) : IFileInfo =
name.ToString ()
|> fun i -> r.Fs.Path.Combine ((Repository.gitDir r).FullName, i) |> r.Fs.FileInfo.FromFileName
|> fun i ->
r.Fs.Path.Combine ((Repository.gitDir r).FullName, i)
|> r.Fs.FileInfo.FromFileName
type SymbolicRefLookupError =
| RefDidNotExist
@@ -40,18 +43,21 @@ module SymbolicReference =
/// This is effectively `git symbolic-ref NAME`.
let lookup (r : Repository) (name : SymbolicRef) : Result<SymbolicRefTarget, SymbolicRefLookupError> =
let f = SymbolicRef.getFile r name
if not <| f.Exists then Error RefDidNotExist
if not <| f.Exists then
Error RefDidNotExist
else
r.Fs.File.ReadAllText f.FullName
|> fun contents ->
if contents.Substring (0, 5) = "ref: " then contents.Substring 5 |> SymbolicRefTarget |> Ok
if contents.Substring (0, 5) = "ref: " then
contents.Substring 5 |> SymbolicRefTarget |> Ok
else
Error (MalformedRef contents)
let write (r : Repository) (name : SymbolicRef) (contents : string) : unit =
if not <| contents.StartsWith "refs/" then
failwithf "refusing to point %O outside of refs/" name
r.Fs.File.WriteAllText ((SymbolicRef.getFile r name).FullName, sprintf "ref: %s" contents)
let delete (r : Repository) (name : SymbolicRef) : unit =
(SymbolicRef.getFile r name).Delete ()
let delete (r : Repository) (name : SymbolicRef) : unit = (SymbolicRef.getFile r name).Delete ()