aRustyDev
aRustyDev

Reputation: 171

go-git "git submodule add <url> <submod>"

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>

git submodule absorbgitdirs

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

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>

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>
}

Upvotes: 1

Views: 487

Answers (1)

VonC
VonC

Reputation: 1324827

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.

Upvotes: 3

Related Questions