Last active
July 13, 2024 20:34
-
-
Save paralin/e504320faef9a62505320e51be4b2079 to your computer and use it in GitHub Desktop.
git utilities written in Go
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"strings" | |
"sync" | |
) | |
func runFindDelete(searchString, dir, workDir string) { | |
cmd := exec.Command("git", "grep", "-l", "-F", searchString) | |
cmd.Stderr = os.Stderr | |
cmd.Dir = dir | |
stdout, err := cmd.StdoutPipe() | |
if err != nil { | |
log.Fatalf("Failed to create stdout pipe for git grep: %s\n", err) | |
} | |
if err := cmd.Start(); err != nil { | |
log.Fatalf("Failed to start git grep: %s\n", err) | |
} | |
var filePaths []string | |
scanner := bufio.NewScanner(stdout) | |
for scanner.Scan() { | |
filePaths = append(filePaths, scanner.Text()) | |
} | |
if err := cmd.Wait(); err != nil { | |
if strings.HasSuffix(err.Error(), "exit status 1") { | |
fmt.Fprintf(os.Stderr, "Pattern not found in: %s\n", dir) | |
return | |
} | |
log.Fatalf("Failed to wait for git grep: %s\n", err) | |
} | |
relDir, err := filepath.Rel(workDir, dir) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) | |
return | |
} | |
fmt.Fprintf(os.Stderr, "Found at %s in %d files...\n", relDir, len(filePaths)) | |
var wg sync.WaitGroup | |
for i, relFilePath := range filePaths { | |
fmt.Fprintf(os.Stderr, "Deleting in: %s\n", relFilePath) | |
absFilePath := filepath.Join(dir, relFilePath) | |
wg.Add(1) | |
go func(i int, filePath string) { | |
defer wg.Done() | |
fileContent, err := os.ReadFile(filePath) | |
if err != nil { | |
log.Printf("Failed to read file %s: %s\n", filePath, err) | |
return | |
} | |
lines := strings.Split(string(fileContent), "\n") | |
var newLines []string | |
for _, line := range lines { | |
if !strings.Contains(line, searchString) { | |
newLines = append(newLines, line) | |
} | |
} | |
err = os.WriteFile(filePath, []byte(strings.Join(newLines, "\n")), 0644) | |
if err != nil { | |
log.Printf("Failed to write to file %s: %s\n", filePath, err) | |
return | |
} | |
fmt.Fprintf(os.Stderr, "[%d/%d]: deleted in %s\n", i+1, len(filePaths), filepath.Base(filePath)) | |
}(i, absFilePath) | |
} | |
wg.Wait() | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatalf("Usage: %s <search string>\n", os.Args[0]) | |
} | |
searchString := os.Args[1] | |
currentDir, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("Failed to get current directory: %s\n", err) | |
} | |
// If the .git directory does not exist in the current dir, check if it exists in child dirs. | |
if _, err := os.Stat(filepath.Join(currentDir, ".git")); os.IsNotExist(err) { | |
// .git does not exist, iterate through subdirectories | |
filepath.Walk(currentDir, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err // propagate the error | |
} | |
if info.IsDir() && info.Name() == ".git" { | |
// Found a .git directory, run find-delete in its parent directory | |
gitDir := filepath.Dir(path) | |
runFindDelete(searchString, gitDir, currentDir) | |
return filepath.SkipDir // skip remaining files in this directory | |
} | |
return nil | |
}) | |
} | |
// Try running the delete in the current dir as well | |
runFindDelete(searchString, currentDir, currentDir) | |
} |
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"strings" | |
"sync" | |
) | |
func runFindReplace(searchString, replacementString, dir, workDir string) { | |
cmd := exec.Command("git", "grep", "-l", "-F", searchString) | |
cmd.Stderr = os.Stderr | |
cmd.Dir = dir | |
stdout, err := cmd.StdoutPipe() | |
if err != nil { | |
log.Fatalf("Failed to create stdout pipe for git grep: %s\n", err) | |
} | |
if err := cmd.Start(); err != nil { | |
log.Fatalf("Failed to start git grep: %s\n", err) | |
} | |
var filePaths []string | |
scanner := bufio.NewScanner(stdout) | |
for scanner.Scan() { | |
filePaths = append(filePaths, scanner.Text()) | |
} | |
if err := cmd.Wait(); err != nil { | |
if strings.HasSuffix(err.Error(), "exit status 1") { | |
fmt.Fprintf(os.Stderr, "Pattern not found in: %s\n", dir) | |
return | |
} | |
log.Fatalf("Failed to wait for git grep: %s\n", err) | |
} | |
relDir, err := filepath.Rel(workDir, dir) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) | |
return | |
} | |
fmt.Fprintf(os.Stderr, "Found at %s in %d files...\n", relDir, len(filePaths)) | |
var wg sync.WaitGroup | |
for i, relFilePath := range filePaths { | |
fmt.Fprintf(os.Stderr, "Replacing in: %s\n", relFilePath) | |
absFilePath := filepath.Join(dir, relFilePath) | |
wg.Add(1) | |
go func(i int, filePath string) { | |
defer wg.Done() | |
content, err := os.ReadFile(filePath) | |
if err != nil { | |
log.Printf("Failed to read file %s: %s\n", filePath, err) | |
return | |
} | |
newContent := strings.ReplaceAll(string(content), searchString, replacementString) | |
err = os.WriteFile(filePath, []byte(newContent), 0) | |
if err != nil { | |
log.Printf("Failed to write to file %s: %s\n", filePath, err) | |
return | |
} | |
fmt.Fprintf(os.Stderr, "[%d/%d]: replaced in %s\n", i+1, len(filePaths), filepath.Base(filePath)) | |
}(i, absFilePath) | |
} | |
wg.Wait() | |
} | |
func main() { | |
if len(os.Args) != 3 { | |
log.Fatalf("Usage: %s <search string> <replacement string>\n", os.Args[0]) | |
} | |
searchString := os.Args[1] | |
replacementString := os.Args[2] | |
currentDir, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("Failed to get current directory: %s\n", err) | |
} | |
// If the .git directory does not exist in the current dir, check if it exists in child dirs. | |
if _, err := os.Stat(filepath.Join(currentDir, ".git")); os.IsNotExist(err) { | |
// .git does not exist, iterate through subdirectories | |
filepath.Walk(currentDir, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err // propagate the error | |
} | |
if info.IsDir() && info.Name() == ".git" { | |
// Found a .git directory, run find-replace in its parent directory | |
gitDir := filepath.Dir(path) | |
runFindReplace(searchString, replacementString, gitDir, currentDir) | |
return filepath.SkipDir // skip remaining files in this directory | |
} | |
return nil | |
}) | |
} | |
// Try running the replace in the current dir as well | |
runFindReplace(searchString, replacementString, currentDir, currentDir) | |
} |
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"strings" | |
) | |
func runFind(searchString, dir, workDir string) { | |
cmd := exec.Command("git", "grep", "-l", "-F", searchString) | |
cmd.Stderr = os.Stderr | |
cmd.Dir = dir | |
stdout, err := cmd.StdoutPipe() | |
if err != nil { | |
log.Fatalf("Failed to create stdout pipe for git grep: %s\n", err) | |
} | |
if err := cmd.Start(); err != nil { | |
log.Fatalf("Failed to start git grep: %s\n", err) | |
} | |
var filePaths []string | |
scanner := bufio.NewScanner(stdout) | |
for scanner.Scan() { | |
filePaths = append(filePaths, scanner.Text()) | |
} | |
if err := cmd.Wait(); err != nil { | |
if strings.HasSuffix(err.Error(), "exit status 1") { | |
fmt.Fprintf(os.Stderr, "Pattern not found in: %s\n", dir) | |
return | |
} | |
log.Fatalf("Failed to wait for git grep: %s\n", err) | |
} | |
for _, repoRelFilePath := range filePaths { | |
relFilePath, err := filepath.Rel(workDir, filepath.Join(dir, repoRelFilePath)) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) | |
continue | |
} | |
fmt.Fprintf(os.Stdout, "%s\n", relFilePath) | |
} | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatalf("Usage: %s <search string>\n", os.Args[0]) | |
} | |
searchString := os.Args[1] | |
currentDir, err := os.Getwd() | |
if err != nil { | |
log.Fatalf("Failed to get current directory: %s\n", err) | |
} | |
// If the .git directory does not exist in the current dir, check if it exists in child dirs. | |
if _, err := os.Stat(filepath.Join(currentDir, ".git")); os.IsNotExist(err) { | |
// .git does not exist, iterate through subdirectories | |
filepath.Walk(currentDir, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err // propagate the error | |
} | |
if info.IsDir() && info.Name() == ".git" { | |
// Found a .git directory, run find in its parent directory | |
gitDir := filepath.Dir(path) | |
runFind(searchString, gitDir, currentDir) | |
return filepath.SkipDir // skip remaining files in this directory | |
} | |
return nil | |
}) | |
} | |
// Try running in the current dir as well | |
runFind(searchString, currentDir, currentDir) | |
} |
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
Copyright 2024 Christian Stewart <[email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can build those like "go build -o git-find ./git-find.go"
mv ./git-find ~/.local/bin/
then it can be used with
"git find ..."