go-git icon indicating copy to clipboard operation
go-git copied to clipboard

gitignore not working

Open CalinR opened this issue 4 years ago • 3 comments

Hey all, I've been trying to get a gitignore to work with this package and I cant seem to get it to work. The gitignore is in the root of the repository.

I'm doing the following. Am I missing something?

	pushOptions := &git.PushOptions{
		RemoteName: "origin",
		Progress:   os.Stdout,
	}

	if client.Auth != nil {
		pushOptions.Auth = client.Auth
	}

	path := fmt.Sprintf("./_tmp/%s", client.Name)
	r, err := git.PlainOpen(path)

	if err != nil {
		return err
	}

	w, err := r.Worktree()

	if err != nil {
		return err
	}

	_, err = w.Add(".")
	if err != nil {
		return err
	}

	status, _ := w.Status()

	if len(status) == 0 {
	 	return nil
	/}

	_, err = w.Commit("downloads latest changes", &git.CommitOptions{
		All: false,
		Author: &object.Signature{
			Name:  "John Doe",
			Email: "[email protected]",
			When:  time.Now(),
		},
	})
	if err != nil {
		return err
	}

	err = r.Push(pushOptions)
	if err != nil {
		return err
	}

	return nil

I've debugged the worktree status file, and diffStagingWithWorktree seems to be ignoring files properly when I run the w.Add method. But when I run commit, it commits all files that should have been ignored.

Any help would be greatly appreciated! Thanks in advance!

CalinR avatar Sep 12 '19 01:09 CalinR

AFAIK gitignore is only used for worktree.Status(). You'll have to do the filtering yourself. As an example the code that does the filtering for status is here:

https://github.com/src-d/go-git/blob/25e9f61108d7097d6614872b4d65e15f4cb581fa/worktree_status.go#L143

It would be nice to have this functionality baked in Add and AddGlob. Related to #1201

jfontan avatar Sep 23 '19 12:09 jfontan

I'm facing the same problem, using this lib to make changes on multiple git repo and it sometimes using the gitignore file in the root and sometimes not and pushing 500+ extra files, which is really annoying.

ncsibra avatar Nov 28 '19 13:11 ncsibra

Hi @jfontan , any good workaround for this?

ljvmiranda921 avatar Dec 02 '19 16:12 ljvmiranda921