-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
init.go
149 lines (129 loc) · 4.17 KB
/
init.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2017 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libgit
import (
"context"
"os"
"github.com/keybase/client/go/kbfs/libcontext"
"github.com/keybase/client/go/kbfs/libkbfs"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
)
const (
// Debug tag ID for a batched set of git operations under a single
// config.
ctxGitOpID = "GITID"
)
type ctxGitTagKey int
const (
paramKeybaseGitMDServerAddr = "KEYBASE_GIT_MDSERVER_ADDR"
paramKeybaseGitBServerAddr = "KEYBASE_GIT_BSERVER_ADDR"
)
const (
ctxGitIDKey ctxGitTagKey = iota
)
// Params returns a set of default parameters for git-related
// operations, along with a temp directory that should be cleaned
// after the git work is complete.
func Params(kbCtx libkbfs.Context,
storageRoot string, paramsBase *libkbfs.InitParams) (
params libkbfs.InitParams, tempDir string, err error) {
tempDir, err = os.MkdirTemp(storageRoot, libkbfs.GitStorageRootPrefix)
if err != nil {
return libkbfs.InitParams{}, "", err
}
if paramsBase != nil {
params = *paramsBase
} else {
params = libkbfs.DefaultInitParams(kbCtx)
}
params.LogToFile = true
// Set the debug default to true only if the env variable isn't
// explicitly set to a false option.
envDebug := os.Getenv("KBFSGIT_DEBUG")
if envDebug != "0" && envDebug != "false" && envDebug != "no" {
params.Debug = true
}
// This is set to false in docker tests for now, but we need it. So
// override it to true here.
params.EnableJournal = true
params.DiskCacheMode = libkbfs.DiskCacheModeRemote
params.StorageRoot = tempDir
params.Mode = libkbfs.InitSingleOpString
params.TLFJournalBackgroundWorkStatus =
libkbfs.TLFJournalSingleOpBackgroundWorkEnabled
if baddr := os.Getenv(paramKeybaseGitBServerAddr); len(baddr) > 0 {
params.BServerAddr = baddr
}
if mdaddr := os.Getenv(paramKeybaseGitMDServerAddr); len(mdaddr) > 0 {
params.MDServerAddr = mdaddr
}
return params, tempDir, nil
}
// Init initializes a context and a libkbfs.Config for git operations.
// The config should be shutdown when it is done being used.
func Init(ctx context.Context, gitKBFSParams libkbfs.InitParams,
kbCtx libkbfs.Context, keybaseServiceCn libkbfs.KeybaseServiceCn,
defaultLogPath string, vlogLevel string) (
context.Context, libkbfs.Config, error) {
log, err := libkbfs.InitLogWithPrefix(
gitKBFSParams, kbCtx, "git", defaultLogPath)
if err != nil {
return ctx, nil, err
}
// Assign a unique ID to each remote-helper instance, since
// they'll all share the same log.
ctx, err = libcontext.NewContextWithCancellationDelayer(
libkbfs.CtxWithRandomIDReplayable(
ctx, ctxGitIDKey, ctxGitOpID, log))
if err != nil {
return ctx, nil, err
}
log.CDebugf(ctx, "Initialized new git config")
config, err := libkbfs.InitWithLogPrefix(
ctx, kbCtx, gitKBFSParams, keybaseServiceCn, nil, log, "git")
if err != nil {
return ctx, nil, err
}
config.SetVLogLevel(vlogLevel)
// Make any blocks written by via this config charged to the git
// quota.
config.SetDefaultBlockType(keybase1.BlockType_GIT)
err = config.MakeDiskBlockCacheIfNotExists()
if err != nil {
log.CDebugf(ctx, "Couldn't initialize disk cache: %+v", err)
}
return ctx, config, nil
}
func getNewConfig(
ctx context.Context, config libkbfs.Config, kbCtx libkbfs.Context,
kbfsInitParams *libkbfs.InitParams, log logger.Logger) (
newCtx context.Context, gitConfig libkbfs.Config,
tempDir string, err error) {
// Initialize libgit.
params, tempDir, err := Params(kbCtx, config.StorageRoot(), kbfsInitParams)
if err != nil {
return nil, nil, "", err
}
defer func() {
if err != nil {
rmErr := os.RemoveAll(tempDir)
if rmErr != nil {
log.CDebugf(
ctx, "Error cleaning storage dir %s: %+v\n", tempDir, rmErr)
}
}
}()
// Let the init code know it shouldn't try to change the
// global logger settings.
params.LogToFile = false
params.LogFileConfig.Path = ""
newCtx, gitConfig, err = Init(
ctx, params, kbCtx, libkbfs.NewKeybaseServicePassthrough(config), "",
config.VLogLevel())
if err != nil {
return nil, nil, "", err
}
return newCtx, gitConfig, tempDir, nil
}