summaryrefslogtreecommitdiff
path: root/doTag.go
blob: 8e029a584cad8000c8c841f7b48445815613c1a7 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

// checks that repos are in a "normal" state

import (
	"os"
	"path/filepath"
	"time"

	"go.wit.com/lib/fhelp"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func FindRepoByFullPath(wd string) *gitpb.Repo {
	for repo := range me.forge.Repos.IterAll() {
		if repo.FullPath == wd {
			return repo
		}
	}
	return nil
}

func findCurrentPwdRepoOrDie() *gitpb.Repo {
	wd, err := os.Getwd()
	repo := FindRepoByFullPath(wd)
	if repo == nil {
		log.Info("Could not find repo:", wd)
		badExit(err)
	}
	return repo
}

func doTag() error {
	wd, _ := os.Getwd()
	if argv.Show.Tag != nil {
		repo := findCurrentPwdRepoOrDie()

		tagTablePB := makeTagTablePB(repo, repo.Tags)
		// tbox := win.Bottom.Box().SetProgName("TBOX")
		// t.SetParent(tbox)
		tagTablePB.MakeTable()
		tagTablePB.PrintTable()
		log.Info("list tags here", repo.Namespace)
		return nil
	}

	if argv.Show.Tag.Delete != "" {
		repo := FindRepoByFullPath(wd)
		if repo == nil {
			log.Info("Could not find repo:", wd)
			return nil
		}

		// check if the git tag already exists somehow
		/*
			if !repo.LocalTagExists(testtag) {
				log.Info("Tag", testtag, "does not exist")
				return log.Errorf("%s TAG DOES NOT EXIST %s", repo.FullPath, testtag)
			}
		*/
		testtag := argv.Show.Tag.Delete
		if !argv.Force {
			if !fhelp.QuestionUser(log.Sprintf("delete tag '%s'?", testtag)) {
				return nil
			}
		}
		log.Info("Delete tag here", testtag)

		// delete local and remote tag
		repo.RunVerbose([]string{"git", "tag", "--delete", testtag})
		repo.RunVerbose([]string{"git", "push", "--delete", "origin", testtag})
		return nil
	}

	log.Info("do other tag stuff here")
	return nil
}

func makeTagTablePB(repo *gitpb.Repo, pb *gitpb.GitTags) *gitpb.GitTagsTable {
	t := pb.NewTable("tagList")
	t.NewUuid()

	col := t.AddHash()
	col.Width = 12

	col = t.AddStringFunc("bashash", func(tag *gitpb.GitTag) string {
		_, base := filepath.Split(tag.Refname)
		cmd, err := repo.RunStrict([]string{"git", "log", "-1", base, "--format=%H"})
		if err != nil {
			return "err"
		}
		if len(cmd.Stdout) == 0 {
			return ""
		}
		return cmd.Stdout[0]
	})
	col.Width = 12

	col = t.AddTimeFunc("ctime", func(tag *gitpb.GitTag) time.Time {
		// todo
		return tag.Creatordate.AsTime()
	})
	col.Width = 4

	col = t.AddTimeFunc("age", func(repo *gitpb.GitTag) time.Time {
		// todo
		return time.Now()
	})
	col.Width = 4

	col = t.AddStringFunc("Ref Name", func(r *gitpb.GitTag) string {
		_, ref := filepath.Split(r.GetRefname())
		return ref
	})
	col.Width = 16

	col = t.AddSubject()
	col.Width = -1

	return t
}