-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
120 lines (104 loc) · 3.04 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2011, Bryan Matsuo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
/* Filename: git.go
* Author: Bryan Matsuo <bryan.matsuo@gmail.com>
* Created: Sun Nov 6 01:17:31 PDT 2011
* Description:
*/
import (
"github.com/bmatsuo/go-script/script"
"path/filepath"
"errors"
"strings"
"fmt"
"os"
)
type ErrorRepoConfig error
func NewErrorRepoConfig(err string) ErrorRepoConfig { return ErrorRepoConfig(errors.New(err)) }
type gitRepo struct {
root string
shell script.Scriptor
}
func NewGitRepo(root string) (Repository, error) {
repo := new(gitRepo)
repo.root = root
repo.shell = script.Bash
return repo, repo.checkRoot()
}
func (repo *gitRepo) checkRoot() error {
repo_dir := repo.root + "/.git"
dir, staterr := os.Stat(repo_dir)
if staterr != nil {
return staterr
}
if !dir.IsDirectory() {
return fmt.Errorf("Git file %s is not a directory.")
}
return nil
}
func (repo *gitRepo) Root() string { return repo.root }
func (repo *gitRepo) Type() string { return "Git" }
func (repo *gitRepo) Name() (string, error) {
// TODO: look at the contents of the .git/config file
abs, err := filepath.Abs(repo.root)
if err != nil {
return "", err
}
return filepath.Base(abs), nil
}
func (repo *gitRepo) IsClean() (bool, error) {
tagcmd := ShellCmd{"git", "diff"}
tagscript := CmdTemplateScript(repo.shell, repo.root, tagcmd)
tagout, _, errexec := script.Output(tagscript)
if errexec != nil {
return false, errexec
}
if len(tagout) > 0 {
return false, nil
}
return true, nil
}
func (repo *gitRepo) Tags() ([]string, error) {
tagcmd := ShellCmd{"git", "tag", "-l"}
tagscript := CmdTemplateScript(repo.shell, repo.root, tagcmd)
tagout, _, errexec := script.Output(tagscript)
if errexec != nil {
return nil, errexec
}
tags := strings.Fields(strings.Trim(string(tagout), "\n"))
return tags, nil
}
func (repo *gitRepo) TagsPush() error {
tagcmd := ShellCmd{"git", "push", "--tags"}
tagscript := CmdTemplateScript(repo.shell, repo.root, tagcmd)
_, err := tagscript.Execute()
return err
}
func (repo *gitRepo) TagsFetch() error {
tagcmd := ShellCmd{"git", "fetch", "--tags"}
tagscript := CmdTemplateScript(repo.shell, repo.root, tagcmd)
_, err := tagscript.Execute()
return err
}
func (repo *gitRepo) TagDelete(tag string) error {
tagcmd := ShellCmd{"git", "tag", "-d", tag}
tagscript := CmdTemplateScript(repo.shell, repo.root, tagcmd)
_, err := tagscript.Execute()
return err
}
// If there is an extra value, it is used as a tag annotation.
// Remaining extra values (e.g. commit hash) will be appended to the command.
func (repo *gitRepo) TagNew(name string, extra ...string) error {
tagcmd := ShellCmd{"git", "tag", name}
if len(extra) > 0 {
// Remove the name on the end, insert an annotation, append extra[1:].
tagcmd = append(
append(tagcmd[:len(tagcmd)-1], "-a", "-m", extra[0], name),
ShellCmd(extra[1:])...)
}
tagscript := CmdTemplateScript(repo.shell, repo.root, tagcmd)
_, err := tagscript.Execute()
return err
}