...

Source file src/github.com/mattn/goveralls/gitinfo.go

Documentation: github.com/mattn/goveralls

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"strings"
     9  )
    10  
    11  // A Head object encapsulates information about the HEAD revision of a git repo.
    12  type Head struct {
    13  	ID             string `json:"id"`
    14  	AuthorName     string `json:"author_name,omitempty"`
    15  	AuthorEmail    string `json:"author_email,omitempty"`
    16  	CommitterName  string `json:"committer_name,omitempty"`
    17  	CommitterEmail string `json:"committer_email,omitempty"`
    18  	Message        string `json:"message"`
    19  }
    20  
    21  // A Git object encapsulates information about a git repo.
    22  type Git struct {
    23  	Head   Head   `json:"head"`
    24  	Branch string `json:"branch"`
    25  }
    26  
    27  // collectGitInfo runs several git commands to compose a Git object.
    28  func collectGitInfo(ref string) *Git {
    29  	gitCmds := map[string][]string{
    30  		"id":      {"rev-parse", ref},
    31  		"branch":  {"branch", "--format", "%(refname:short)", "--contains", ref},
    32  		"aname":   {"show", "-s", "--format=%aN", ref},
    33  		"aemail":  {"show", "-s", "--format=%aE", ref},
    34  		"cname":   {"show", "-s", "--format=%cN", ref},
    35  		"cemail":  {"show", "-s", "--format=%cE", ref},
    36  		"message": {"show", "-s", "--format=%s", ref},
    37  	}
    38  	results := map[string]string{}
    39  	gitPath, err := exec.LookPath("git")
    40  	if err != nil {
    41  		log.Printf("fail to look path of git: %v", err)
    42  		log.Print("git information is omitted")
    43  		return nil
    44  	}
    45  
    46  	if ref != "HEAD" {
    47  		// make sure that the commit is in the local
    48  		// e.g. shallow cloned repository
    49  		_, _ = runCommand(gitPath, "fetch", "--depth=1", "origin", ref)
    50  		// ignore errors because we don't have enough information about the origin.
    51  	}
    52  
    53  	for key, args := range gitCmds {
    54  		if key == "branch" {
    55  			if envBranch := loadBranchFromEnv(); envBranch != "" {
    56  				results[key] = envBranch
    57  				continue
    58  			}
    59  		}
    60  
    61  		ret, err := runCommand(gitPath, args...)
    62  		if err != nil {
    63  			log.Printf(`fail to run "%s %s": %v`, gitPath, strings.Join(args, " "), err)
    64  			log.Print("git information is omitted")
    65  			return nil
    66  		}
    67  		results[key] = ret
    68  	}
    69  	h := Head{
    70  		ID:             firstLine(results["id"]),
    71  		AuthorName:     firstLine(results["aname"]),
    72  		AuthorEmail:    firstLine(results["aemail"]),
    73  		CommitterName:  firstLine(results["cname"]),
    74  		CommitterEmail: firstLine(results["cemail"]),
    75  		Message:        results["message"],
    76  	}
    77  	g := &Git{
    78  		Head:   h,
    79  		Branch: firstLine(results["branch"]),
    80  	}
    81  	return g
    82  }
    83  
    84  func runCommand(gitPath string, args ...string) (string, error) {
    85  	cmd := exec.Command(gitPath, args...)
    86  	ret, err := cmd.CombinedOutput()
    87  	if err != nil {
    88  		return "", err
    89  	}
    90  	ret = bytes.TrimRight(ret, "\n")
    91  	return string(ret), nil
    92  }
    93  
    94  func firstLine(s string) string {
    95  	if idx := strings.Index(s, "\n"); idx >= 0 {
    96  		return s[:idx]
    97  	}
    98  	return s
    99  }
   100  
   101  var varNames = [...]string{
   102  	"GIT_BRANCH",
   103  
   104  	// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables
   105  	"GITHUB_HEAD_REF", "GITHUB_REF",
   106  
   107  	"CIRCLE_BRANCH", "TRAVIS_BRANCH",
   108  	"CI_BRANCH", "APPVEYOR_REPO_BRANCH",
   109  	"WERCKER_GIT_BRANCH", "DRONE_BRANCH",
   110  	"BUILDKITE_BRANCH", "BRANCH_NAME",
   111  }
   112  
   113  func loadBranchFromEnv() string {
   114  	for _, varName := range varNames {
   115  		if branch := os.Getenv(varName); branch != "" {
   116  			if varName == "GITHUB_REF" {
   117  				return strings.TrimPrefix(branch, "refs/heads/")
   118  			}
   119  			return branch
   120  		}
   121  	}
   122  
   123  	return ""
   124  }
   125  

View as plain text