github.com/konsorten/ktn-build-info@v1.0.11/ver/git.go (about)

     1  package ver
     2  
     3  import (
     4  	"os/exec"
     5  	"syscall"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  func TryReadFromGit() (*VersionInformation, error) {
    11  	// get revision
    12  	rev, err := exec.Command("git", "log", "-n", "1", "--pretty=format:%H").Output()
    13  
    14  	if err != nil {
    15  		// not a git repository?
    16  		if exiterr, ok := err.(*exec.ExitError); ok {
    17  			if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
    18  				if status.ExitStatus() == 128 /* not found */ {
    19  					log.Debugf("Not a Git repository; no revision retrieved")
    20  
    21  					return nil, nil
    22  				}
    23  			}
    24  		}
    25  
    26  		return nil, err
    27  	}
    28  
    29  	// done
    30  	vi := MakeVersionInformation()
    31  
    32  	vi.Revision = string(rev)
    33  
    34  	log.Debugf("Found Git revision: %v", vi.Revision)
    35  
    36  	return vi, nil
    37  }