Skip to content

Commit b5d7caa

Browse files
committed
feat: adds tagging functionality
Signed-off-by: ChrisJBurns <29541485+ChrisJBurns@users.noreply.github.com>
1 parent c353fdf commit b5d7caa

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ go.work
2424
create-cli
2525

2626
# exclude the folders created by the download command
27-
create-repositories
27+
create-repositories
28+
29+
.DS_Store

internal/download/download.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
910
"github.com/go-git/go-git/v5/plumbing/transport/http"
1011
"github.com/spf13/viper"
1112
)
@@ -42,8 +43,10 @@ func ReturnRepoListWithCloudProviderTemplate(cloudProvider string) map[string]st
4243
}
4344

4445
func Download() {
46+
4547
cloudProvider := viper.GetString("cloud-provider")
4648
personalAccessToken := viper.GetString("pat")
49+
tag := "1.0.0.0"
4750

4851
ReturnRepoListWithCloudProviderTemplate(cloudProvider)
4952

@@ -55,7 +58,7 @@ func Download() {
5558

5659
for repoName, repoUrl := range ReposToClone {
5760
log.Printf("Cloning %s into %s", repoName, CreateRepositoryDirectory)
58-
_, err := git.PlainClone(CreateRepositoryDirectory+repoName, false, &git.CloneOptions{
61+
r, err := git.PlainClone(CreateRepositoryDirectory+repoName, false, &git.CloneOptions{
5962
// The intended use of a GitHub personal access token is in replace of your password
6063
// because access tokens can easily be revoked.
6164
// https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
@@ -66,10 +69,66 @@ func Download() {
6669
URL: repoUrl,
6770
Progress: os.Stdout,
6871
})
72+
if err != nil {
73+
panic(err)
74+
}
6975

76+
// get work tree for cloned repo
77+
tree, err := r.Worktree()
7078
if err != nil {
7179
panic(err)
7280
}
81+
82+
checkoutTag(tag, tree)
83+
deletesMainBranch(r)
84+
createAndCheckoutMainBranchOffTag(tag, r, tree)
85+
}
86+
}
87+
88+
// checkoutTag checks out the passed tag
89+
func checkoutTag(tag string, tree *git.Worktree) {
90+
err := tree.Checkout(&git.CheckoutOptions{
91+
Branch: plumbing.ReferenceName("refs/tags/" + tag),
92+
})
93+
if err != nil {
94+
panic(err)
7395
}
96+
}
7497

98+
// deletesMainBranch deletes the main branch because we want to create a new
99+
// main branch off the specific tag. otherwise, the main branch will have the entire
100+
// history of the repository which in this case we don't want. just the specific version
101+
func deletesMainBranch(r *git.Repository) {
102+
err := r.Storer.RemoveReference("refs/heads/main")
103+
if err != nil {
104+
panic(err)
105+
}
106+
}
107+
108+
// createAndCheckpoutMainBranchOffTag will create and checkout a main branch off the commit hash reference
109+
// of the tag that is passed in. this is to ensure that the main branch only has the code on the tag.
110+
// otherwise, if you want the code on the 1.0.0.0 tag, without deleting and creating a new main
111+
// branch off the tag, the main branch will have all of the latest code - which in this case is not
112+
// what you want.
113+
func createAndCheckoutMainBranchOffTag(tag string, r *git.Repository, tree *git.Worktree) {
114+
// we get the commit reference that is at the tip of the tag
115+
headRef, err := r.Reference(plumbing.ReferenceName("refs/tags/"+tag), false)
116+
if err != nil {
117+
panic(err)
118+
}
119+
120+
// we create a new branch called `main` off the commit hash from the tag
121+
ref := plumbing.NewHashReference("refs/heads/main", headRef.Hash())
122+
err = r.Storer.SetReference(ref)
123+
if err != nil {
124+
panic(err)
125+
}
126+
127+
// we checkout the `main` branch just created.
128+
err = tree.Checkout(&git.CheckoutOptions{
129+
Branch: plumbing.ReferenceName("refs/heads/main"),
130+
})
131+
if err != nil {
132+
panic(err)
133+
}
75134
}

0 commit comments

Comments
 (0)