-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2681ea1
commit 6175555
Showing
1 changed file
with
88 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,88 @@ | ||
package shell | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
var ( | ||
gitError bool | ||
gitConfigCache map[string]string | ||
) | ||
|
||
func (a *Alias) git() string { | ||
if a.If.Ignore() { | ||
return "" | ||
} | ||
|
||
if gitError { | ||
return "" | ||
} | ||
|
||
// make sure we have the aliases | ||
if err := a.makeGitAliae(); err != nil { | ||
gitError = true | ||
return "" | ||
} | ||
|
||
// in case we already have this alias, we do not add it again | ||
if match, OK := gitConfigCache[a.Name]; OK && match == string(a.Value) { | ||
return "" | ||
} | ||
|
||
// safe to add the alias | ||
format := `git config --global alias.%s '%s'` | ||
return fmt.Sprintf(format, a.Name, a.Value) | ||
} | ||
|
||
func (a *Alias) makeGitAliae() error { | ||
if gitConfigCache != nil { | ||
return nil | ||
} | ||
|
||
config, err := a.getGitAliasOutput() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a.parsegitConfig(config) | ||
|
||
return nil | ||
} | ||
|
||
func (a *Alias) getGitAliasOutput() (string, error) { | ||
path, err := exec.LookPath("git") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
cmd := exec.Command(path, "config", "--get-regexp", "^alias\\.") | ||
// when no aliae have been set, it causes git to panic | ||
raw, _ := cmd.Output() | ||
return string(raw), nil | ||
} | ||
|
||
func (a *Alias) parsegitConfig(config string) { | ||
gitConfigCache = make(map[string]string) | ||
|
||
for _, line := range strings.Split(config, "\n") { | ||
if len(line) == 0 { | ||
continue | ||
} | ||
|
||
parts := strings.SplitN(line, " ", 2) | ||
|
||
if len(parts) != 2 || !strings.HasPrefix(parts[0], "alias.") { | ||
continue | ||
} | ||
|
||
gitConfigCache[parts[0][6:]] = parts[1] | ||
} | ||
} | ||
package shell | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/jandedobbeleer/aliae/src/context" | ||
) | ||
|
||
var ( | ||
gitError bool | ||
gitConfigCache map[string]string | ||
) | ||
|
||
func (a *Alias) git() string { | ||
if a.If.Ignore() { | ||
return "" | ||
} | ||
|
||
if gitError { | ||
return "" | ||
} | ||
|
||
// make sure we have the aliases | ||
if err := a.makeGitAliae(); err != nil { | ||
gitError = true | ||
return "" | ||
} | ||
|
||
// in case we already have this alias, we do not add it again | ||
if match, OK := gitConfigCache[a.Name]; OK && match == string(a.Value) { | ||
return "" | ||
} | ||
|
||
// safe to add the alias | ||
format := `git config --global alias.%s '%s'` | ||
if context.Current.Shell == NU { | ||
format = `git config --global alias.%s r#'%s'#` | ||
} | ||
|
||
return fmt.Sprintf(format, a.Name, a.Value) | ||
} | ||
|
||
func (a *Alias) makeGitAliae() error { | ||
if gitConfigCache != nil { | ||
return nil | ||
} | ||
|
||
config, err := a.getGitAliasOutput() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
a.parsegitConfig(config) | ||
|
||
return nil | ||
} | ||
|
||
func (a *Alias) getGitAliasOutput() (string, error) { | ||
path, err := exec.LookPath("git") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
cmd := exec.Command(path, "config", "--get-regexp", "^alias\\.") | ||
// when no aliae have been set, it causes git to panic | ||
raw, _ := cmd.Output() | ||
return string(raw), nil | ||
} | ||
|
||
func (a *Alias) parsegitConfig(config string) { | ||
gitConfigCache = make(map[string]string) | ||
|
||
for _, line := range strings.Split(config, "\n") { | ||
if len(line) == 0 { | ||
continue | ||
} | ||
|
||
parts := strings.SplitN(line, " ", 2) | ||
|
||
if len(parts) != 2 || !strings.HasPrefix(parts[0], "alias.") { | ||
continue | ||
} | ||
|
||
gitConfigCache[parts[0][6:]] = parts[1] | ||
} | ||
} |