Skip to content

Commit 2c925bc

Browse files
committed
separate command for initializing lfsconfig
1 parent 2e16e2f commit 2c925bc

File tree

7 files changed

+214
-74
lines changed

7 files changed

+214
-74
lines changed

cmd/git-gitopia/lfs/init.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package lfs
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path"
9+
"strings"
10+
11+
"github.com/cosmos/cosmos-sdk/codec"
12+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
13+
"github.com/gitopia/git-remote-gitopia/config"
14+
"github.com/gitopia/git-remote-gitopia/core"
15+
gitopiatypes "github.com/gitopia/gitopia/v2/x/gitopia/types"
16+
"github.com/pkg/errors"
17+
"github.com/spf13/cobra"
18+
"google.golang.org/grpc"
19+
"google.golang.org/grpc/credentials/insecure"
20+
)
21+
22+
var initCmd = &cobra.Command{
23+
Use: "init <remote_name>",
24+
Short: "Initialize the lfsconfig for the gitopia remote",
25+
Args: cobra.ExactArgs(1),
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
dir := path.Dir(os.Getenv("GIT_DIR"))
28+
if dir == "" {
29+
return errors.New("not a git repository")
30+
}
31+
32+
lfsConfigPath := path.Join(dir, ".lfsconfig")
33+
if _, err := os.Stat(lfsConfigPath); os.IsNotExist(err) {
34+
c, buf := core.GitCommand("git", "remote", "get-url", args[0])
35+
if err := c.Start(); err != nil {
36+
return err
37+
}
38+
39+
output, err := io.ReadAll(buf)
40+
if err != nil {
41+
return err
42+
}
43+
44+
if err := c.Wait(); err != nil {
45+
return err
46+
}
47+
48+
remoteURL := strings.TrimSpace(string(output))
49+
50+
remoteUserId, remoteRepositoryName, err := core.ValidateGitopiaRemoteURL(string(remoteURL))
51+
if err != nil {
52+
return err
53+
}
54+
55+
interfaceRegistry := codectypes.NewInterfaceRegistry()
56+
57+
grpcConn, err := grpc.Dial(config.GRPCHost,
58+
grpc.WithTransportCredentials(insecure.NewCredentials()),
59+
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(interfaceRegistry).GRPCCodec())),
60+
)
61+
if err != nil {
62+
return err
63+
}
64+
defer grpcConn.Close()
65+
66+
queryClient := gitopiatypes.NewQueryClient(grpcConn)
67+
68+
// Get RepositoryId
69+
res, err := queryClient.AnyRepository(context.Background(), &gitopiatypes.QueryGetAnyRepositoryRequest{
70+
Id: remoteUserId,
71+
RepositoryName: remoteRepositoryName,
72+
})
73+
if err != nil {
74+
return err
75+
}
76+
77+
remoteRepository := *res.Repository
78+
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, remoteRepository.Id)
79+
80+
args := []string{
81+
"config",
82+
fmt.Sprintf("--file=%s", lfsConfigPath),
83+
"lfs.url",
84+
lfsURL,
85+
}
86+
87+
cmd, _ := core.GitCommand("git", args...)
88+
if err := cmd.Run(); err != nil {
89+
return errors.Wrap(err, "error creating .lfsconfig")
90+
}
91+
92+
return nil
93+
}
94+
95+
return errors.New(".lfsconfig file already exists")
96+
},
97+
}
98+
99+
func init() {
100+
Commands.AddCommand(initCmd)
101+
}

cmd/git-gitopia/lfs/root.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package lfs
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var Commands = &cobra.Command{
8+
Use: "lfs",
9+
Short: "Configure the lfsconfig for the gitopia remote",
10+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
11+
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
12+
},
13+
}

