├── .gitignore ├── .github └── workflows │ └── release.yml ├── go.mod ├── LICENSE ├── git.go ├── README.md ├── go.sum └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | /gh-local-changes 2 | /gh-local-changes.exe 3 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | on: 3 | push: 4 | tags: 5 | - "v*" 6 | permissions: 7 | contents: write 8 | 9 | jobs: 10 | release: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | - uses: cli/gh-extension-precompile@v1 15 | with: 16 | go_version: "1.21" 17 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/baruchiro/gh-local-changes 2 | 3 | go 1.21 4 | 5 | require github.com/charmbracelet/log v0.3.1 6 | 7 | require ( 8 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect 9 | github.com/charmbracelet/lipgloss v0.9.1 // indirect 10 | github.com/go-logfmt/logfmt v0.6.0 // indirect 11 | github.com/lucasb-eyer/go-colorful v1.2.0 // indirect 12 | github.com/mattn/go-isatty v0.0.20 // indirect 13 | github.com/mattn/go-runewidth v0.0.15 // indirect 14 | github.com/muesli/reflow v0.3.0 // indirect 15 | github.com/muesli/termenv v0.15.2 // indirect 16 | github.com/rivo/uniseg v0.2.0 // indirect 17 | golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect 18 | golang.org/x/sys v0.18.0 // indirect 19 | ) 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Timothy Lin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /git.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os/exec" 6 | "strings" 7 | ) 8 | 9 | type Repo interface { 10 | getUnpushedBranches() (map[string]string, error) 11 | getUnpushedChanges() (int, error) 12 | } 13 | 14 | type GitRepo struct { 15 | folder string 16 | } 17 | 18 | var _ Repo = (*GitRepo)(nil) 19 | 20 | func runGit(folder string, commandAndArgs ...string) (string, error) { 21 | cmd := exec.Command("git", commandAndArgs...) 22 | cmd.Dir = folder 23 | out, err := cmd.Output() 24 | if err != nil { 25 | return "", fmt.Errorf("error running git: %w", err) 26 | } 27 | return string(out), nil 28 | } 29 | 30 | var unpushedBranchesCommand = []string{"log", "--branches", "--not", "--remotes", `--pretty=format:"%h %d"`} 31 | 32 | func (r *GitRepo) getUnpushedBranches() (map[string]string, error) { 33 | out, err := runGit(r.folder, unpushedBranchesCommand...) 34 | if err != nil { 35 | return nil, fmt.Errorf("Error running git log: %w", err) 36 | } 37 | 38 | branches := make(map[string]string) 39 | for _, line := range strings.Split(out, "\n") { 40 | if line == "" { 41 | continue 42 | } 43 | words := strings.Fields(line) 44 | commit := words[0] 45 | branch := words[1] 46 | if !strings.HasPrefix(branch, "(") { 47 | continue 48 | } 49 | branch = strings.Trim(branch, "()") 50 | branches[branch] = commit 51 | } 52 | return branches, nil 53 | } 54 | 55 | var unpushedChangesCommand = []string{"status", "--porcelain"} 56 | 57 | func (r *GitRepo) getUnpushedChanges() (int, error) { 58 | out, err := runGit(r.folder, unpushedChangesCommand...) 59 | if err != nil { 60 | return -1, fmt.Errorf("error running git status: %w", err) 61 | } 62 | return strings.Count(out, "\n"), nil 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GH Local Changes 2 | 3 | GH Local Changes is a GitHub CLI extension that scans a given directory for Git repositories and reports on branches and changes that have not been pushed to the remote repository. 4 | 5 | ## Installation 6 | 7 | To install GH Local Changes, you need to have the [GitHub CLI](https://cli.github.com/) installed. Then, you can install the extension with the following command: 8 | 9 | ```bash 10 | gh extension install baruchiro/gh-local-changes 11 | ``` 12 | 13 | ## Usage 14 | 15 | To use GH Local Changes, run the following command: 16 | 17 | ``` 18 | gh local-changes 19 | ``` 20 | 21 | By default, GH Local Changes scans the current directory. You can specify a different directory by passing it as an argument: 22 | 23 | ``` 24 | gh local-changes ~/source 25 | ``` 26 | 27 | To print debug logs , use --debug flag : 28 | 29 | ``` 30 | gh local-changes --debug 31 | gh local-changes --debug ~/source 32 | ``` 33 | 34 | GH Local Changes will recursively scan the specified directory for Git repositories. For each repository, it will report the branches and changes that have not been pushed to the remote repository. 35 | 36 | ## Contributing 37 | 38 | To develop GH Local Changes, you need to have [Go](https://golang.org) installed. You can then clone the repository and run the program with the following commands: 39 | 40 | ```bash 41 | git clone https://github.com/baruchiro/gh-local-changes.git 42 | cd gh-local-changes 43 | go build 44 | gh extension install . 45 | ``` 46 | 47 | To continuously check your changes, you can run the following command: 48 | 49 | ```bash 50 | go build; gh local-changes ~/source 51 | ``` 52 | 53 | ## License 54 | 55 | GH Local Changes is released under the [MIT License](LICENSE). 56 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= 2 | github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= 3 | github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg= 4 | github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I= 5 | github.com/charmbracelet/log v0.3.1 h1:TjuY4OBNbxmHWSwO3tosgqs5I3biyY8sQPny/eCMTYw= 6 | github.com/charmbracelet/log v0.3.1/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g= 7 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 8 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 9 | github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= 10 | github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= 11 | github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= 12 | github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= 13 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 14 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 15 | github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= 16 | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= 17 | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 18 | github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= 19 | github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= 20 | github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= 21 | github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= 22 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 23 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 24 | github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 25 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 26 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 27 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 28 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 29 | golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= 30 | golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= 31 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 32 | golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= 33 | golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 34 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 35 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 36 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "path/filepath" 8 | "strings" 9 | "sync" 10 | "text/tabwriter" 11 | 12 | "github.com/charmbracelet/log" 13 | ) 14 | 15 | var skipFolders = []string{ 16 | "node_modules", 17 | } 18 | var gitReposChan = make(chan string, 10) 19 | 20 | type results struct { 21 | repo string 22 | branches map[string]string 23 | changes int 24 | } 25 | 26 | var allResults = []results{} 27 | 28 | func walkDirectory(dir string) error { 29 | defer close(gitReposChan) 30 | return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { 31 | if err != nil { 32 | return err 33 | } 34 | if info.IsDir() { 35 | for _, skip := range skipFolders { 36 | if info.Name() == skip { 37 | return filepath.SkipDir 38 | } 39 | } 40 | if info.Name() == ".git" { 41 | repoFolder := filepath.Dir(path) 42 | log.Debug("Found git repository", "path", repoFolder) 43 | gitReposChan <- repoFolder 44 | return filepath.SkipDir 45 | } 46 | } 47 | return nil 48 | }) 49 | } 50 | 51 | func handleGitRepos(wg *sync.WaitGroup) { 52 | defer wg.Done() 53 | for repoFolder := range gitReposChan { 54 | log.Debug("Processing git repository", "path", repoFolder) 55 | repo := &GitRepo{folder: repoFolder} 56 | results := results{ 57 | repo: repoFolder, 58 | branches: map[string]string{}, 59 | changes: 0, 60 | } 61 | 62 | branches, err := repo.getUnpushedBranches() 63 | if err != nil { 64 | log.Error("Error checking git status:", err) 65 | continue 66 | } else { 67 | results.branches = branches 68 | } 69 | 70 | changes, err := repo.getUnpushedChanges() 71 | if err != nil { 72 | log.Error("Error checking git status:", err) 73 | continue 74 | } else { 75 | results.changes = changes 76 | } 77 | 78 | allResults = append(allResults, results) 79 | } 80 | } 81 | 82 | func printLookingForContributors() { 83 | log.Info("--------------------------------------------") 84 | log.Info("This project was developed in a short time, before I returned my laptop to the IT department. I'm looking for contributors to help me improve it.") 85 | log.Info("Any help is welcome, be it a suggestion, a bug report, a pull request...") 86 | log.Info("https://github.com/baruchiro/gh-local-changes") 87 | log.Info("--------------------------------------------") 88 | } 89 | 90 | var debug bool 91 | 92 | func main() { 93 | defer printLookingForContributors() 94 | flag.BoolVar(&debug, "debug", false, "Enable debug logging") 95 | 96 | dir := "." 97 | for _, arg := range os.Args[1:] { 98 | if arg == "-h" || arg == "--help" { 99 | log.Info("Usage: go-gh [dir]") 100 | os.Exit(0) 101 | } 102 | } 103 | flag.Parse() 104 | parsedArgs := flag.Args() 105 | 106 | if len(parsedArgs) > 0 { 107 | dir = parsedArgs[0] 108 | // Fail if the directory does not exist 109 | if _, err := os.Stat(dir); os.IsNotExist(err) { 110 | log.Fatal("Directory does not exist:", dir) 111 | } 112 | } 113 | 114 | if debug { 115 | log.SetLevel(log.DebugLevel) 116 | } 117 | wg := &sync.WaitGroup{} 118 | wg.Add(1) 119 | go handleGitRepos(wg) 120 | 121 | if err := walkDirectory(dir); err != nil { 122 | log.Fatal("Error walking directory:", err) 123 | os.Exit(1) 124 | } 125 | wg.Wait() 126 | 127 | // Print results in a table 128 | log.Info("Results:") 129 | w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug) 130 | fmt.Fprintln(w, "Repo\tBranches\tChanges") 131 | for _, r := range allResults { 132 | if len(r.branches) > 0 || r.changes > 0 { 133 | fmt.Fprintf(w, "%s\t%v\t%d\n", r.repo, len(r.branches), r.changes) 134 | } 135 | } 136 | w.Flush() 137 | 138 | log.Info("To see the unpushed branches, run: git " + strings.Join(unpushedBranchesCommand, " ")) 139 | log.Info("To see the unpushed changes, run: git " + strings.Join(unpushedChangesCommand, " ")) 140 | } 141 | 142 | // For more examples of using go-gh, see: 143 | // https://github.com/cli/go-gh/blob/trunk/example_gh_test.go 144 | --------------------------------------------------------------------------------