1

I basically work from home or from my company office accessing git repos available on my company network or elsewhere (personnal gitlab repos, github...). I usually use the same laptop for all locations.

When I'm working from office I can ssh to my local company repos but I can't ssh out of the network. I have setup the following relevant config snip in my .gitconfig so that the repos cloned from home are still accessible:

[url "ssh://git@comp.any.co/"]
    insteadof = https://comp.any.co/
[url "https://pers.onal.co/]
    insteadof = ssh://git@pers.onal.co:1234/
[url "https://github.com/"]
    insteadof = ssh://git@github.com/

When I work remotely from home, I cannot ssh to my company repos anymore but I can use ssh elsewhere. In this case I need to change my configuration snippet to:

[url "https://comp.any.co/"]
    insteadof = ssh://git@comp.any.co/
[url "ssh://git@pers.onal.co:1234/]
    insteadof = https://pers.onal.co/
[url "ssh://git@github.com/"]
    insteadof = https://github.com/

I could actually force https for every repo from every location. Although this would not make much difference for personal and github repos, there are some specificity on the company network that make this protocol switch highly preferred.

I'm looking for a way to automate this config switch based on network location (e.g. ip subnet, proxy availability...). I've looked at include.path and includeif.path but neither will let me execute a script or check for e.g. an environment variable.

I see a possible workaround with includeif.path and playing with the path from which I access my repos on my laptop depending on my physical location. But that is not really an automation and really far from ideal.

Before I go back writing a script or a shell function to automate this outside of git, did I miss any power usage of git config that would actually fulfill my requirement?

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • 1
    "includeIf" should probably be generalized to allow running external commands (although this is a bit slow, costing fork+exec on every read through of the config file). Right now, a hacky "edit my config" method is probably the thing to do. – torek Sep 01 '21 at 14:16

1 Answers1

2

I see a possible workaround with includeif.path and playing with the path from which I acces my repos on my laptop depending on my physical location. But that is not really an automation and really far from ideal.

And yet, this is the git-native solution, based on conditional includes.
The alternative would be a custom script.

I am in the same situation, and use a company network I can access at work, but not at home, to include one or the other config file.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm really not a C developper but I think I'll have a look at the code to evaluate the amount of work to propose an new `environment` verb aside `gitdir`, `gitdir/i` and `onbranch` for conditional includes. Unless this kind of proposition has already been rejected ? Thanks for confirming my first analyse though. Upvoted but won't accept as this is clearly not "automated" and needs to duplicate paths for each project (unless I missed something). – Zeitounator Sep 01 '21 at 12:39
  • 1
    @Zeitounator As an example of code, I list the patches for `onbranch` in https://stackoverflow.com/a/57031395/6309 – VonC Sep 01 '21 at 12:48