Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rebuild gotip if the HEAD does not change #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion gotip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package main

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -95,6 +97,9 @@ func installTip(root, clNumber string) error {
cmd.Dir = root
return cmd.Output()
}
chomp := func(s []byte) []byte {
return bytes.TrimRight(s, " \t\r\n")
}

if _, err := os.Stat(filepath.Join(root, ".git")); err != nil {
if err := os.MkdirAll(root, 0755); err != nil {
Expand All @@ -105,6 +110,16 @@ func installTip(root, clNumber string) error {
}
}

// Get current HEAD if there is an existing build.
var oldHead []byte
if _, err := os.Stat(filepath.Join(root, builtOkay)); err == nil {
head, err := gitOutput("rev-parse", "--short", "HEAD")
if err != nil {
return fmt.Errorf("failed to parse old HEAD revision: %v", err)
}
oldHead = head
}

if clNumber != "" {
fmt.Fprintf(os.Stderr, "This will download and execute code from golang.org/cl/%s, continue? [y/n] ", clNumber)
var answer string
Expand Down Expand Up @@ -152,6 +167,23 @@ func installTip(root, clNumber string) error {
if err := git("-c", "advice.detachedHead=false", "checkout", "FETCH_HEAD"); err != nil {
return fmt.Errorf("failed to checkout git repository: %v", err)
}

// Compare old and new HEADs to avoid unnecessary rebuilds if there are no changes.
// Notice that oldHead is not nil iff the last build was successful.
if oldHead != nil {
newHead, err := gitOutput("rev-parse", "--short", "HEAD")
if err != nil {
return fmt.Errorf("failed to parse new HEAD revision: %v", err)
}
if bytes.Equal(oldHead, newHead) {
log.Printf("Already built %s in %v", chomp(newHead), root)
return nil
}
if err := os.Remove(filepath.Join(root, builtOkay)); err != nil {
return err
}
}

// It shouldn't be the case, but in practice sometimes binary artifacts
// generated by earlier Go versions interfere with the build.
//
Expand Down Expand Up @@ -181,7 +213,9 @@ func installTip(root, clNumber string) error {
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to build go: %v", err)
}

if err := ioutil.WriteFile(filepath.Join(root, builtOkay), nil, 0644); err != nil {
return err
}
return nil
}

Expand All @@ -198,6 +232,10 @@ func makeScript() string {

const caseInsensitiveEnv = runtime.GOOS == "windows"

// builtOkay is a sentinel zero-byte file to indicate that Go
// repository was cloned and built successfully.
const builtOkay = ".built-success"

func exe() string {
if runtime.GOOS == "windows" {
return ".exe"
Expand Down