Skip to content

Commit 4f99292

Browse files
authored
Merge pull request #17 from techquestsdev/andre.nogueira/dir-based-profiles
feat: directory-based profile assignment via includeIf
2 parents e1b66e4 + c11f5da commit 4f99292

21 files changed

Lines changed: 4906 additions & 459 deletions

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,32 @@ git-context remove university
182182
| `git-context current` | Show active profile |
183183
| `git-context show <name>` | Show profile details |
184184
| `git-context remove <name>` | Delete a profile |
185+
| `git-context dir add <path> <profile>` | Assign a directory to a profile (auto-applied via includeIf) |
186+
| `git-context dir remove <path>` | Remove a directory assignment |
187+
| `git-context dir list` | List all directory assignments |
185188
| `git-context --help` | Show help |
186189
| `git-context --version` | Show version |
187190

191+
### Directory-Based Profiles
192+
193+
Assign filesystem paths to profiles so git applies the right identity automatically when you're inside them — no need to remember to `switch`.
194+
195+
```bash
196+
# Make 'work' the default profile (used everywhere unless overridden)
197+
git-context switch work
198+
199+
# Assign specific directories to other profiles
200+
git-context dir add ~/projects/personal personal
201+
git-context dir add ~/Mollie work
202+
203+
# See all assignments
204+
git-context dir list
205+
```
206+
207+
Under the hood git-context generates one gitconfig file per profile under `~/.config/git-context/profiles/` and rewrites `~/.gitconfig` as a thin manifest of `[include]` and `[includeIf "gitdir:..."]` blocks that git-context regenerates on every mutating command. The YAML at `~/.config/git-context/config.yaml` is always the source of truth.
208+
209+
> Note: git-context owns `~/.gitconfig` end-to-end — don't run `git config --global` directly. Edit the YAML or use the CLI instead.
210+
188211
## Configuration
189212

190213
The configuration is stored in YAML format at `~/.config/git-context/config.yaml`.

cmd/add.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/aanogueira/git-context/internal/config"
7+
"github.com/aanogueira/git-context/internal/git"
78
"github.com/aanogueira/git-context/internal/ui"
89
"github.com/cockroachdb/errors"
910
"github.com/spf13/cobra"
@@ -77,6 +78,30 @@ func runAdd(cmd *cobra.Command, args []string) error {
7778
profile.URL = promptURLRewrites()
7879
}
7980

81+
addDirs, _ := ui.PromptConfirm("Assign directories to this profile?")
82+
if addDirs {
83+
for {
84+
path, err := ui.PromptText("Directory path (leave empty to stop)", "")
85+
if err != nil || path == "" {
86+
break
87+
}
88+
89+
normalized, err := config.NormalizeDir(path)
90+
if err != nil {
91+
ui.PrintWarning(fmt.Sprintf("Skipping %q: %v", path, err))
92+
93+
continue
94+
}
95+
96+
profile.Directories = append(profile.Directories, normalized)
97+
98+
more, _ := ui.PromptConfirm("Add another directory?")
99+
if !more {
100+
break
101+
}
102+
}
103+
}
104+
80105
// Add the profile
81106
if err := cfg.AddProfile(profileName, profile); err != nil {
82107
ui.PrintError(fmt.Sprintf("Failed to add profile: %v", err))
@@ -91,6 +116,15 @@ func runAdd(cmd *cobra.Command, args []string) error {
91116
return errors.Wrap(err, "failed to save config")
92117
}
93118

119+
if len(profile.Directories) > 0 || cfg.Current != "" {
120+
g := git.NewGit(paths.GitConfigFile)
121+
if err := g.Regenerate(cfg, paths.ProfilesDir); err != nil {
122+
ui.PrintError(fmt.Sprintf("Failed to regenerate git config: %v", err))
123+
124+
return errors.Wrap(err, "failed to regenerate git config")
125+
}
126+
}
127+
94128
ui.PrintSuccess(fmt.Sprintf("Profile '%s' created successfully", profileName))
95129

96130
return nil

0 commit comments

Comments
 (0)