<commit_before>// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The gitlock command helps write Dockerfiles with a bunch of lines
// to lock git dependencies in place.
package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"go/build"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"
)

var (
	ignorePrefixFlag = flag.String("ignore", "golang.org/x/build", "comma-separated list of package prefixes to ignore")
	updateFile       = flag.String("update", "", "if non-empty, the Dockerfile to update. must have \"# BEGIN deps\" and \"# END deps\" lines.")
)

var ignorePrefixes []string

func parseDockerFile() (header, footer []byte) {
	if *updateFile == "" {
		return nil, nil
	}
	var headBuf, footBuf bytes.Buffer
	slurp, err := ioutil.ReadFile(*updateFile)
	if err != nil {
		log.Fatal(err)
	}
	var sawBegin, sawEnd bool
	bs := bufio.NewScanner(bytes.NewReader(slurp))
	for bs.Scan() {
		if !sawBegin {
			headBuf.Write(bs.Bytes())
			headBuf.WriteByte('\n')
			if strings.HasPrefix(bs.Text(), "# BEGIN deps") {
				sawBegin = true
				continue
			}
			continue
		}
		if strings.HasPrefix(bs.Text(), "# END deps") {
			sawEnd = true
		}
		if sawEnd {
			footBuf.Write(bs.Bytes())
			footBuf.WriteByte('\n')
		}
	}
	if err := bs.Err(); err != nil {
		log.Fatalf("error parsing %s: %v", *updateFile, err)
	}
	if !sawBegin {
		log.Fatalf(`file %s is missing a "# BEGIN deps" line`, *updateFile)
	}
	if !sawEnd {
		log.Fatalf(`file %s is missing a "# END deps" line`, *updateFile)
	}
	return headBuf.Bytes(), footBuf.Bytes()
}

func main() {
	flag.Parse()
	ignorePrefixes = strings.Split(*ignorePrefixFlag, ",")
	if flag.NArg() != 1 {
		log.SetFlags(0)
		log.Fatalf("Usage: gitlock <package>")
	}
	mainPkg := flag.Arg(0) // not necessary "package main", but almost certainly.
	header, footer := parseDockerFile()

	depOut, err := exec.Command("go", "list",
		"-tags=h2demo", // for golang.org/x/net/http2/h2demo; TODO: make a flag? meh.
		"-f", "{{range .Deps}}{{.}}\n{{end}}", mainPkg).Output()<commit_msg>cmd/gitlog: add flag for tags<commit_after>// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// The gitlock command helps write Dockerfiles with a bunch of lines
// to lock git dependencies in place.
package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"go/build"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"
)

var (
	ignorePrefixFlag = flag.String("ignore", "golang.org/x/build", "comma-separated list of package prefixes to ignore")
	updateFile       = flag.String("update", "", "if non-empty, the Dockerfile to update. must have \"# BEGIN deps\" and \"# END deps\" lines.")
	tags             = flag.String("tags", "", "space-separated tags to pass on to 'go list -tags=XXX target'")
)

var ignorePrefixes []string

func parseDockerFile() (header, footer []byte) {
	if *updateFile == "" {
		return nil, nil
	}
	var headBuf, footBuf bytes.Buffer
	slurp, err := ioutil.ReadFile(*updateFile)
	if err != nil {
		log.Fatal(err)
	}
	var sawBegin, sawEnd bool
	bs := bufio.NewScanner(bytes.NewReader(slurp))
	for bs.Scan() {
		if !sawBegin {
			headBuf.Write(bs.Bytes())
			headBuf.WriteByte('\n')
			if strings.HasPrefix(bs.Text(), "# BEGIN deps") {
				sawBegin = true
				continue
			}
			continue
		}
		if strings.HasPrefix(bs.Text(), "# END deps") {
			sawEnd = true
		}
		if sawEnd {
			footBuf.Write(bs.Bytes())
			footBuf.WriteByte('\n')
		}
	}
	if err := bs.Err(); err != nil {
		log.Fatalf("error parsing %s: %v", *updateFile, err)
	}
	if !sawBegin {
		log.Fatalf(`file %s is missing a "# BEGIN deps" line`, *updateFile)
	}
	if !sawEnd {
		log.Fatalf(`file %s is missing a "# END deps" line`, *updateFile)
	}
	return headBuf.Bytes(), footBuf.Bytes()
}

func main() {
	flag.Parse()
	ignorePrefixes = strings.Split(*ignorePrefixFlag, ",")
	if flag.NArg() != 1 {
		log.SetFlags(0)
		log.Fatalf("Usage: gitlock <package>")
	}
	mainPkg := flag.Arg(0) // not necessary "package main", but almost certainly.
	header, footer := parseDockerFile()

	depOut, err := exec.Command("go", "list",
		"-tags="+*tags,
		"-f", "{{range .Deps}}{{.}}\n{{end}}", mainPkg).Output()
{'fraction_non_alphanumeric': 0.16555835817473058, 'fraction_numerical': 0.0038981884888786974, 'mean_word_length': 3.54375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7519957', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1592, 'n_words': 444}