Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scanpullrequest/scanpullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,19 @@
err = fmt.Errorf("failed to download source branch code. Error: %s", err.Error())
return
}
if err = utils.CreateSyntheticHeadCommit(sourceBranchWd); err != nil {
log.Warn("Failed to create synthetic HEAD commit for source branch: " + err.Error())
err = nil

Check failure on line 136 in scanpullrequest/scanpullrequest.go

View workflow job for this annotation

GitHub Actions / Static-Check

assigned to err, but reassigned without using the value (wastedassign)
}
target := repoConfig.Params.Git.PullRequestDetails.Target
if targetBranchWd, cleanupTarget, err = utils.DownloadRepoToTempDir(scanDetails.Client(), target.Owner, target.Repository, target.Name); err != nil {
err = fmt.Errorf("failed to download target branch code. Error: %s", err.Error())
return
}
if err = utils.CreateSyntheticHeadCommit(targetBranchWd); err != nil {
log.Warn("Failed to create synthetic HEAD commit for target branch: " + err.Error())
err = nil
}
return
}

Expand Down
25 changes: 25 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ func (gm *GitManager) SetAuth(username, token string) *GitManager {
return gm
}

// CreateSyntheticHeadCommit creates an empty commit in an existing .git directory so that
// jgit-based tools (e.g. Gradle versioning plugins) can resolve HEAD without a real clone.
func CreateSyntheticHeadCommit(repoPath string) error {
repo, err := git.PlainOpen(repoPath)
if err != nil {
return fmt.Errorf("failed to open git repository at '%s': %s", repoPath, err.Error())
}
w, err := repo.Worktree()
if err != nil {
return fmt.Errorf("failed to get worktree at '%s': %s", repoPath, err.Error())
}
_, err = w.Commit("init", &git.CommitOptions{
AllowEmptyCommits: true,
Author: &object.Signature{
Name: "frogbot",
Email: "frogbot@jfrog.com",
When: time.Now(),
},
})
if err != nil {
return fmt.Errorf("failed to create synthetic HEAD commit at '%s': %s", repoPath, err.Error())
}
return nil
}

func (gm *GitManager) SetRemoteGitUrl(remoteHttpsGitUrl string) (*GitManager, error) {
// Check if the .git directory exists
dotGitExists, err := fileutils.IsDirExists(git.GitDirName, false)
Expand Down
Loading