-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scan PR - Add option to scan source and most common ancestor target c… #763
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |||||
"errors" | ||||||
"fmt" | ||||||
"net/http" | ||||||
// "os/exec" | ||||||
"regexp" | ||||||
"strings" | ||||||
"time" | ||||||
|
@@ -154,6 +155,64 @@ func (gm *GitManager) Checkout(branchName string) error { | |||||
return nil | ||||||
} | ||||||
|
||||||
func (gm *GitManager) CheckoutToHash(hash, targetBranchWd string) error { | ||||||
log.Debug("Running git fetch...") | ||||||
if err := gm.Fetch(); err != nil { | ||||||
return err | ||||||
} | ||||||
log.Debug("Running git checkout to hash:", hash) | ||||||
if err := gm.createBranchAndCheckoutToHash(hash, false); err != nil { | ||||||
return fmt.Errorf("'git checkout %s' failed with error: %s", hash, err.Error()) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func (gm *GitManager) Fetch() error { | ||||||
log.Debug("Running git fetch...") | ||||||
err := gm.localGitRepository.Fetch(&git.FetchOptions{ | ||||||
RemoteName: gm.remoteName, | ||||||
RemoteURL: gm.remoteGitUrl, | ||||||
Auth: gm.auth, | ||||||
}) | ||||||
if err != nil && err != git.NoErrAlreadyUpToDate { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens when the error is NoErrAlreadyUpToDate? will the fetch is still performed when we got this error? |
||||||
return fmt.Errorf("git fetch failed with error: %s", err.Error()) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func (gm *GitManager) GetMostCommonAncestorHash(baseBranch, headBranch string) (string, error) { | ||||||
// Get the commit of the base branch | ||||||
baseCommitHash, err := gm.localGitRepository.ResolveRevision(plumbing.Revision(baseBranch)) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
baseCommit, err := gm.localGitRepository.CommitObject(*baseCommitHash) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
// Get the commit of the head branch | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Its just a bit more clear term than "head branch" |
||||||
headCommitHash, err := gm.localGitRepository.ResolveRevision(plumbing.Revision(headBranch)) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
headCommit, err := gm.localGitRepository.CommitObject(*headCommitHash) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
// Get the most common ancestor | ||||||
log.Debug(fmt.Sprintf("Finding common ancestor between %s and %s...", baseBranch, headBranch)) | ||||||
ancestorCommit, err := baseCommit.MergeBase(headCommit) | ||||||
if err != nil { | ||||||
return "", err | ||||||
} | ||||||
if len(ancestorCommit) == 0 { | ||||||
return "", fmt.Errorf("no common ancestor found for %s and %s", baseBranch, headBranch) | ||||||
} else if len(ancestorCommit) > 1 { | ||||||
return "", fmt.Errorf("more than one common ancestor found for %s and %s", baseBranch, headBranch) | ||||||
} | ||||||
return ancestorCommit[0].Hash.String(), nil | ||||||
} | ||||||
|
||||||
func (gm *GitManager) Clone(destinationPath, branchName string) error { | ||||||
if gm.dryRun { | ||||||
// "Clone" the repository from the testdata folder | ||||||
|
@@ -201,6 +260,26 @@ func (gm *GitManager) CreateBranchAndCheckout(branchName string, keepLocalChange | |||||
return err | ||||||
} | ||||||
|
||||||
func (gm *GitManager) createBranchAndCheckoutToHash(hash string, keepLocalChanges bool) error { | ||||||
var checkoutConfig *git.CheckoutOptions | ||||||
if keepLocalChanges { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the single usage of this new function you pass keepLocalChanges=false, so why do we need to even refer to that in this function? |
||||||
checkoutConfig = &git.CheckoutOptions{ | ||||||
Hash: plumbing.NewHash(hash), | ||||||
Keep: true, | ||||||
} | ||||||
} else { | ||||||
checkoutConfig = &git.CheckoutOptions{ | ||||||
Hash: plumbing.NewHash(hash), | ||||||
Force: true, | ||||||
} | ||||||
} | ||||||
worktree, err := gm.localGitRepository.Worktree() | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
return worktree.Checkout(checkoutConfig) | ||||||
} | ||||||
|
||||||
func (gm *GitManager) createBranchAndCheckout(branchName string, create bool, keepLocalChanges bool) error { | ||||||
var checkoutConfig *git.CheckoutOptions | ||||||
if keepLocalChanges { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate debug log, as similar one exists in Fetch itself