-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.go
More file actions
91 lines (79 loc) · 2.12 KB
/
Copy pathgit.go
File metadata and controls
91 lines (79 loc) · 2.12 KB
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
// Hap - the simple and effective provisioner
// Copyright (c) 2019 GWoo (https://github.com/gwoo)
// The BSD License http://opensource.org/licenses/bsd-license.php.
package hap
import (
"fmt"
"os"
"os/exec"
)
// Git struct
type Git struct {
Repo string
Work string
Key string
}
// Exists checks whether the git executable exists
func (g Git) Exists() error {
o, err := exec.LookPath("git")
if err != nil {
return fmt.Errorf("%s\n%s", o, err)
}
return nil
}
// Commit adds all files, including untracked to the repo
func (g Git) Commit(message string) ([]byte, error) {
cmd := exec.Command("git", "add", ".")
cmd.Dir = g.Work
result, err := cmd.CombinedOutput()
if err != nil {
return result, err
}
cmd = exec.Command("git", "commit", "-q", "-m", message)
cmd.Dir = g.Work
return cmd.CombinedOutput()
}
// Branch creates a new branch
func (g Git) Branch(name string) ([]byte, error) {
cmd := exec.Command("git", "branch", name)
cmd.Dir = g.Work
return cmd.CombinedOutput()
}
// Checkout switches the branch
func (g Git) Checkout(name string) ([]byte, error) {
cmd := exec.Command("git", "checkout", name)
cmd.Dir = g.Work
return cmd.CombinedOutput()
}
// Push forces push to the branch to the remote repo
func (g Git) Push(branch string) ([]byte, error) {
if branch == "" {
branch = "master"
}
cmd := exec.Command("git", "push", "-f", g.Repo, branch)
cmd.Dir = g.Work
cmd.Env = os.Environ()
return cmd.CombinedOutput()
}
// RevParse returns current revision of HEAD
func (g Git) RevParse() ([]byte, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = g.Work
return cmd.CombinedOutput()
}
// UpdateSubmodules updates and initializes submodules
func (g Git) UpdateSubmodules() ([]byte, error) {
cmd := exec.Command("git", "submodule", "update", "--init")
cmd.Dir = g.Work
return cmd.CombinedOutput()
}
// Add this hook to the remote repo
const postReceiveHook string = `cat > ".git/hooks/post-receive" << "EOF"
#!/bin/bash
test "${PWD%/.git}" != "$PWD" && cd ..
unset GIT_DIR GIT_WORK_TREE
read oldrev newrev ref
branch=${ref#refs/heads/}
git reset -q --hard
git checkout -q ${branch}
EOF`