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

suggestion: use a native golang git library instead of binary #10

Closed
DataHearth opened this issue Jun 15, 2022 · 8 comments · Fixed by #13
Closed

suggestion: use a native golang git library instead of binary #10

DataHearth opened this issue Jun 15, 2022 · 8 comments · Fixed by #13
Labels
refactor Internal changes that improve code quality

Comments

@DataHearth
Copy link
Collaborator

Hey! I've been browsing your code to understand how to build up sema and I came across the git section in agent/agent.go.

I think using a native golang git library would be a great addition as you'll abstract sema from the git binary (which may differ from a system to another).

I've been using this library (which is listed on the git-scm website). It's a great and efficient library.
This should be simple to implement as you don't extensively rely on git commands.

@sharpvik sharpvik added the refactor Internal changes that improve code quality label Jun 15, 2022
@sharpvik sharpvik assigned sharpvik and unassigned sharpvik Jun 21, 2022
@sharpvik sharpvik linked a pull request Jun 27, 2022 that will close this issue
@sharpvik
Copy link
Owner

That's a great suggestion! However, I cannot figure out how to properly implement the git add . operation that is so dear to my heart. If you delete a file in a repo, running

// `sema -a` will result in the following:
a.workTree.AddGlob(".")

will not actually stage that deletion (at least on my computer). Please see this file to review my failed attempt.

I have created this PR to work on the issue and I'd really appreciate your help with this one since you already have experience with the library.

Have you got any spare time to help?

@DataHearth
Copy link
Collaborator Author

DataHearth commented Jun 27, 2022

Hmmm, so the problem is still there... I've found an already opened issue relating this problem. It also has a PR but it hasn't beed merged yet...
I'm gonna ping the PR and the issue.

My workaround was to use (unfortunatly) the git binary just for this operation.
BUT, I've found this comment giving a good workaround. I haven't tested yet.

edit: if you want to check out what I did => https://github.com/DataHearth/config-mapper/blob/main/internal/git/git.go .
It is not really advanced but it can give you an idea on how I built my cli around it. I'm gonna test the workaround tonight or tomorrow.

@DataHearth
Copy link
Collaborator Author

Ok, it seems to work :). Here's how I did it

w, err := r.repository.Worktree()
if err != nil {
	return err
}

status, err := w.Status()
if err != nil {
	return err
}

for file := range status {
    _, err = w.Add(file)
    if err != nil {
        return err
    }
}

You first get your working tree then get its status (equivalent to git status). Then you loop through each file and add them one by one. After, you should be good to go :)

@sharpvik
Copy link
Owner

Thanks so much for this, @DataHearth. I've rewritten most of the functionality by now. Last question (I hope): is there a way to enact git push? I don't seem to find a function with that name.

@DataHearth
Copy link
Collaborator Author

Sure ! You can use your repository to push changes

repo, err := git.PlainOpen(".")
...
// add your thing, status, worktree, etc

err := repo.Push(&git.PushOptions{})

There you go :)

@sharpvik
Copy link
Owner

What about authentication, @DataHearth? When I used this method, I get

ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

Am I supposed to parse git configs myself to get this to work?

@DataHearth
Copy link
Collaborator Author

Hey, I encoutered another issue (but quite similar to this one actually) a while ago and resolved it using this issue go-git/go-git#411

@sharpvik
Copy link
Owner

I tried this fix, @DataHearth, but it didn't do much for me on MacOS. The error message is still the same.

It looks like the rest of the functionality is in order and works fine with go-git so here's what I'm going to do: I'm gonna merge this pull request without the changes to the --push implementation. Sema will partially still rely on the git command to perform push operations.

I'll open an issue just for the push with go-git and I would welcome you to open a PR with a proposition for a stable fix for this. Then I'll test it on MacOS and Linux and if it works, we'll merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Internal changes that improve code quality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants