Skip to content

Commit

Permalink
feat: Add --git-lfs option to init command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 12, 2025
1 parent 6c4431e commit 621379b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions assets/chezmoi.io/docs/reference/commands/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ existing template data.

Clone the repo with depth *depth*.

### `--git-lfs` *bool*

Run `git lfs pull` after cloning the repo.

### `-g`, `--guess-repo-url` *bool*

Guess the repo URL from the *repo* argument. This defaults to `true`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ sections:
commitMessageTemplateFile:
type: string
description: Commit message template file (relative to source directory)
lfs:
type: bool
description: Run `git lfs pull` after cloning
gitHub:
refreshPeriod:
type: duration
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/gitcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type gitCmdConfig struct {
AutoPush bool `json:"autopush" mapstructure:"autopush" yaml:"autopush"`
CommitMessageTemplate string `json:"commitMessageTemplate" mapstructure:"commitMessageTemplate" yaml:"commitMessageTemplate"`
CommitMessageTemplateFile string `json:"commitMessageTemplateFile" mapstructure:"commitMessageTemplateFile" yaml:"commitMessageTemplateFile"`
LFS bool `json:"lfs" mapstructure:"lfs" yaml:"lfs"`
}

func (c *Config) newGitCmd() *cobra.Command {
Expand Down
12 changes: 10 additions & 2 deletions internal/cmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (c *Config) newInitCmd() *cobra.Command {
initCmd.Flags().VarP(c.init.filter.Exclude, "exclude", "x", "Exclude entry types")
initCmd.Flags().BoolVarP(&c.init.guessRepoURL, "guess-repo-url", "g", c.init.guessRepoURL, "Guess the repo URL")
initCmd.Flags().VarP(c.init.filter.Include, "include", "i", "Include entry types")
initCmd.Flags().BoolVar(&c.Git.LFS, "git-lfs", c.Git.LFS, "Run git pull lfs after cloning")
initCmd.Flags().BoolVar(&c.init.oneShot, "one-shot", c.init.oneShot, "Run in one-shot mode")
initCmd.Flags().BoolVarP(&c.init.purge, "purge", "p", c.init.purge, "Purge config and source directories after running")
initCmd.Flags().BoolVarP(&c.init.purgeBinary, "purge-binary", "P", c.init.purgeBinary, "Purge chezmoi binary after running")
Expand All @@ -129,6 +130,8 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
c.init.purgeBinary = true
}

useBuiltinGit := c.UseBuiltinGit.Value(c.useBuiltinGitAutoFunc)

// If we're not in a working tree then init it or clone it.
gitDirAbsPath := c.WorkingTreeAbsPath.JoinString(git.GitDirName)
switch _, err := c.baseSystem.Stat(gitDirAbsPath); {
Expand All @@ -138,8 +141,6 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
return err
}

useBuiltinGit := c.UseBuiltinGit.Value(c.useBuiltinGitAutoFunc)

if len(args) == 0 {
if useBuiltinGit {
if err := c.builtinGitInit(workingTreeRawPath); err != nil {
Expand Down Expand Up @@ -196,6 +197,13 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
return err
}

if c.Git.LFS && !useBuiltinGit {
args := []string{"lfs", "pull"}
if err := c.run(chezmoi.EmptyAbsPath, c.Git.Command, args); err != nil {
return err
}
}

// Apply.
if c.init.apply {
if err := c.applyArgs(cmd.Context(), c.destSystem, c.DestDirAbsPath, noArgs, applyArgsOptions{
Expand Down
15 changes: 15 additions & 0 deletions internal/cmd/testdata/scripts/initgitlfs.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[windows] skip 'UNIX only'

chmod 755 bin/git

# test that chezmoi init --git-lfs runs git lfs pull after cloning
exec chezmoi init --git-lfs user
cmpenv stdout golden/init

-- bin/git --
#!/bin/sh

echo git $*
-- golden/init --
git clone --recurse-submodules https://github.com/user/dotfiles.git $HOME/.local/share/chezmoi
git lfs pull

0 comments on commit 621379b

Please sign in to comment.