Skip to content

Commit

Permalink
Add pushing support with push.go
Browse files Browse the repository at this point in the history
No tests for this, though it might be very easy to test.
  • Loading branch information
vHanda committed May 3, 2022
1 parent 7721546 commit ed3c785
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
23 changes: 23 additions & 0 deletions push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"github.com/ztrue/tracerr"
)

func push(repoPath string) error {
bi, err := fetchBranchInfo(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

if bi.UpstreamBranch == "" || bi.UpstreamRemote == "" {
return nil
}

err, _ = GitCommand(repoPath, []string{"push", bi.UpstreamRemote + "/" + bi.UpstreamBranch})
if err != nil {
return tracerr.Wrap(err)
}

return nil
}
60 changes: 38 additions & 22 deletions rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,12 @@ import (
)

func rebase(repoPath string) error {
repo, err := git.PlainOpen(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

config, err := repo.Config()
if err != nil {
return tracerr.Wrap(err)
}

ref, err := repo.Reference(plumbing.HEAD, false)
bi, err := fetchBranchInfo(repoPath)
if err != nil {
return tracerr.Wrap(err)
}

currentBranchName := ref.Target().Short()
branchConfig := config.Branches[currentBranchName]
if branchConfig == nil {
// No tracking branch, nothing to do
return nil
}

remoteName := branchConfig.Remote
remoteBranchName := branchConfig.Merge.Short()

rebaseErr, _ := GitCommand(repoPath, []string{"rebase", remoteName + "/" + remoteBranchName})
rebaseErr, _ := GitCommand(repoPath, []string{"rebase", bi.UpstreamRemote + "/" + bi.UpstreamBranch})
if rebaseErr != nil {
ra, err := exists(path.Join(repoPath, ".git", "rebase-apply"))
if err != nil {
Expand Down Expand Up @@ -82,3 +62,39 @@ func exists(name string) (bool, error) {
}
return false, err
}

type branchInfo struct {
CurrentBranch string
UpstreamRemote string
UpstreamBranch string
}

func fetchBranchInfo(repoPath string) (branchInfo, error) {
repo, err := git.PlainOpen(repoPath)
if err != nil {
return branchInfo{}, tracerr.Wrap(err)
}

config, err := repo.Config()
if err != nil {
return branchInfo{}, tracerr.Wrap(err)
}

ref, err := repo.Reference(plumbing.HEAD, false)
if err != nil {
return branchInfo{}, tracerr.Wrap(err)
}

currentBranchName := ref.Target().Short()
branchConfig := config.Branches[currentBranchName]
if branchConfig == nil {
// No tracking branch, nothing to do
return branchInfo{CurrentBranch: currentBranchName}, nil
}

return branchInfo{
CurrentBranch: currentBranchName,
UpstreamRemote: branchConfig.Remote,
UpstreamBranch: branchConfig.Merge.Short(),
}, nil
}

0 comments on commit ed3c785

Please sign in to comment.