cmd/git-gitopia/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
1313
sdk "github.com/cosmos/cosmos-sdk/types"
1414
"github.com/cosmos/cosmos-sdk/version"
15+
"github.com/gitopia/git-remote-gitopia/cmd/git-gitopia/lfs"
1516
"github.com/spf13/cobra"
1617
)
1718

@@ -48,6 +49,7 @@ func main() {
4849
},
4950
}
5051
cmd.AddCommand(keys.Commands("."))
52+
cmd.AddCommand(lfs.Commands)
5153
err := cmd.ExecuteContext(ctx)
5254
if err != nil {
5355
fmt.Fprint(os.Stderr, err.Error())

cmd/git-gitopia/root.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/cosmos/cosmos-sdk/client"
7+
"github.com/cosmos/cosmos-sdk/codec"
8+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
9+
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
10+
sdk "github.com/cosmos/cosmos-sdk/types"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var rootCmd = &cobra.Command{
15+
Use: "gitopia",
16+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
17+
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
18+
conf := sdk.GetConfig()
19+
conf.SetBech32PrefixForAccount(AccountAddressPrefix, AccountAddressPrefix+sdk.PrefixPublic)
20+
conf.Seal()
21+
22+
registry := codectypes.NewInterfaceRegistry()
23+
cryptocodec.RegisterInterfaces(registry)
24+
marshaler := codec.NewProtoCodec(registry)
25+
26+
initClientCtx := client.GetClientContextFromCmd(cmd).
27+
WithCodec(marshaler).
28+
WithInterfaceRegistry(registry).
29+
WithInput(os.Stdin)
30+
31+
// sets global flags for keys subcommand
32+
return client.SetCmdClientContextHandler(initClientCtx, cmd)
33+
},
34+
}

cmd/git-remote-gitopia/gitopia.go

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"math"
99
"os"
10-
"path"
1110
"strings"
1211

