summaryrefslogtreecommitdiff
path: root/doCommit.go
blob: 95c844aff2fa7f8cac46ea1ee8af49429d7db450 (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
// 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"

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

func doCommit() {
	if argv.All {
		log.Info("do a commit everywhere")
		doCheckDirtyAndConfigSave()
		me.found = new(gitpb.Repos)
		findDirty()
		all := me.found.All()
		for all.Scan() {
			repo := all.Next()
			log.Info("do a commit on repo", repo.GetGoPath())
			if err := doCommitRepo(repo); err != nil {
				badExit(err)
			}
		}
		okExit("")
	}

	pwd, _ := os.Getwd()
	repo := me.forge.Repos.FindByFullPath(pwd)
	if repo == nil {
		log.Info("todo: forge doesn't know how to work here yet")
		okExit("")
	}

	if repo.GetCurrentBranchName() != repo.GetUserBranchName() {
		me.found.Append(repo)
		me.forge.PrintHumanTable(me.found)
		log.Info("")
		log.Info("wrong branch. Can not commit on", repo.GetCurrentBranchName())
		log.Info("")
		okExit("")
	}

	os.Setenv("LESS", "-XR")
	if err := shell.Exec([]string{"git", "diff"}); err != nil {
		badExit(err)
	}

	if argv.All {
		if err := shell.ExecCheck([]string{"git", "add", "--all"}); err != nil {
			badExit(err)
		}
	}

	if err := shell.ExecCheck([]string{"git", "commit", "--all"}); err != nil {
		badExit(err)
	}
	log.Info("git commit ok. forge done")
}

func doCommitRepo(repo *gitpb.Repo) error {
	if repo.GetCurrentBranchName() != repo.GetUserBranchName() {
		me.found.Append(repo)
		me.forge.PrintHumanTable(me.found)
		log.Info("")
		log.Info("wrong branch. Can not commit on", repo.GetCurrentBranchName())
		log.Info("")
		return nil
	}

	os.Setenv("LESS", "-XR")
	if err := shell.Exec([]string{"git", "diff"}); err != nil {
		return err
	}

	if argv.All {
		if err := shell.ExecCheck([]string{"git", "add", "--all"}); err != nil {
			return err
		}
	}

	if err := shell.ExecCheck([]string{"git", "commit", "--all"}); err != nil {
		return err
	}
	log.Info("git commit ok. forge done")
	return nil
}