From 115484e8ad8fc7399f8e98ebc146fce1552944e6 Mon Sep 17 00:00:00 2001 From: Chris Darroch Date: Wed, 31 Jan 2024 13:56:32 -0800 Subject: [PATCH] commands,t: report invalid ref in push command Unlike the "git lfs fetch" command, the "git lfs push" command does not currently report an error if an invalid ref is provided as a command-line argument, so we update the command to output an error message in this situation, and we add a test to confirm the new behaviour works as expected. Note that in contrast to the "git lfs fetch" command's older implementation of this type of check, we simply report an error message when an invalid ref is encountered, rather than calling our Panic() function and also writing a stack trace to a log file. This approach is more in keeping with how many of our commands now handle similar error conditions; e.g., the "git lfs fsck" and "git lfs migrate" commands call ExitWithError() rather than Panic() when refs cannot be resolved. Co-authored-by: Philip Peterson --- commands/command_push.go | 3 +++ t/t-push.sh | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/commands/command_push.go b/commands/command_push.go index e620823eda..6a73498807 100644 --- a/commands/command_push.go +++ b/commands/command_push.go @@ -157,6 +157,9 @@ func lfsPushRefs(refnames []string, pushAll bool) ([]*git.RefUpdate, error) { refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), ref, nil) } else { ref := &git.Ref{Name: name, Type: git.RefTypeOther, Sha: name} + if _, err := git.ResolveRef(name); err != nil { + return nil, errors.New(tr.Tr.Get("Invalid ref argument: %v", name)) + } refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), ref, nil) } } diff --git a/t/t-push.sh b/t/t-push.sh index 28d6d7434f..2be60ef5ff 100755 --- a/t/t-push.sh +++ b/t/t-push.sh @@ -38,6 +38,16 @@ begin_test "push with tracked ref" ) end_test +begin_test "push with invalid ref" +( + set -e + push_repo_setup "push-invalid-branch-required" + + git lfs push origin jibberish >push.log 2>&1 && exit 1 + grep "Invalid ref argument" push.log +) +end_test + begin_test "push with bad ref" ( set -e