1312
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
@@ -20,7 +19,7 @@ import (
2019
"github.com/gitopia/git-remote-gitopia/config"
2120
core "github.com/gitopia/git-remote-gitopia/core"
2221
"github.com/gitopia/git-remote-gitopia/core/wallet"
23-
gitopiaTypes "github.com/gitopia/gitopia/v2/x/gitopia/types"
22+
gitopiatypes "github.com/gitopia/gitopia/v2/x/gitopia/types"
2423
"github.com/gitopia/gitopia/v2/x/gitopia/utils"
2524
"github.com/go-git/go-git/v5/plumbing"
2625
"github.com/pkg/errors"
@@ -38,12 +37,12 @@ const (
3837

3938
type GitopiaHandler struct {
4039
grpcConn *grpc.ClientConn
41-
queryClient gitopiaTypes.QueryClient
40+
queryClient gitopiatypes.QueryClient
4241

4342
chainId string
4443
remoteUserId string
4544
remoteRepositoryName string
46-
remoteRepository gitopiaTypes.Repository
45+
remoteRepository gitopiatypes.Repository
4746

4847
didPush bool
4948

@@ -66,7 +65,7 @@ func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
6665
}
6766
// defer grpcConn.Close()
6867

69-
h.queryClient = gitopiaTypes.NewQueryClient(h.grpcConn)
68+
h.queryClient = gitopiatypes.NewQueryClient(h.grpcConn)
7069
serviceClient := tmservice.NewServiceClient(h.grpcConn)
7170

7271
// Get chain id for signing transaction
@@ -77,7 +76,7 @@ func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
7776
h.chainId = nodeInfoRes.DefaultNodeInfo.Network
7877

7978
// Get RepositoryId
80-
res, err := h.queryClient.AnyRepository(context.Background(), &gitopiaTypes.QueryGetAnyRepositoryRequest{
79+
res, err := h.queryClient.AnyRepository(context.Background(), &gitopiatypes.QueryGetAnyRepositoryRequest{
8180
Id: h.remoteUserId,
8281
RepositoryName: h.remoteRepositoryName,
8382
})
@@ -93,7 +92,7 @@ func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
9392
func (h *GitopiaHandler) List(remote *core.Remote, forPush bool) ([]string, error) {
9493
out := make([]string, 0)
9594

96-
branchAllRes, err := h.queryClient.RepositoryBranchAll(context.Background(), &gitopiaTypes.QueryAllRepositoryBranchRequest{
95+
branchAllRes, err := h.queryClient.RepositoryBranchAll(context.Background(), &gitopiatypes.QueryAllRepositoryBranchRequest{
9796
Id: h.remoteRepository.Owner.Id,
9897
RepositoryName: h.remoteRepository.Name,
9998
Pagination: &query.PageRequest{
@@ -107,7 +106,7 @@ func (h *GitopiaHandler) List(remote *core.Remote, forPush bool) ([]string, erro
107106
out = append(out, fmt.Sprintf("%s %s%s", branch.Sha, branchPrefix, branch.Name))
108107
}
109108

110-
tagAllRes, err := h.queryClient.RepositoryTagAll(context.Background(), &gitopiaTypes.QueryAllRepositoryTagRequest{
109+
tagAllRes, err := h.queryClient.RepositoryTagAll(context.Background(), &gitopiatypes.QueryAllRepositoryTagRequest{
111110
Id: h.remoteRepository.Owner.Id,
112111
RepositoryName: h.remoteRepository.Name,
113112
Pagination: &query.PageRequest{
@@ -123,25 +122,6 @@ func (h *GitopiaHandler) List(remote *core.Remote, forPush bool) ([]string, erro
123122

124123
out = append(out, fmt.Sprintf("@refs/heads/%s HEAD", h.remoteRepository.DefaultBranch))
125124

126-
dir := path.Dir(os.Getenv("GIT_DIR"))
127-
lfsConfigPath := path.Join(dir, ".lfsconfig")
128-
if _, err := os.Stat(lfsConfigPath); os.IsNotExist(err) {
129-
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, h.remoteRepository.Id)
130-
131-
args := []string{
132-
"config",
133-
fmt.Sprintf("--file=%s", lfsConfigPath),
134-
"lfs.url",
135-
lfsURL,
136-
}
137-
138-
cmd, _ := core.GitCommand("git", args...)
139-
if err := cmd.Run(); err != nil {
140-
return nil, errors.Wrap(err, "error creating .lfsconfig")
141-
}
142-
defer core.CleanUpProcessGroup(cmd)
143-
}
144-
145125
return out, nil
146126
}
147127

@@ -233,8 +213,8 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
233213
remoteURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, h.remoteRepository.Id)
234214

235215
var newRemoteRefSha string
236-
var setBranches []gitopiaTypes.MsgMultiSetBranch_Branch
237-
var setTags []gitopiaTypes.MsgMultiSetTag_Tag
216+
var setBranches []gitopiatypes.MsgMultiSetBranch_Branch
217+
var setTags []gitopiatypes.MsgMultiSetTag_Tag
238218
var deleteBranches, deleteTags []string
239219
var res []string
240220

@@ -305,7 +285,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
305285

306286
newRemoteRefSha = localCommitHash.String()
307287
remoteBranchName := strings.TrimPrefix(ref.Remote, branchPrefix)
308-
branch := gitopiaTypes.MsgMultiSetBranch_Branch{
288+
branch := gitopiatypes.MsgMultiSetBranch_Branch{
309289
Name: remoteBranchName,
310290
Sha: newRemoteRefSha,
311291
}
@@ -321,7 +301,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
321301

322302
newRemoteRefSha = tagRef.Hash().String()
323303
remoteTagName := strings.TrimPrefix(ref.Remote, tagPrefix)
324-
tag := gitopiaTypes.MsgMultiSetTag_Tag{
304+
tag := gitopiatypes.MsgMultiSetTag_Tag{
325305
Name: remoteTagName,
326306
Sha: newRemoteRefSha,
327307
}
@@ -336,25 +316,25 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
336316
var msg []sdk.Msg
337317

338318
if len(setBranches) > 0 {
339-
msg = append(msg, gitopiaTypes.NewMsgMultiSetBranch(h.wallet.Address(), gitopiaTypes.RepositoryId{
319+
msg = append(msg, gitopiatypes.NewMsgMultiSetBranch(h.wallet.Address(), gitopiatypes.RepositoryId{
340320
Id: h.remoteRepository.Owner.Id,
341321
Name: h.remoteRepository.Name,
342322
}, setBranches))
343323
}
344324
if len(setTags) > 0 {
345-
msg = append(msg, gitopiaTypes.NewMsgMultiSetTag(h.wallet.Address(), gitopiaTypes.RepositoryId{
325+
msg = append(msg, gitopiatypes.NewMsgMultiSetTag(h.wallet.Address(), gitopiatypes.RepositoryId{
346326
Id: h.remoteRepository.Owner.Id,
347327
Name: h.remoteRepository.Name,
348328
}, setTags))
349329
}
350330
if len(deleteBranches) > 0 {
351-
msg = append(msg, gitopiaTypes.NewMsgMultiDeleteBranch(h.wallet.Address(), gitopiaTypes.RepositoryId{
331+
msg = append(msg, gitopiatypes.NewMsgMultiDeleteBranch(h.wallet.Address(), gitopiatypes.RepositoryId{
352332
Id: h.remoteRepository.Owner.Id,
353333
Name: h.remoteRepository.Name,
354334
}, deleteBranches))
355335
}
356336
if len(deleteTags) > 0 {
357-
msg = append(msg, gitopiaTypes.NewMsgMultiDeleteTag(h.wallet.Address(), gitopiaTypes.RepositoryId{
337+
msg = append(msg, gitopiatypes.NewMsgMultiDeleteTag(h.wallet.Address(), gitopiatypes.RepositoryId{
358338
Id: h.remoteRepository.Owner.Id,
359339
Name: h.remoteRepository.Name,
360340
}, deleteTags))
@@ -368,26 +348,26 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
368348
}
369349

370350
func (h *GitopiaHandler) havePushPermission(walletAddress string) (havePermission bool, err error) {
371-
if h.remoteRepository.Owner.Type == gitopiaTypes.OwnerType_USER {
351+
if h.remoteRepository.Owner.Type == gitopiatypes.OwnerType_USER {
372352
if h.wallet.Address() == h.remoteRepository.Owner.Id {
373353
havePermission = true
374354
}
375-
} else if h.remoteRepository.Owner.Type == gitopiaTypes.OwnerType_DAO {
376-
member, err := h.queryClient.DaoMember(context.Background(), &gitopiaTypes.QueryGetDaoMemberRequest{
355+
} else if h.remoteRepository.Owner.Type == gitopiatypes.OwnerType_DAO {
356+
member, err := h.queryClient.DaoMember(context.Background(), &gitopiatypes.QueryGetDaoMemberRequest{
377357
DaoId: h.remoteRepository.Owner.Id,
378358
UserId: h.wallet.Address(),
379359
})
380360
if err != nil {
381361
return havePermission, err
382362
}
383-
if member.Member.Role == gitopiaTypes.MemberRole_OWNER {
363+
if member.Member.Role == gitopiatypes.MemberRole_OWNER {
384364
havePermission = true
385365
}
386366
}
387367

388368
if !havePermission {
389369
if i, exists := utils.RepositoryCollaboratorExists(h.remoteRepository.Collaborators, h.wallet.Address()); exists {
390-
if h.remoteRepository.Collaborators[i].Permission >= gitopiaTypes.PushBranchPermission {
370+
if h.remoteRepository.Collaborators[i].Permission >= gitopiatypes.PushBranchPermission {
391371
havePermission = true
392372
}
393373
}

0 commit comments

Comments
 (0)