-
Notifications
You must be signed in to change notification settings - Fork 19
/
gitupdater.go
81 lines (74 loc) · 1.85 KB
/
gitupdater.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
// Copyright 2014 The Azul3D Authors. 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
import (
"bytes"
"os/exec"
)
// GitUpdater can update a git repository and tell you if anything was actually
// changed.
//
// Pulling changes into a git repository is not enough without knowing if
// something was pulled, because we will restart the process if changes have
// occured.
type GitUpdater struct {
Dir string
}
// localSHA returns the SHA of the local repository:
//
// git rev-parse HEAD
//
func (g *GitUpdater) localSHA() (string, error) {
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = g.Dir
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return string(out.Bytes()[:40]), nil
}
// remoteSHA queries the origin of the repository for the HEAD SHA:
//
// git ls-remote origin HEAD
//
func (g *GitUpdater) remoteSHA() (string, error) {
cmd := exec.Command("git", "ls-remote", "origin", "HEAD")
cmd.Dir = g.Dir
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return string(out.Bytes()[:40]), nil
}
// Update checks if the local and remote SHA's of HEAD match, if they don't
// then it fast-forwards the repository and returns true. False is always
// returned otherwise.
func (g *GitUpdater) Update() (bool, error) {
// If the local and remote are identical, then no changes have occured in
// HEAD.
local, err := g.localSHA()
if err != nil {
return false, err
}
remote, err := g.remoteSHA()
if err != nil {
return false, err
}
if local == remote {
// No changes in HEAD.
return false, nil
}
// Pull and fast-forward now.
cmd := exec.Command("git", "pull", "--ff-only")
cmd.Dir = g.Dir
err = cmd.Run()
if err != nil {
return false, err
}
return true, nil
}