Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
62 changes: 53 additions & 9 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ func isDirectoryAGitRepository(dir string) (bool, error) {
return info != nil, err
}

func isBareRepo(osCommand *oscommands.OSCommand) (bool, error) {
res, err := osCommand.Cmd.New("git rev-parse --is-bare-repository").DontLog().RunWithOutput()
Copy link
Collaborator

@mark2185 mark2185 Aug 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jesseduffield should we somehow conflate this isBareRepo and this one?

Currently I think the one from recent_repos_panel isn't reachable from app.go, but maybe it's better to make it public rather than have a duplicate?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, let's make the existing one public like you say

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would that look, and do you want that implemented in this PR? Also how do I go about accessing the existing function?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would that look, and do you want that implemented in this PR?

It seems like a trivial change, I see no reason why to postpone it.

Also how do I go about accessing the existing function?

Should be accessible through app.IsBareRepo once you make it public (capitalize it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a go.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say we wait for Jesse to see if he can deus ex machina this pickle.

I also say we extract it to something like pkg/commands/git_commands/misc.go, throw in IsDirectoryAGitRepository as well and call it a day.

We'll see!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meaaaaaan that's not even a bad idea per-se. But we can wait for Jesse to comment on this one. Go is a real pain sometimes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the app package can depend on the commands package (which contains git.go) but not vice-versa. I see two approaches we can take here:

  1. make it so that we can instantiate the git struct (in git.go) without having to do the validation stuff that currently happens there.
  2. Move the code we've got here into pkg/commands/git_commands/status.go Like so:
func (self *StatusCommands) IsBareRepo() (bool, error) {
	return IsBareRepo(self.os)
}

func IsBareRepo(osCommand *oscommands.OSCommand) (bool, error) {
	res, err := osCommand.Cmd.New("git rev-parse --is-bare-repository").DontLog().RunWithOutput()
	if err != nil {
		return false, err
	}

	// The command returns output with a newline, so we need to strip
	return strconv.ParseBool(strings.TrimSpace(res))
}

Then in app.go we call IsBareRepo, passing in our os struct.

I would prefer option 1 but that deserves its own PR, so I'm happy to go with option 2 for now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the go-git method is very fast btw because it's just checking if a value is nil, but this function is only called when you want to view recent repos so it's not a hot path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be that sorted, took me a while to figure out how to import the function lol.


if err != nil {
return false, err
}

// The command returns output with a newline, so we need to strip
return strconv.ParseBool(strings.TrimSpace(res))
}

func openRecentRepo(app *App) bool {
for _, repoDir := range app.Config.GetAppState().RecentRepos {
if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo {
if err := os.Chdir(repoDir); err == nil {
return true
}
}
}

return false
}

func (app *App) setupRepo() (bool, error) {
if err := app.validateGitVersion(); err != nil {
return false, err
Expand All @@ -167,6 +190,7 @@ func (app *App) setupRepo() (bool, error) {
if err != nil {
return false, err
}

if isRepo, err := isDirectoryAGitRepository(cwd); isRepo {
return false, err
}
Expand All @@ -193,23 +217,43 @@ func (app *App) setupRepo() (bool, error) {
}

if !shouldInitRepo {
// check if we have a recent repo we can open
for _, repoDir := range app.Config.GetAppState().RecentRepos {
if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo {
if err := os.Chdir(repoDir); err == nil {
return true, nil
}
}
// Attempt to open a recent repo, exit if no repo could be opened
if didOpenRepo := openRecentRepo(app); !didOpenRepo {
fmt.Println(app.Tr.NoRecentRepositories)
os.Exit(1)
}

fmt.Println(app.Tr.NoRecentRepositories)
os.Exit(1)
return true, nil
}
if err := app.OSCommand.Cmd.New("git init " + initialBranch).Run(); err != nil {
return false, err
}
}

// Run this afterward so that the previous repo creation steps can run without this interfering
if isBare, err := isBareRepo(app.OSCommand); isBare {
if err != nil {
return false, err
}

fmt.Print(app.Tr.BareRepo)

response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
shouldOpenRecent := strings.Trim(response, " \r\n") == "y"

if shouldOpenRecent {
if didOpenRepo := openRecentRepo(app); !didOpenRepo {
fmt.Println(app.Tr.NoRecentRepositories)
os.Exit(1)
}

// We managed to open a recent repo, continue as usual
return true, nil
} else {
os.Exit(0)
}
}

return false, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ type TranslationSet struct {
DiscardFileChangesPrompt string
DisabledForGPG string
CreateRepo string
BareRepo string
InitialBranch string
NoRecentRepositories string
AutoStashTitle string
Expand Down Expand Up @@ -896,6 +897,7 @@ func EnglishTranslationSet() TranslationSet {
DiscardFileChangesPrompt: "Are you sure you want to discard this commit's changes to this file? If this file was created in this commit, it will be deleted",
DisabledForGPG: "Feature not available for users using GPG",
CreateRepo: "Not in a git repository. Create a new git repository? (y/n): ",
BareRepo: "You've attempted to open Lazygit in a bare repo but Lazygit does not yet support bare repos. Open most recent repo? (y/n) ",
InitialBranch: "Branch name? (leave empty for git's default): ",
NoRecentRepositories: "Must open lazygit in a git repository. No valid recent repositories. Exiting.",
AutoStashTitle: "Autostash?",
Expand Down