[llvm] r305598 - utils: Add a git-r utility for mapping svn revisions to git revisions in the monorepo.
Adrian Prantl via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 19 10:02:46 PDT 2017
I think there's a difference between using Go in the Go bindings (that are only going to be used by developers that already have Go installed) and using it for a utility that is of general interest.
-- adrian
> On Jun 19, 2017, at 10:00 AM, Peter Collingbourne via llvm-commits <llvm-commits at lists.llvm.org> wrote:
>
> We already use Go in the LLVM project in a few places (llgo, llvm-go and the Go bindings). We even have a section about it in the coding standards:
>
> http://llvm.org/docs/CodingStandards.html#other-languages <http://llvm.org/docs/CodingStandards.html#other-languages>
>
> So I don't see a problem with using Go for this tool, especially given that it is relatively isolated.
>
> Peter
>
> On Mon, Jun 19, 2017 at 9:48 AM, David Blaikie <dblaikie at gmail.com <mailto:dblaikie at gmail.com>> wrote:
> I think it's probably best to use a language already used in the LLVM project (Python? Bash?) than branching out into another language (Go) in terms of maintainability by other LLVM developers?
>
> On Fri, Jun 16, 2017 at 3:15 PM Peter Collingbourne via llvm-commits <llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>> wrote:
> Author: pcc
> Date: Fri Jun 16 17:15:18 2017
> New Revision: 305598
>
> URL: http://llvm.org/viewvc/llvm-project?rev=305598&view=rev <http://llvm.org/viewvc/llvm-project?rev=305598&view=rev>
> Log:
> utils: Add a git-r utility for mapping svn revisions to git revisions in the monorepo.
>
> Added:
> llvm/trunk/utils/git-svn/git-r/
> llvm/trunk/utils/git-svn/git-r/git-r.go
>
> Added: llvm/trunk/utils/git-svn/git-r/git-r.go
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/git-svn/git-r/git-r.go?rev=305598&view=auto <http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/git-svn/git-r/git-r.go?rev=305598&view=auto>
> ==============================================================================
> --- llvm/trunk/utils/git-svn/git-r/git-r.go (added)
> +++ llvm/trunk/utils/git-svn/git-r/git-r.go Fri Jun 16 17:15:18 2017
> @@ -0,0 +1,169 @@
> +//===- git-r.go - svn revisions to git revisions --------------------------===//
> +//
> +// The LLVM Compiler Infrastructure
> +//
> +// This file is distributed under the University of Illinois Open Source
> +// License. See LICENSE.TXT for details.
> +//
> +//===----------------------------------------------------------------------===//
> +//
> +// This is a small program for mapping svn revisions to git revisions in the
> +// monorepo.
> +//
> +// To set up:
> +// 1) http://llvm.org/docs/GettingStarted.html#for-developers-to-work-with-a-git-monorepo <http://llvm.org/docs/GettingStarted.html#for-developers-to-work-with-a-git-monorepo>
> +// and make sure to follow the instructions for fetching commit notes.
> +// 2) go build
> +// 3) cp git-r ~/bin
> +//
> +// To use:
> +// $ git r 1
> +// 09c4b68e68c4fcff64b00e1ac077c4f4a524cbcc
> +//
> +//===----------------------------------------------------------------------===//
> +
> +package main
> +
> +import (
> + "bufio"
> + "bytes"
> + "encoding/gob"
> + "fmt"
> + "log"
> + "os"
> + "os/exec"
> + "strconv"
> + "strings"
> +)
> +
> +func git(args ...string) (*bytes.Buffer, error) {
> + cmd := exec.Command("git", args...)
> +
> + var b bytes.Buffer
> + cmd.Stdout = &b
> + err := cmd.Run()
> +
> + return &b, err
> +}
> +
> +func mkrevmap() []string {
> + revs, err := git("grep", "git-svn-rev", "refs/notes/commits")
> + if err != nil {
> + panic(err)
> + }
> +
> + var revmap []string
> +
> + scanner := bufio.NewScanner(revs)
> + for scanner.Scan() {
> + // refs/notes/commits:00/0b/d4acb454290301c140a1d9c4f7a45aa2fa9c:git-svn-rev: 37235
> +
> + bits := strings.Split(scanner.Text(), ":")
> + gitrev := strings.Replace(bits[1], "/", "", -1)
> + svnrev := bits[3][1:]
> +
> + svnrevn, err := strconv.Atoi(svnrev)
> + if err != nil {
> + panic(err)
> + }
> +
> + if svnrevn >= len(revmap) {
> + newrevmap := make([]string, svnrevn+1)
> + copy(newrevmap, revmap)
> + revmap = newrevmap
> + }
> + revmap[svnrevn] = gitrev
> + }
> +
> + return revmap
> +}
> +
> +type revmap struct {
> + Noterev string
> + Revs []string
> +}
> +
> +func writerevmap(path string, rmap *revmap, svnrev int) {
> + noterevbuf, err := git("rev-parse", "refs/notes/commits")
> + if err != nil {
> + fmt.Fprintf(os.Stderr, "%s: could not find refs/notes/commits, see instructions:\n", os.Args[0])
> + fmt.Fprintln(os.Stderr, "http://llvm.org/docs/GettingStarted.html#for-developers-to-work-with-a-git-monorepo <http://llvm.org/docs/GettingStarted.html#for-developers-to-work-with-a-git-monorepo>")
> + os.Exit(1)
> + }
> + noterev := noterevbuf.String()
> + noterev = noterev[:len(noterev)-1]
> +
> + if rmap == nil || rmap.Noterev != noterev {
> + var newrmap revmap
> + newrmap.Revs = mkrevmap()
> + newrmap.Noterev = noterev
> +
> + f, err := os.Create(path)
> + if err != nil {
> + panic(err)
> + }
> +
> + enc := gob.NewEncoder(f)
> + err = enc.Encode(newrmap)
> + if err != nil {
> + os.Remove(path)
> + panic(err)
> + }
> +
> + rmap = &newrmap
> + }
> +
> + if svnrev >= len(rmap.Revs) || rmap.Revs[svnrev] == "" {
> + fmt.Fprintf(os.Stderr, "%s: %d: unknown revision\n", os.Args[0], svnrev)
> + os.Exit(1)
> + }
> +
> + fmt.Println(rmap.Revs[svnrev])
> +}
> +
> +func main() {
> + if len(os.Args) != 2 {
> + fmt.Fprintf(os.Stderr, "%s: expected a single argument\n", os.Args[0])
> + os.Exit(1)
> + }
> + svnrev, err := strconv.Atoi(os.Args[1])
> + if err != nil {
> + fmt.Fprintf(os.Stderr, "%s: %s: expected an integer argument\n", os.Args[0], os.Args[1])
> + os.Exit(1)
> + }
> +
> + gitdirbuf, err := git("rev-parse", "--git-common-dir")
> + if err != nil {
> + fmt.Fprintf(os.Stderr, "%s: not in a git repository\n", os.Args[0])
> + os.Exit(1)
> + }
> +
> + gitdir := gitdirbuf.String()
> + gitdir = gitdir[:len(gitdir)-1]
> + err = os.Chdir(gitdir)
> + if err != nil {
> + panic(err)
> + }
> +
> + mappath := "git-svn-revmap-cache"
> + f, err := os.Open(mappath)
> + if err != nil {
> + writerevmap(mappath, nil, svnrev)
> + return
> + }
> +
> + dec := gob.NewDecoder(f)
> + var rmap revmap
> + err = dec.Decode(&rmap)
> + if err != nil {
> + writerevmap(mappath, nil, svnrev)
> + return
> + }
> +
> + if svnrev < len(rmap.Revs) && rmap.Revs[svnrev] != "" {
> + fmt.Println(rmap.Revs[svnrev])
> + return
> + }
> +
> + writerevmap(mappath, &rmap, svnrev)
> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits>
>
>
>
> --
> --
> Peter
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170619/708bc845/attachment.html>
More information about the llvm-commits
mailing list