github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt-smudge/git-dolt-smudge.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"log"
    22  	"os"
    23  
    24  	"github.com/dolthub/dolt/go/cmd/git-dolt/config"
    25  	"github.com/dolthub/dolt/go/cmd/git-dolt/doltops"
    26  	"github.com/dolthub/dolt/go/cmd/git-dolt/utils"
    27  )
    28  
    29  func main() {
    30  	// Because this is a git smudge filter, the pointer file contents
    31  	// are read through stdin.
    32  	r := bufio.NewReader(os.Stdin)
    33  	bs, err := ioutil.ReadAll(r)
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  
    38  	// Print the pointer file contents right back to stdout; the smudge filter
    39  	// uses this output to replace the contents of the smudged file. In this case,
    40  	// no changes to the file are desired (though this may change).
    41  	fmt.Printf("%s", bs)
    42  
    43  	cfg, err := config.Parse(string(bs))
    44  	if err != nil {
    45  		log.Fatalf("error parsing config: %v", err)
    46  	}
    47  
    48  	dirname := utils.LastSegment(cfg.Remote)
    49  
    50  	// We send output intended for the console to stderr instead of stdout
    51  	// or else it will end up in the pointer file.
    52  	fmt.Fprintf(os.Stderr, "Found git-dolt pointer file. Cloning remote %s to revision %s in directory %s...", cfg.Remote, cfg.Revision, dirname)
    53  
    54  	if err := doltops.CloneToRevisionSilent(cfg.Remote, cfg.Revision); err != nil {
    55  		log.Fatalf("error cloning repository: %v", err)
    56  	}
    57  
    58  	fmt.Fprintln(os.Stderr, "done.")
    59  }