Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
baliestri committed Dec 26, 2024
2 parents 2f06bad + eace770 commit cc871fb
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 128 deletions.
12 changes: 6 additions & 6 deletions .github/assets/day.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions .github/assets/night.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
name: Generate release
name: Auto Release

on:
push:
tags:
- "v*"

jobs:
build:
release:
runs-on: ubuntu-latest

permissions:
contents: write
discussions: write

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Generate release
uses: softprops/action-gh-release@v1
env:
Expand All @@ -21,4 +27,3 @@ jobs:
files: |
dist/*.zip
dist/*.tar.gz
git-profiles.plugin.zsh
4 changes: 3 additions & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copyright (c) 2023 Bruno Sales <me@baliestri.dev>
# The MIT License (MIT)

Copyright (c) `2023-2025` `Bruno Sales <me@baliestri.dev>`

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<p align="center">
<a href="#gh-dark-mode-only" target="_blank" rel="noopener noreferrer">
<img src=".github/assets/night.svg" alt="git-extra-profiles.plugin.zsh">
<img src=".github/assets/night.svg" alt="gitprofiles.plugin.zsh">
</a>

<a href="#gh-light-mode-only" target="_blank" rel="noopener noreferrer">
<img src=".github/assets/day.svg" alt="git-extra-profiles.plugin.zsh">
<img src=".github/assets/day.svg" alt="gitprofiles.plugin.zsh">
</a>
</p>

Expand All @@ -14,53 +14,52 @@ Plugin for managing multiple `git` profiles.

## Installation

### Using [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh)
#### [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh)

```sh
git clone https://github.com/baliestri/git-profiles.plugin.zsh.git $ZSH_CUSTOM/plugins/git-profiles
```shell
git clone https://github.com/empresslabs/gitprofiles.plugin.zsh.git $ZSH_CUSTOM/plugins/gitprofiles
```

Then add `git-profiles` to the plugins array in your zshrc file:

```sh
plugins=(... git-profiles)
```shell
~/.zshrc
plugins=(... gitprofiles)
```

### Using [zplug](https://github.com/zplug/zplug)
#### [zinit](https://github.com/zdharma-continuum/zinit)

```sh
zplug "baliestri/git-profiles.plugin.zsh"
```shell
zinit light empresslabs/gitprofiles.plugin.zsh
```

### Using [zinit](https://github.com/zdharma-continuum/zinit)
#### [zi](https://github.com/z-shell/zi)

```sh
zinit light baliestri/git-profiles.plugin.zsh
```shell
zi light empresslabs/gitprofiles.plugin.zsh
```

### Using [zgenom](https://github.com/jandamm/zgenom)
#### [zgenom](https://github.com/jandamm/zgenom)

```sh
zgenom load baliestri/git-profiles.plugin.zsh
```shell
zgenom load empresslabs/gitprofiles.plugin.zsh
```

### Using [zi](https://github.com/z-shell/zi)
#### [zplug](https://github.com/zplug/zplug)

```sh
zi light baliestri/git-profiles.plugin.zsh
```shell
zplug empresslabs/gitprofiles.plugin.zsh
```

## Usage

### Define where your profiles are stored
#### Define where your profiles are stored

```sh
# ~/.zshrc

export GIT_PROFILES_FILE="$HOME/.config/git/profiles" # Fallback to $HOME/.git-profiles
zstyle ":empresslabs:git:profiles" path "$HOME/.config/git/profiles"
```

### Add a new profile
#### Add a new profile

```sh
# ~/.config/git/profiles
Expand Down
88 changes: 0 additions & 88 deletions git-profiles.plugin.zsh

This file was deleted.

90 changes: 90 additions & 0 deletions gitprofiles.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright (c) Bruno Sales <me@baliestri.dev>. Licensed under the MIT License.
# See the LICENSE file in the project root for full license information.

#!/usr/bin/env zsh

function __gitprofiles_hook() {
## Check if git is installed
if (( ! $+commands[git] )); then
return 1
fi

local -A profile_path_map=()
local -A profile_cfg_map=()

## Get the path to the profile file
zstyle -s ":empresslabs:git:profile" path profile_filepath

## Check if the file exists
if [[ ! -f "${profile_filepath}" ]]; then
return 1
fi

## Load all stored profiles
local profiles=($(grep -o '\[profile [^]]*\]' ${profile_filepath} | tr -d '[]" ' | sed 's/profile//g' | tr '\n' ' '))

## Check if default profile exists
if [[ ! "${profiles}" =~ "default" ]]; then
echo "gitprofiles: 'default' profile not found in '${profile_filepath}'"
return 1
fi

## Iterate over all profiles to get the name, email, signingkey and path
for profile in ${profiles}; do
local -A profile_value_map=()

while read -r key value; do
case "${key}" in
name)
profile_value_map[name]="${value}"
;;
email)
profile_value_map[email]="${value}"
;;
signingkey)
profile_value_map[signingkey]="${value}"
;;
path)
profile_value_map[path]="${value}"
;;
esac
done < <(awk -F ' = ' '/^\[profile/{p=0} /^\[profile "[^"]*'"${profile}"'"/{p=1} p {gsub(/"/, "", $2); print $1,$2}' ${profile_filepath})

profile_path_map[${profile}]="${profile_value_map[path]}"

profile_cfg_map[${profile}.name]="${profile_value_map[name]}"
profile_cfg_map[${profile}.email]="${profile_value_map[email]}"

if [[ -n "${profile[signingkey]}" ]]; then
profile_cfg_map[${profile}.signingkey]="${profile_value_map[signingkey]}"
fi
done

## Get the current directory
local -A current=()
current[dir]=$(pwd)

## Check if the current directory is in one of the profiles paths
for profile in ${(k)profile_path_map}; do
if [[ "${current[dir]}" =~ "${profile_path_map[${profile}]}" ]]; then
local current[profile]="${profile}"
break
fi
done

## If the current directory is not in any profile path, use the default profile
if [[ -z "${current[profile]}" ]]; then
local current[profile]="default"
fi

## Set the current profile name and email
git config --global user.name "${profile_cfg_map[${current[profile]}.name]}"
git config --global user.email "${profile_cfg_map[${current[profile]}.email]}"

## Set the current profile signingkey if it exists
if [[ -n "${profile_cfg_map[${current[profile]}.signingkey]}" ]]; then
git config --global user.signingkey "${profile_cfg_map[${current[profile]}.signingkey]}"
fi
}

add-zsh-hook chpwd __gitprofiles_hook

0 comments on commit cc871fb

Please sign in to comment.