Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

gitignore not working #1219

Open
CalinR opened this issue Sep 12, 2019 · 3 comments · Fixed by gleich/texsch#5, fhs/gig#2, b4nst/turbogit#26, phamdt/adminiutiae#7 or jlucktay/stack#113
Open

gitignore not working #1219

CalinR opened this issue Sep 12, 2019 · 3 comments · Fixed by gleich/texsch#5, fhs/gig#2, b4nst/turbogit#26, phamdt/adminiutiae#7 or jlucktay/stack#113

Comments

@CalinR
Copy link

CalinR commented Sep 12, 2019

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: "johndoe@email.com",
			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!

@jfontan
Copy link
Contributor

jfontan commented Sep 23, 2019

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:

func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {

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

@ncsibra
Copy link

ncsibra commented Nov 28, 2019

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.

@ljvmiranda921
Copy link

Hi @jfontan , any good workaround for this?

jk2K added a commit to jk2K/go-git-1 that referenced this issue Jun 10, 2020
mcuadros added a commit to go-git/go-git that referenced this issue Jun 13, 2020
feat: add file with using .gitignore, fixed src-d/go-git#1219
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.