Add commit graph demo

This commit is contained in:
Jesse Duffield 2023-08-07 10:53:42 +10:00
parent f1753f36c8
commit 8dd517870d
4 changed files with 182 additions and 0 deletions

View file

@ -472,3 +472,26 @@ func Encrypt(key []byte, plaintext []byte) ([]byte, error) {
}
`,
}
var RandomBranchNames = []string{
"hotfix/fix-bug",
"r-u-fkn-srs",
"iserlohn-build",
"hotfix/fezzan-corridor",
"terra-investigation",
"quash-rebellion",
"feature/attack-on-odin",
"feature/peace-time",
"feature/repair-brunhild",
"feature/iserlohn-backdoor",
"bugfix/resolve-crash",
"enhancement/improve-performance",
"experimental/new-feature",
"release/v1.0.0",
"release/v2.0.0",
"chore/update-dependencies",
"docs/add-readme",
"refactor/cleanup-code",
"style/update-css",
"test/add-unit-tests",
}

View file

@ -3,10 +3,14 @@ package components
import (
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"github.com/jesseduffield/lazygit/pkg/utils"
)
// this is for running shell commands, mostly for the sake of setting up the repo
@ -124,6 +128,10 @@ func (self *Shell) NewBranch(name string) *Shell {
return self.RunCommand([]string{"git", "checkout", "-b", name})
}
func (self *Shell) NewBranchFrom(name string, from string) *Shell {
return self.RunCommand([]string{"git", "checkout", "-b", name, from})
}
func (self *Shell) Checkout(name string) *Shell {
return self.RunCommand([]string{"git", "checkout", name})
}
@ -152,6 +160,10 @@ func (self *Shell) EmptyCommit(message string) *Shell {
return self.RunCommand([]string{"git", "commit", "--allow-empty", "-m", message})
}
func (self *Shell) EmptyCommitDaysAgo(message string, daysAgo int) *Shell {
return self.RunCommand([]string{"git", "commit", "--allow-empty", "--date", fmt.Sprintf("%d days ago", daysAgo), "-m", message})
}
func (self *Shell) Revert(ref string) *Shell {
return self.RunCommand([]string{"git", "revert", ref})
}
@ -223,6 +235,73 @@ func (self *Shell) CreateNCommitsWithRandomMessages(n int) *Shell {
return self
}
// This creates a repo history of commits
// It uses a branching strategy where each feature branch is directly branched off
// of the master branch
// Only to be used in demos
func (self *Shell) CreateRepoHistory() *Shell {
authors := []string{"Yang Wen-li", "Siegfried Kircheis", "Paul Oberstein", "Oscar Reuenthal", "Fredrica Greenhill"}
numAuthors := 5
numBranches := 10
numInitialCommits := 20
maxCommitsPerBranch := 5
// Each commit will happen on a separate day
repoStartDaysAgo := 100
totalCommits := 0
// Generate commits
for i := 0; i < numInitialCommits; i++ {
author := authors[i%numAuthors]
commitMessage := RandomCommitMessages[totalCommits%len(RandomCommitMessages)]
self.SetAuthor(author, "")
self.EmptyCommitDaysAgo(commitMessage, repoStartDaysAgo-totalCommits)
totalCommits++
}
// Generate branches and merges
for i := 0; i < numBranches; i++ {
// We'll have one author creating all the commits in the branch
author := authors[i%numAuthors]
branchName := RandomBranchNames[i%len(RandomBranchNames)]
// Choose a random commit within the last 20 commits on the master branch
lastMasterCommit := totalCommits - 1
commitOffset := rand.Intn(utils.Min(lastMasterCommit, 5)) + 1
// Create the feature branch and checkout the chosen commit
self.NewBranchFrom(branchName, fmt.Sprintf("master~%d", commitOffset))
numCommitsInBranch := rand.Intn(maxCommitsPerBranch) + 1
for j := 0; j < numCommitsInBranch; j++ {
commitMessage := RandomCommitMessages[totalCommits%len(RandomCommitMessages)]
self.SetAuthor(author, "")
self.EmptyCommitDaysAgo(commitMessage, repoStartDaysAgo-totalCommits)
totalCommits++
}
self.Checkout("master")
prevCommitterDate := os.Getenv("GIT_COMMITTER_DATE")
prevAuthorDate := os.Getenv("GIT_AUTHOR_DATE")
commitDate := time.Now().Add(time.Duration(totalCommits-repoStartDaysAgo) * time.Hour * 24)
os.Setenv("GIT_COMMITTER_DATE", commitDate.Format(time.RFC3339))
os.Setenv("GIT_AUTHOR_DATE", commitDate.Format(time.RFC3339))
// Merge branch into master
self.RunCommand([]string{"git", "merge", "--no-ff", branchName, "-m", fmt.Sprintf("Merge %s into master", branchName)})
os.Setenv("GIT_COMMITTER_DATE", prevCommitterDate)
os.Setenv("GIT_AUTHOR_DATE", prevAuthorDate)
}
return self
}
// Creates a commit with a random file
// Only to be used in demos
func (self *Shell) RandomChangeCommit(message string) *Shell {

View file

@ -0,0 +1,79 @@
package demo
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var CommitGraph = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Show commit graph",
ExtraCmdArgs: []string{"log"},
Skip: false,
IsDemo: true,
SetupConfig: func(config *config.AppConfig) {
config.UserConfig.Gui.NerdFontsVersion = "3"
config.UserConfig.Gui.AuthorColors = map[string]string{
"Fredrica Greenhill": "#fb5aa3",
"Oscar Reuenthal": "#86c82f",
"Paul Oberstein": "#ffd500",
"Siegfried Kircheis": "#fe7e11",
"Yang Wen-li": "#8e3ccb",
}
},
SetupRepo: func(shell *Shell) {
shell.CreateRepoHistory()
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.SetCaptionPrefix("View commit log")
t.Wait(1000)
t.Views().Commits().
IsFocused().
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100).
SelectNextItem().
Wait(100)
},
})

View file

@ -93,6 +93,7 @@ var tests = []*components.IntegrationTest{
demo.Bisect,
demo.CherryPick,
demo.CommitAndPush,
demo.CommitGraph,
demo.CustomCommand,
demo.CustomPatch,
demo.Filter,