github.com/komosa/bug@v0.3.1/scm/GitManager.go (about)

     1  package scm
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/driusan/bug/bugs"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"regexp"
    10  	"strings"
    11  )
    12  
    13  type PreconditionFailed string
    14  
    15  func (a PreconditionFailed) Error() string {
    16  	return string(a)
    17  }
    18  
    19  type ExecutionFailed string
    20  
    21  func (a ExecutionFailed) Error() string {
    22  	return string(a)
    23  }
    24  
    25  type UnsupportedType string
    26  
    27  func (a UnsupportedType) Error() string {
    28  	return string(a)
    29  }
    30  
    31  type GitManager struct {
    32  	Autoclose bool
    33  }
    34  
    35  func (a GitManager) Purge(dir bugs.Directory) error {
    36  	cmd := exec.Command("git", "clean", "-fd", string(dir))
    37  
    38  	cmd.Stdin = os.Stdin
    39  	cmd.Stdout = os.Stdout
    40  	cmd.Stderr = os.Stderr
    41  	err := cmd.Run()
    42  
    43  	if err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  func (a GitManager) getDeletedIdentifiers(dir bugs.Directory) []string {
    50  	cmd := exec.Command("git", "status", "-z", "--porcelain", string(dir))
    51  	out, _ := cmd.CombinedOutput()
    52  	files := strings.Split(string(out), "\000")
    53  	retVal := []string{}
    54  	for _, file := range files {
    55  		if file == "" {
    56  			continue
    57  		}
    58  		if file[0:1] == "D" && strings.HasSuffix(file, "Identifier") {
    59  			ghRegex := regexp.MustCompile("(?im)^-Github:(\\s*)(.*)(\\s*)$^")
    60  			diff := exec.Command("git", "diff", "--staged", "--", file[3:])
    61  			diffout, _ := diff.CombinedOutput()
    62  			if matches := ghRegex.FindStringSubmatch(string(diffout)); len(matches) > 2 {
    63  				retVal = append(retVal, matches[2])
    64  			}
    65  		}
    66  	}
    67  	return retVal
    68  }
    69  func (a GitManager) Commit(dir bugs.Directory, commitMsg string) error {
    70  	cmd := exec.Command("git", "add", "-A", string(dir))
    71  	if err := cmd.Run(); err != nil {
    72  		fmt.Printf("Could not add issues to be commited: %s?\n", err.Error())
    73  		return err
    74  
    75  	}
    76  
    77  	var deletedIdentifiers []string
    78  	if a.Autoclose == true {
    79  		deletedIdentifiers = a.getDeletedIdentifiers(dir)
    80  	} else {
    81  		deletedIdentifiers = []string{}
    82  	}
    83  	if len(deletedIdentifiers) > 0 {
    84  		commitMsg = fmt.Sprintf("%s\n\nCloses %s\n", commitMsg, strings.Join(a.getDeletedIdentifiers(dir), ", closes "))
    85  	} else {
    86  		commitMsg = fmt.Sprintf("%s\n", commitMsg)
    87  	}
    88  	file, err := ioutil.TempFile("", "bugCommit")
    89  	if err != nil {
    90  		fmt.Fprintf(os.Stderr, "Could not create file for commit message.\n")
    91  	}
    92  	defer func() {
    93  		os.Remove(file.Name())
    94  	}()
    95  	file.WriteString(commitMsg)
    96  	cmd = exec.Command("git", "commit", "-o", string(dir), "-F", file.Name(), "-q")
    97  	if err := cmd.Run(); err != nil {
    98  		// If nothing was added commit will have an error,
    99  		// but we don't care it just means there's nothing
   100  		// to commit.
   101  		fmt.Printf("No new issues commited\n")
   102  		return nil
   103  	}
   104  	return nil
   105  }
   106  
   107  func (a GitManager) GetSCMType() string {
   108  	return "git"
   109  }