summaryrefslogtreecommitdiff
path: root/doGit.go
blob: 167342c155e7662516276d578a3075ceda4728ff (plain)
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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func doGit() error {
	log.DaemonMode(true)
	defer log.DaemonMode(false)

	if argv.Git.Log != nil {
		fstr := "--format=\"%h %>(24)%ar %>(20)%an  %s"
		cmd := []string{"git", "log", fstr}
		shell.RunVerbose(cmd)
		okExit("")
	}

	if argv.Git.Tag != nil {
		cmd := []string{"git", "for-each-ref", "--sort=taggerdate", "--format"}
		cmd = append(cmd, "%(tag)%00%(taggerdate:raw)%00%(taggername)%00%(subject)")
		cmd = append(cmd, "refs/tags")
		result := shell.RunQuiet(cmd)
		for i, line := range result.Stdout {
			parts := strings.Split(line, "\x00")
			log.Infof("LINE: %d %v\n", i, parts)
		}
	}

	if argv.Git.Who != nil {
		cmd := []string{"git", "who"}
		shell.RunVerbose(cmd)
		okExit("")
	}

	if argv.Git.Pull != nil {
		doPull(".config/wit")
		doPull("tools")
		okExit("")
	}

	if argv.Git.Push != nil {
		doPush(".config/wit")
		doPush("tools")
		okExit("")
	}

	return nil
}

func findRepo(wpath string) (*gitpb.Repo, bool) {
	d := filepath.Join(me.homedir, wpath)
	if !shell.IsDir(d) {
		return nil, false
	}
	repo, err := gitpb.NewRepo(d)
	if err != nil {
		log.Info("path error", d, err)
		return nil, false
	}
	return repo, true
}

func doPull(wpath string) {
	repo, ok := findRepo(wpath)
	if !ok {
		return
	}
	os.Chdir(repo.FullPath)
	exitOnErrorRealtime([]string{"git", "pull"})
}

func doPush(wpath string) {
	doPull(wpath)
	d := filepath.Join(me.homedir, wpath)
	if !shell.IsDir(d) {
		return
	}
	repo, err := gitpb.NewRepo(d)
	if err != nil {
		log.Info("path error", d, err)
		return
	}
	if repo == nil {
		log.Info("repo is nil", d, err)
		return
	}

	if err := repo.GitCommit(); err != nil {
		badExit(err)
	}

	repo.RunRealtime([]string{"git", "push"})
}