/* Attempts to load a Graph at the given dir, or creates a new one if no graph repo is found. If a new graph is fabricated, it will be initialized by: - creating a new git repo, - making a blank root commit, - and tagging it with a branch name that declares it to be a graph repo. Note if your cwd is already in a git repo, the new graph will not be commited, nor will it be made a submodule. You're free to make it a submodule yourself, but git quite wants you to have a remote url before it accepts your submodule. */ func NewGraph(dir string) *Graph { g := newGraph(dir) if g.isHrootGraphRepo() { // if we can just be a load, do it return g } else if g.isRepoRoot() { // if this is a repo root, but didn't look like a real graph... util.ExitGently("Attempted to make a hroot graph at ", g.dir, ", but there is already a git repo there and it does not appear to belong to hroot.") } // else carry on, make it! // we'll make exactly one new dir if the path doesn't exist yet. more is probably argument error and we abort. // this is actually implemented via MkdirAll here (because Mkdir errors on existing, and I can't be arsed) and letting the SaneDir check earlier blow up if we're way out. err := os.MkdirAll(g.dir, 0755) if err != nil { panic(err) } // git init g.cmd("init")("--bare")() g.withTempTree(func(cmd Command) { // set up basic repo to identify as graph repo cmd("commit", "--allow-empty", "-mhroot")() cmd("checkout", "-b", hroot_ref_prefix+"init")() // discard master branch. a hroot graph has no real use for it. cmd("branch", "-D", "master")() }) // should be good to go return g }
func (g *Graph) Load(lineage string, gr GraphLoadRequest) (hash string) { lineage, _ = SplitImageName(lineage) //Handle tags //Check if the image is in the graph so we can generate a relatively friendly error message if !g.HasBranch(hroot_image_ref_prefix + lineage) { //HALP util.ExitGently("Image branch name", lineage, "not found in graph.") } g.withTempTree(func(cmd Command) { // checkout lineage. // "-f" because otherwise if git thinks we already had this branch checked out, this working tree is just chock full of deletes. g.cmd("checkout", "-f", git_branch_ref_prefix+hroot_image_ref_prefix+lineage)() // the gr consumes this filesystem and shoves it at whoever it deals with; we're actually hands free after handing over a dir. gr.receive(".") hash = "" //FIXME }) return }