Skip to content
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

AddWithOptions(All:true) doesn't respect .gitignore #597

Open
h4ck3rk3y opened this issue Oct 11, 2022 · 4 comments
Open

AddWithOptions(All:true) doesn't respect .gitignore #597

h4ck3rk3y opened this issue Oct 11, 2022 · 4 comments

Comments

@h4ck3rk3y
Copy link

Here is the repo I'll be using as an example,
image

Here's the code that I'm running to test the AddWithOptions(All:true)

package main

import (
	"fmt"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing/object"
	"time"
)

func main() {
	repository, err := git.PlainOpen("../testrepo")
	if err != nil {
		panic("this shouldnt happen")
	}

	worktree, err := repository.Worktree()
	fmt.Println(worktree.Status())

	err = worktree.AddWithOptions(&git.AddOptions{All: true})

	commitMsg := fmt.Sprintf("testing add with options")
	_, err = worktree.Commit(commitMsg, &git.CommitOptions{
		Author: &object.Signature{
			Name:  "foo",
			Email: "foo@bar.com",
			When:  time.Now(),
		},
	})
}

After running main.go this is what I get , even though the worker tree is empty (nil output in the first screenshot)
image

The gitignore contains this,
image

Is this expected behavior? How do I force AddWithOptions(all:true) to respect .gigignore?

@h4ck3rk3y
Copy link
Author

h4ck3rk3y commented Oct 12, 2022

Have fixed the .gitignore too

# gyani @ Gyanendras-MacBook-Pro in ~/work/testrepo on git:master o [18:43:57] 
$ cat .gitignore
/unusedfolder

I hadn't attached the screenshot from the execution, here it is
The .gitignore contains unusedfolder the worktree says its nil but for some reason it still gets added.

image

@jghal
Copy link

jghal commented Oct 20, 2022

I have a gitignore as follows

$ cat .gitignore
# Local .terraform directories
.terraform/

# .tfplan files
*.tfplan
*.tfplan.*

I'm not seeing the gitignore file being used at all in either Add() or AddWithOptions() .

In Add() it's explicitly passing an empty list of excludes and then on line 277 it skips building the gitignore object.

In AddWithOptions() it's referencing the Worktree's Excludes field. But when I step through my code in debugger, I see after a call of w, err := repo.Worktree() that w.Excludes is empty

image

so once again the doAddDirectory() function skips over creating the gitignore object.

I'm now stepping through my Repo.PlainOpen() call to see if it ever attempts to read my gitignore.

@jghal
Copy link

jghal commented Oct 20, 2022

Adding my ignore patterns to the Worktree explicitly before calling AddWithOptions() like this seems to work

	// Add the ignore patterns that aren't benig read from the .gitignore file
	w.Excludes = append(w.Excludes, gitignore.ParsePattern(".terraform/", nil))
	w.Excludes = append(w.Excludes, gitignore.ParsePattern("*.tfplan", nil))
	w.Excludes = append(w.Excludes, gitignore.ParsePattern("*.tfplan.*", nil))

edit: I also have the set All: true in the AddOptions, otherwise it just calls the Add() function instead of doAdd() and we're back to the hard coded list of ignore patterns.

Which is pretty lame, it should really be reading my gitignore file.

@Silthus
Copy link

Silthus commented Nov 3, 2022

Here is a little snippet that parses an existing .gitignore and adds everything to the excludes list. This does not work with nested .gitignore files however.

func AddGitignoreToWorktree(wt *git.Worktree, repositoryPath string) {
	readFile, err := os.Open(filepath.Join(repositoryPath, ".gitignore"))
	defer readFile.Close()

	if err != nil {
		fmt.Println("Failed to read .gitignore: %s", err.Error())
		return
	}

	fileScanner := bufio.NewScanner(readFile)
	fileScanner.Split(bufio.ScanLines)

	for fileScanner.Scan() {
		ignorePattern := fileScanner.Text()
		fmt.Println("add %s from .gitignore to ignore list", ignorePattern)
		wt.Excludes = append(wt.Excludes, gitignore.ParsePattern(ignorePattern, nil))
	}
}

Use it just before calling worktree.AddWithOptions(...).

AddGitignoreToWorktree(worktree, repo.Path())

fmt.Println(fmt.Sprint(worktree.Status()))
if err = worktree.AddWithOptions(&git.AddOptions{All: true}); err != nil {
  return "", err
}

Is this behavior intentional or are there contributions accepted to fix it? Maybe with a new parameter?

git.AddOptions{
  All: true, 
  ExcludeGitignore: true, // defaults to false?
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants