Skip to content

Commit 7bec250

Browse files
committed
init: exit with error if route already exists
If 'init' tries to initialize a new route, but that route already exists on disk (enabled or not), exit with an error. Without this explicit check, the initialization will fail anyway (specifically, the bare repo clone), but the call to 'RepositoryProvider.CreateRepository()' will enable the existing route even if it was disabled. To avoid that unintended behavior and provide clearer information to users, explicitly check for existing routes before 'CreateRepository()' and exit if the route already exists. Signed-off-by: Victoria Dye <[email protected]>
1 parent 5530ec2 commit 7bec250

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

cmd/git-bundle-server/init.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ func (i *initCmd) Run(ctx context.Context, args []string) error {
5353
bundleProvider := utils.GetDependency[bundles.BundleProvider](ctx, i.container)
5454
gitHelper := utils.GetDependency[git.GitHelper](ctx, i.container)
5555

56+
// First, check whether route already exists (enabled or not). If it does,
57+
// exit with an error.
58+
allRepos, err := repoProvider.ReadRepositoryStorage(ctx)
59+
if err != nil {
60+
return i.logger.Errorf(ctx, "could not load existing routes: %w", err)
61+
} else if _, ok := allRepos[*route]; ok {
62+
return i.logger.Errorf(ctx, "route '%s' already exists; if you want to "+
63+
"overwrite the route, delete it with 'git-bundle-server delete' "+
64+
"then re-run this command", *route)
65+
}
66+
67+
// Create the new route
5668
repo, err := repoProvider.CreateRepository(ctx, *route)
5769
if err != nil {
5870
return i.logger.Error(ctx, err)

0 commit comments

Comments
 (0)