1

TLDR

I've been scouring the package Docs & the official Examples and can't figure out how to describe adding a submodule using go-git. I'm trying to git submodule add <url> <submod_name>, as well as configure it for sparse checkout

Goal

I want to convert the following list using golang's go-git package. My overall goal is to have a sparsely checked out submodule, which will be from Repo-A, then the overall repo will be for Repo-B. I'm trying to source content from Repo-A, make some edits, and reorganize, then push the results to Repo-B. All that I've included below is the first step, specifically for how to configure this repo+submod from scratch.

git init
git remote add -f <remote_name> <repo_url>
git clone --depth=1 --no-checkout <submod_repo_url> <submod_name>
git submodule add <submod_repo_url> <submod_name>

  • Not sure how to call this, I'm thinking it may be possible with edits to the .git directory directly, but that seems messy. Any explanations or suggestions would be appreciated.

git submodule absorbgitdirs

  • Not sure if this is even needed, I couldn't find a good description of what it does, but it seems to not cause a problem either way?

git -C pinkbrain config core.sparseCheckout true (NOTE: will only apply to the submodule)

  • Not sure how to make this work, since its a .git config, but only for the submodule. Maybe just create a new git.PlainOpen() object and access its working tree that way?

echo "cir-1" >>.git/modules/<submod_name>/info/sparse-checkout
echo "cir-2" >>.git/modules/<submod_name>/info/sparse-checkout
git submodule update --force --checkout <submod_name>

  • Not sure if this needs two seperate calls? Worktree.Pull() & Worktree.Pull(), see code below.

git pull <remote_name> main
git add .
git commit -S -m "commit example message"

My Questions

  1. How do I add a submodule using go-git?
  2. Do I need the separate sw.Checkout(&git.CheckoutOptions bit? And if so, in what order should I do them?
  3. How do I go about configuring a Sparse Checkout in either the main repo OR the submodule?
  4. Any thoughts on the git submodule absorbgitdirs? Any info about what it is would be appreciated

What I've got so far (condensed)

NOTE: I have more, but it doesn't relate to my questions
This bit is just for the submodule update, since the go-git documentation doesn't allow for --checkout as part of the git.PullOptions()

// Should be equivalent to : git submodule update --force --checkout <submod_name>
// Get the repo object (could be git.plainOpen() too)
r, err := git.PlainClone(directory, false, &git.CloneOptions{
    <options>
})

// Get the Submodule WorkTree object
w, err := r.Worktree()
sub, err := w.Submodule(submodule)
sr, err := sub.Init()
sw, err := sr.Worktree()

// Get the Update the Submodule
err = sw.Pull(&git.PullOptions{
    <options>
}

// Checkout the submodule, Not sure if this is the right order?
err = sw.Checkout(&git.CheckoutOptions{
    <options>
}
aRustyDev
  • 101
  • 1
  • 11

1 Answers1

2

From the current state of submodule.go and submodule_test.go, adding a submodule does not seem implemented.

That means you would need to use the client.go to exec.Command the git submodule add yourself.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250