Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 44 lines (39 sloc) 1.116 kb
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
let file =
  if Array.length Sys.argv <> 2 then (
    Printf.eprintf "usage: ocaml %s <file>\n" Sys.argv.(0);
    exit 1
  ) else
    Sys.argv.(1)

let read file =
  if Sys.file_exists file then
    let ic = open_in_bin file in
    Some (input_line ic)
  else
    None

let write file contents =
  let write () =
    let oc = open_out file in
    output_string oc contents;
    output_char oc '\n';
    close_out oc in
  match read file with
  | None -> write ()
  | Some actual -> if actual <> contents then write ()

let (/) = Filename.concat

let git file = ".git" / file

let () =
  let opamGitVersion = "src" / "core" / "opamGitVersion.ml" in
  let version_none () =
    write opamGitVersion "let version = None" in
  match read (git "HEAD") with
  | None -> version_none ()
  | Some s ->
    let reference =
      try
        let c = String.rindex s ' ' in
        String.sub s (c+1) (String.length s -c-1)
      with Not_found ->
        s in
    match read (git reference) with
    | None -> version_none ()
    | Some sha1 -> write opamGitVersion (Printf.sprintf "let version = Some %S" sha1)
Something went wrong with that request. Please try again.