potatobuffer
potatobuffer

Reputation: 11

Local git config in git submodule

My repository structure:

repo_a
└── repo_b
    └── .gitconfig

repo_b is a submodule.
.gitconfig:

[alias]
    b = branch

How can I add the path of .gitconfig such that only repo_b can use it?

Expected output:

$ cd repo_a
$ git b
git: 'b' is not a git command. See 'git --help'.
$ cd repo_b
$ git b
* master

Edit #1:

The answers from @jthill and @bk2204 are good.
They describe how to access the config file for the submodule using:

git rev-parse --git-path config

I want to specify what I want.
I want the changes in the .gitconfig file in repo_b to be tracked by git. Such that when cloning only repo_b I would also have access to the same .gitconfig.
The solution from @jthill does seem possible. However, it does also seem quite error-prone, and I would have to add a hook for every single change in git that might change the .gitconfig. Such as git pull, git checkout, git merge.

Thus my question would have 2 parts:

  1. How can I both track changes for repo/.gitconfig and use it as a local config file?
  2. How can I implement said functionality for both a normal repo and within a submodule?

Upvotes: 1

Views: 5422

Answers (5)

guiccbr
guiccbr

Reputation: 720

If you just cd into the submodule and use something like:

git config user.name "Jon Doe"

You'll see that git will add that config to your root repository under .git/modules/<your-submodule-name>/config

You could use that file to set any specific config for your submodule.

Upvotes: 1

potatobuffer
potatobuffer

Reputation: 11

I did not implement this feature because of security concerns.

I decided to use a separate configuration file in JSON and store the values I needed there.
Thus I could parse the config file when needed instead of using git.

Upvotes: 0

jthill
jthill

Reputation: 60305

As a smoketest, you can have people using the repo do

cat .gitconfig >>$(git rev-parse --git-dir)/config

and that will append your configs to the repository setup. For a safer version,

sed -si '/^# --repo-b-config--$/,/^# --repo-b-config--$/d; $r .gitconfig' \
        $(git rev-parse --git-dir)/config

and have # --repo-b-config-- lines at the top and bottom of your special fil.

Git intentionally does not allow fetch to override administrative choices. There is no way to make fetch usurp anything. Your repository is yours. You decide what happens in it, and that means nobody else gets to sneak anything in unless you build the pathway for them yourself. You can run arbitrary commands on checkout to update the local config or anything else, but in your repository, in every repository you administer (including for instance every repo you create with git clone or git init or git submodule update --init) you control what those commands are.

So if you want to have the above sed run after every checkout, put (a possibly-fixed-up version of) it in that repo's hooks/post-checkout and you'll have what you want.

Upvotes: 0

prosoitos
prosoitos

Reputation: 7347

I don't think that it is possible:

Reading the section of the Git manual corresponding to the configuration of submodules, it seems that the 2 possible options are:

  • Creating a file called config in the root of the submodule
  • Adding content to the .gitmodules file of the superproject

I tried to add an alias in both of these files and the alias did not take effect.

Edit:

Actually, now that I am taking the time to think, it makes sense that this would not be possible since the code for git-submodule was not written with the idea that you would run git commands from within a submodule, but instead, directly from the superproject.

If you run your git commands for your submodule from within the submodule, then you are not making use of the submodule functionality. In that case, you might be better off creating a normal repo within your superproject and add its path to the .gitignore file of the superproject. Probably better though would be to learn to handle the submodule from the superproject.

Upvotes: 0

bk2204
bk2204

Reputation: 76549

If you're looking for the equivalent of .git/config for a submodule, then the easiest way to find it is to run git rev-parse --git-path config from within repo_b. That's the easiest way to set up configuration for a local repository.

You can't put a .gitconfig file into a repository and expect it to be used; it's not secure to share configuration in a repository, so Git doesn't allow it.

If you want to edit the local configuration with git config, you can do that by changing into repo_b.

Upvotes: 3

Related Questions