From 7d7fb3c80b056eb3297d5f901ebba7d5685cea76 Mon Sep 17 00:00:00 2001 From: Neutron Soutmun Date: Mon, 31 Aug 2020 19:16:35 +0700 Subject: [PATCH] Use appropriated one-liner if clause and named return parameter Thanks to `wingyplus` for the suggestions. [1] * Use one-liner if clause for the error checking when appropriated. * Use the named return parameter for the variable that need to be declared at the first of function. --- [1] https://github.com/neutronth/git-sync/commit/cfd4d75 --- cmd/git-sync/main.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 0cd8a6a02..18db1b9f1 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -545,8 +545,7 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string, // Update submodules // NOTE: this works for repo with or without submodules. if submoduleMode != SubmoduleModeOff { - err = updateSubmodules(ctx, worktreePath, depth, submoduleMode) - if err != nil { + if err := updateSubmodules(ctx, worktreePath, depth, submoduleMode); err != nil { return err } } @@ -586,8 +585,8 @@ func updateSubmodules(ctx context.Context, worktreePath string, depth int, submo updateArgs := submoduleUpdateArgs(depth, submoduleMode) submodulesArgs := append([]string{"submodule", "update", "--init"}, updateArgs...) - _, err := runCommand(ctx, worktreePath, *flGitCmd, submodulesArgs...) - if err != nil { + + if _, err := runCommand(ctx, worktreePath, *flGitCmd, submodulesArgs...); err != nil { return err } @@ -624,8 +623,8 @@ func updateSubmodulesWithRemoteTracking(ctx context.Context, worktreePath string log.V(0).Info("updating submodule with remote tracking", "submoduleName", submoduleName, "submodulePath", submodulePath) submoduleRemoteUpdateArgs := append([]string{"submodule", "update", "--remote"}, updateArgs...) submoduleRemoteUpdateArgs = append(submoduleRemoteUpdateArgs, submodulePath) - _, err = runCommand(ctx, worktreePath, *flGitCmd, submoduleRemoteUpdateArgs...) - if err != nil { + + if _, err = runCommand(ctx, worktreePath, *flGitCmd, submoduleRemoteUpdateArgs...); err != nil { return err } @@ -640,8 +639,7 @@ func updateSubmodulesWithRemoteTracking(ctx context.Context, worktreePath string return nil } -func submoduleUpdateArgs(depth int, submoduleMode string) []string { - args := []string{} +func submoduleUpdateArgs(depth int, submoduleMode string) (args []string) { if submoduleMode == SubmoduleModeRecursive { args = append(args, "--recursive") } @@ -649,7 +647,7 @@ func submoduleUpdateArgs(depth int, submoduleMode string) []string { args = append(args, "--depth", strconv.Itoa(depth)) } - return args + return } func submoduleIsUpToDate(ctx context.Context, worktreePath, submodulePath string) (bool, error) {