src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.9k stars 540 forks source link

gitignore not working #1219

Open CalinR opened 5 years ago

CalinR commented 5 years ago

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 commented 5 years ago

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

ncsibra commented 5 years ago

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 commented 5 years ago

Hi @jfontan , any good workaround for this?