1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-09 09:22:48 +03:00

honour push.default matching config value

This commit is contained in:
Jesse Duffield
2022-03-27 11:22:16 +11:00
parent ae10a5ea88
commit 2b3d457aa4
54 changed files with 196 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
package controllers package controllers
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
@@ -74,13 +75,8 @@ func (self *SyncController) branchCheckedOut(f func(*models.Branch) error) func(
func (self *SyncController) push(currentBranch *models.Branch) error { func (self *SyncController) push(currentBranch *models.Branch) error {
// if we have pullables we'll ask if the user wants to force push // if we have pullables we'll ask if the user wants to force push
if currentBranch.IsTrackingRemote() { if currentBranch.IsTrackingRemote() {
opts := pushOpts{ opts := pushOpts{}
force: false,
upstreamRemote: currentBranch.UpstreamRemote,
upstreamBranch: currentBranch.UpstreamBranch,
}
if currentBranch.HasCommitsToPull() { if currentBranch.HasCommitsToPull() {
opts.force = true
return self.requestToForcePush(opts) return self.requestToForcePush(opts)
} else { } else {
return self.pushAux(opts) return self.pushAux(opts)
@@ -90,21 +86,15 @@ func (self *SyncController) push(currentBranch *models.Branch) error {
return self.pushAux(pushOpts{setUpstream: true}) return self.pushAux(pushOpts{setUpstream: true})
} else { } else {
return self.promptForUpstream(currentBranch, func(upstream string) error { return self.promptForUpstream(currentBranch, func(upstream string) error {
var upstreamBranch, upstreamRemote string upstreamRemote, upstreamBranch, err := self.parseUpstream(upstream)
split := strings.Split(upstream, " ") if err != nil {
if len(split) == 2 { return self.c.Error(err)
upstreamRemote = split[0]
upstreamBranch = split[1]
} else {
upstreamRemote = upstream
upstreamBranch = ""
} }
return self.pushAux(pushOpts{ return self.pushAux(pushOpts{
force: false, setUpstream: true,
upstreamRemote: upstreamRemote, upstreamRemote: upstreamRemote,
upstreamBranch: upstreamBranch, upstreamBranch: upstreamBranch,
setUpstream: true,
}) })
}) })
} }
@@ -117,27 +107,46 @@ func (self *SyncController) pull(currentBranch *models.Branch) error {
// if we have no upstream branch we need to set that first // if we have no upstream branch we need to set that first
if !currentBranch.IsTrackingRemote() { if !currentBranch.IsTrackingRemote() {
return self.promptForUpstream(currentBranch, func(upstream string) error { return self.promptForUpstream(currentBranch, func(upstream string) error {
if err := self.setCurrentBranchUpstream(upstream); err != nil {
return self.c.Error(err)
}
return self.PullAux(PullFilesOptions{Action: action})
})
}
return self.PullAux(PullFilesOptions{Action: action})
}
func (self *SyncController) setCurrentBranchUpstream(upstream string) error {
upstreamRemote, upstreamBranch, err := self.parseUpstream(upstream)
if err != nil {
return err
}
if err := self.git.Branch.SetCurrentBranchUpstream(upstreamRemote, upstreamBranch); err != nil {
if strings.Contains(err.Error(), "does not exist") {
return fmt.Errorf(
"upstream branch %s/%s not found.\nIf you expect it to exist, you should fetch (with 'f').\nOtherwise, you should push (with 'shift+P')",
upstreamRemote, upstreamBranch,
)
}
return err
}
return nil
}
func (self *SyncController) parseUpstream(upstream string) (string, string, error) {
var upstreamBranch, upstreamRemote string var upstreamBranch, upstreamRemote string
split := strings.Split(upstream, " ") split := strings.Split(upstream, " ")
if len(split) != 2 { if len(split) != 2 {
return self.c.ErrorMsg(self.c.Tr.InvalidUpstream) return "", "", errors.New(self.c.Tr.InvalidUpstream)
} }
upstreamRemote = split[0] upstreamRemote = split[0]
upstreamBranch = split[1] upstreamBranch = split[1]
if err := self.git.Branch.SetCurrentBranchUpstream(upstreamRemote, upstreamBranch); err != nil { return upstreamRemote, upstreamBranch, nil
errorMessage := err.Error()
if strings.Contains(errorMessage, "does not exist") {
errorMessage = fmt.Sprintf("upstream branch %s not found.\nIf you expect it to exist, you should fetch (with 'f').\nOtherwise, you should push (with 'shift+P')", upstream)
}
return self.c.ErrorMsg(errorMessage)
}
return self.PullAux(PullFilesOptions{UpstreamRemote: upstreamRemote, UpstreamBranch: upstreamBranch, Action: action})
})
}
return self.PullAux(PullFilesOptions{UpstreamRemote: currentBranch.UpstreamRemote, UpstreamBranch: currentBranch.UpstreamBranch, Action: action})
} }
func (self *SyncController) promptForUpstream(currentBranch *models.Branch, onConfirm func(string) error) error { func (self *SyncController) promptForUpstream(currentBranch *models.Branch, onConfirm func(string) error) error {
@@ -229,6 +238,7 @@ func (self *SyncController) requestToForcePush(opts pushOpts) error {
Title: self.c.Tr.ForcePush, Title: self.c.Tr.ForcePush,
Prompt: self.c.Tr.ForcePushPrompt, Prompt: self.c.Tr.ForcePushPrompt,
HandleConfirm: func() error { HandleConfirm: func() error {
opts.force = true
return self.pushAux(opts) return self.pushAux(opts)
}, },
}) })

View File

@@ -0,0 +1 @@
myfile4

View File

@@ -0,0 +1,2 @@
b82ed4a67bef9ef50807adf409f103ef7b0832ab branch 'master' of ../actual_remote
d3708eeec2b9d69acbe87862330e844e85f77de1 not-for-merge branch 'other_branch' of ../actual_remote

View File

@@ -0,0 +1 @@
ref: refs/heads/master

View File

@@ -0,0 +1 @@
d3708eeec2b9d69acbe87862330e844e85f77de1

View File

@@ -0,0 +1,21 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
email = CI@example.com
name = CI
[push]
default = matching
[remote "origin"]
url = ../actual_remote
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "other_branch"]
remote = origin
merge = refs/heads/other_branch

View File

@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store

View File

@@ -0,0 +1,10 @@
0000000000000000000000000000000000000000 16d8875e19987b16f1991a41fd3f4536d16f7cb4 CI <CI@example.com> 1648340487 +1100 commit (initial): myfile1
16d8875e19987b16f1991a41fd3f4536d16f7cb4 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 commit: myfile2
42d408cffcc087da21115f9ebc29e9765a2beb83 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 checkout: moving from master to other_branch
42d408cffcc087da21115f9ebc29e9765a2beb83 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 checkout: moving from other_branch to master
42d408cffcc087da21115f9ebc29e9765a2beb83 b82ed4a67bef9ef50807adf409f103ef7b0832ab CI <CI@example.com> 1648340487 +1100 commit: myfile3
b82ed4a67bef9ef50807adf409f103ef7b0832ab 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 reset: moving to HEAD^
42d408cffcc087da21115f9ebc29e9765a2beb83 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 checkout: moving from master to other_branch
42d408cffcc087da21115f9ebc29e9765a2beb83 d3708eeec2b9d69acbe87862330e844e85f77de1 CI <CI@example.com> 1648340487 +1100 commit: myfile4
d3708eeec2b9d69acbe87862330e844e85f77de1 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 reset: moving to HEAD^
42d408cffcc087da21115f9ebc29e9765a2beb83 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 checkout: moving from other_branch to master

View File

@@ -0,0 +1,4 @@
0000000000000000000000000000000000000000 16d8875e19987b16f1991a41fd3f4536d16f7cb4 CI <CI@example.com> 1648340487 +1100 commit (initial): myfile1
16d8875e19987b16f1991a41fd3f4536d16f7cb4 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 commit: myfile2
42d408cffcc087da21115f9ebc29e9765a2beb83 b82ed4a67bef9ef50807adf409f103ef7b0832ab CI <CI@example.com> 1648340487 +1100 commit: myfile3
b82ed4a67bef9ef50807adf409f103ef7b0832ab 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 reset: moving to HEAD^

View File

@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 branch: Created from HEAD
42d408cffcc087da21115f9ebc29e9765a2beb83 d3708eeec2b9d69acbe87862330e844e85f77de1 CI <CI@example.com> 1648340487 +1100 commit: myfile4
d3708eeec2b9d69acbe87862330e844e85f77de1 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 reset: moving to HEAD^

View File

@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 fetch origin: storing head
42d408cffcc087da21115f9ebc29e9765a2beb83 b82ed4a67bef9ef50807adf409f103ef7b0832ab CI <CI@example.com> 1648340487 +1100 update by push
b82ed4a67bef9ef50807adf409f103ef7b0832ab 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340489 +1100 update by push

View File

@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340487 +1100 fetch origin: storing head
42d408cffcc087da21115f9ebc29e9765a2beb83 d3708eeec2b9d69acbe87862330e844e85f77de1 CI <CI@example.com> 1648340487 +1100 update by push
d3708eeec2b9d69acbe87862330e844e85f77de1 42d408cffcc087da21115f9ebc29e9765a2beb83 CI <CI@example.com> 1648340489 +1100 update by push

View File

@@ -0,0 +1,2 @@
x<01><>A
<EFBFBD>0@Ѯs<D1AE><73>ʌN'))<29><>c2<63><32>!")<29><><EFBFBD>#t<>y<EFBFBD>c-ei@,<2C><><EFBFBD><01>Č*<2A>XH<58>FAR<41>N){<7B><>O<EFBFBD>Y<EFBFBD><59>s<EFBFBD>i<EFBFBD><69><EFBFBD>8<EFBFBD>s<EFBFBD>^vh<76>V<EFBFBD><56>Z <20><>3r<33>p%Btg='<27><><EFBFBD><EFBFBD>|<7C><><1A>3,<2C>

View File

@@ -0,0 +1,3 @@
x<01><>A
<EFBFBD> E<><45><14> <0B><>ɨJ!<21>C<>
<EFBFBD>& <0B><><EFBFBD><11><><<3C><><EFBFBD>e<EFBFBD>um"^<5E>ɬ<EFBFBD>wi<>bJa<4A>H<EFBFBD>)J&G<>k<><11>@<40>H'<27><>H%?0<>|<06> !Hq<48><71><EFBFBD>ґ_2<5F><32>n<EFBFBD><6E><EFBFBD>Ӭ<EFBFBD>i~<7E>'<27>c<EFBFBD>۲<EFBFBD>{<7B><><EFBFBD><EFBFBD>`<60><>

View File

@@ -0,0 +1 @@
42d408cffcc087da21115f9ebc29e9765a2beb83

View File

@@ -0,0 +1 @@
42d408cffcc087da21115f9ebc29e9765a2beb83

View File

@@ -0,0 +1 @@
42d408cffcc087da21115f9ebc29e9765a2beb83

View File

@@ -0,0 +1 @@
42d408cffcc087da21115f9ebc29e9765a2beb83

View File

@@ -0,0 +1 @@
test1

View File

@@ -0,0 +1 @@
test2

View File

@@ -0,0 +1 @@
ref: refs/heads/master

View File

@@ -0,0 +1,8 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = /Users/jesseduffieldduffield/go/src/github.com/jesseduffield/lazygit/test/integration/forcePushMultiple/./actual

View File

@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store

View File

@@ -0,0 +1,2 @@
x<01><>A
<EFBFBD>0@Ѯs<D1AE><73>ʌN'))<29><>c2<63><32>!")<29><><EFBFBD>#t<>y<EFBFBD>c-ei@,<2C><><EFBFBD><01>Č*<2A>XH<58>FAR<41>N){<7B><>O<EFBFBD>Y<EFBFBD><59>s<EFBFBD>i<EFBFBD><69><EFBFBD>8<EFBFBD>s<EFBFBD>^vh<76>V<EFBFBD><56>Z <20><>3r<33>p%Btg='<27><><EFBFBD><EFBFBD>|<7C><><1A>3,<2C>

View File

@@ -0,0 +1,3 @@
x<01><>A
<EFBFBD> E<><45><14> <0B><>ɨJ!<21>C<>
<EFBFBD>& <0B><><EFBFBD><11><><<3C><><EFBFBD>e<EFBFBD>um"^<5E>ɬ<EFBFBD>wi<>bJa<4A>H<EFBFBD>)J&G<>k<><11>@<40>H'<27><>H%?0<>|<06> !Hq<48><71><EFBFBD>ґ_2<5F><32>n<EFBFBD><6E><EFBFBD>Ӭ<EFBFBD>i~<7E>'<27>c<EFBFBD>۲<EFBFBD>{<7B><><EFBFBD><EFBFBD>`<60><>

View File

@@ -0,0 +1,3 @@
# pack-refs with: peeled fully-peeled sorted
42d408cffcc087da21115f9ebc29e9765a2beb83 refs/heads/master
42d408cffcc087da21115f9ebc29e9765a2beb83 refs/heads/other_branch

View File

@@ -0,0 +1 @@
42d408cffcc087da21115f9ebc29e9765a2beb83

View File

@@ -0,0 +1 @@
42d408cffcc087da21115f9ebc29e9765a2beb83

View File

@@ -0,0 +1 @@
{"KeyEvents":[{"Timestamp":591,"Mod":0,"Key":256,"Ch":80},{"Timestamp":1207,"Mod":0,"Key":13,"Ch":13},{"Timestamp":1990,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":272,"Height":74}]}

View File

@@ -0,0 +1,54 @@
#!/bin/sh
set -e
set -e
cd $1
git init
git config user.email "CI@example.com"
git config user.name "CI"
git config push.default matching
echo test1 > myfile1
git add .
git commit -am "myfile1"
echo test2 > myfile2
git add .
git commit -am "myfile2"
git checkout -b other_branch
git checkout master
cd ..
git clone --bare ./actual actual_remote
cd actual
git remote add origin ../actual_remote
git fetch origin
git branch --set-upstream-to=origin/master master
git branch --set-upstream-to=origin/other_branch other_branch
echo test3 > myfile3
git add .
git commit -am "myfile3"
git push origin master
git reset --hard HEAD^
git checkout other_branch
echo test4 > myfile4
git add .
git commit -am "myfile4"
git push origin other_branch
git reset --hard HEAD^
git checkout master
# at this point, both branches have diverged from their remote counterparts, meaning if you
# attempt to push either, it'll ask if you want to force push.

View File

@@ -0,0 +1,4 @@
{
"description": "Force push to multiple branches because the user hasn't configured git to do otherwise",
"speed": 10
}