-
Notifications
You must be signed in to change notification settings - Fork 3
Performance Improvements #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b72e8e
Performance improvements using maps and goroutines
pmartindev 66be413
Update internal/commitremap/commitremap.go
pmartindev 9e68bcb
Comments,
pmartindev 3dc938a
Code clarity
pmartindev 390afa5
Code clarity
pmartindev 89704c0
Moving field len check for invalid input
ssulei7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,3 +22,5 @@ go.work | |
| go.work.sum | ||
|
|
||
| gh-commit-remap | ||
|
|
||
| .DS_Store | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,129 +1,180 @@ | ||
| package commitremap | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| // Struct to represent a single entry in the commit map | ||
| type CommitMapEntry struct { | ||
| Old string | ||
| New string | ||
| const COMMIT_MAP_HEADER string = "old new" | ||
|
|
||
| type File struct { | ||
| FilePath string | ||
| Prefix string | ||
| } | ||
|
|
||
| // Parses the file and returns a map of old commit hashes to new commit hashes | ||
| func ParseCommitMap(filePath string) (*[]CommitMapEntry, error) { | ||
| commitMap := []CommitMapEntry{} | ||
| // Parses the commit-map file and returns a map of old commit hashes to | ||
| // new commit hashes using the old commit sha as the key | ||
|
|
||
| // Read the commit-map file | ||
| func ParseCommitMap(filePath string) (*map[string]string, error) { | ||
| commitMap := make(map[string]string) | ||
| content, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // Split the file content into lines | ||
| lines := strings.Split(string(content), "\n") | ||
|
|
||
| // Iterate over the lines and parse the old and new commit hashes | ||
| for _, line := range lines { | ||
| if strings.TrimSpace(line) == "" { | ||
| buf := bytes.NewBuffer(content) | ||
| if buf.Len() == 0 { | ||
| return &commitMap, nil | ||
| } | ||
| scanner := bufio.NewScanner(buf) | ||
| for scanner.Scan() { | ||
|
pmartindev marked this conversation as resolved.
|
||
| line := scanner.Text() | ||
| // Skip adding the header to the map | ||
| if line == COMMIT_MAP_HEADER { | ||
| continue | ||
| } | ||
|
|
||
| fields := strings.Fields(line) | ||
| fields := strings.Split(line, " ") | ||
| if len(fields) != 2 { | ||
| return nil, fmt.Errorf("invalid line: %s", line) | ||
| } | ||
|
|
||
| commitMap = append(commitMap, CommitMapEntry{ | ||
| Old: fields[0], | ||
| New: fields[1], | ||
| }) | ||
| oldSha, newSha := fields[0], fields[1] | ||
| commitMap[oldSha] = newSha | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
| return &commitMap, nil | ||
| } | ||
|
|
||
| func ProcessFiles(archiveLocation string, prefixes []string, commitMap *[]CommitMapEntry) error { | ||
|
|
||
| for _, prefix := range prefixes { | ||
| // Get a list of all files that match the pattern | ||
| files, err := filepath.Glob(filepath.Join(archiveLocation, prefix+"_*.json")) | ||
| // Processes the files in the archive and updates the commit shas | ||
| func ProcessFiles(archiveLocation string, prefixes []string, | ||
| commitMap *map[string]string, workers int) error { | ||
| workerCount := workers | ||
| fileChannel := make(chan File, workerCount) | ||
| fileProcessWg := sync.WaitGroup{} | ||
| filesToProcess := getAllFilesToProcess(prefixes, archiveLocation) | ||
| totalFiles := len(filesToProcess) | ||
| processedFiles := make(chan File, totalFiles) | ||
| var processedFilesCount atomic.Int64 | ||
|
|
||
| // go routine to print out the progress of the processed files. It also | ||
| // writes the processed files to a log file | ||
| fmt.Printf("Processed %d/%d files\n", processedFilesCount, totalFiles) | ||
| go func() { | ||
| f, err := os.OpenFile("processed_files.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
| if err != nil { | ||
| log.Fatalf("Error getting files: %v", err) | ||
| log.Fatalf("error opening processed files log: %v", err) | ||
| } | ||
|
|
||
| // Process each file | ||
| for _, file := range files { | ||
| log.Println("Processing file:", file) | ||
|
|
||
| err := updateMetadataFile(file, commitMap) | ||
| if err != nil { | ||
| return fmt.Errorf("Error updating metadata file: %v; %v", file, err) | ||
| defer f.Close() | ||
| for file := range processedFiles { | ||
| // Clear the previous line | ||
| // \033 is the ASCII escape character | ||
| // [1A moves the cursor up one line | ||
| // [K erases the line | ||
| // https://en.wikipedia.org/wiki/ANSI_escape_code | ||
| fmt.Printf("\033[1A\033[K") | ||
|
pmartindev marked this conversation as resolved.
|
||
| fmt.Printf("Processed %d/%d files\n", processedFilesCount, totalFiles) | ||
| if _, err := f.WriteString(fmt.Sprintf("%s\n", file.FilePath)); err != nil { | ||
| log.Fatalf("error writing to processed files log: %v", err) | ||
| } | ||
| } | ||
| }() | ||
| // Starts a pool of workers to process the files | ||
| for i := 0; i < workerCount; i++ { | ||
| fileProcessWg.Add(1) | ||
| go func() { | ||
| defer fileProcessWg.Done() | ||
| for file := range fileChannel { | ||
| err := updateMetadataFile(file, *commitMap) | ||
| if err != nil { | ||
| log.Fatalf("error updating metadata file: %v", err) | ||
| } | ||
| processedFiles <- file | ||
| processedFilesCount.Add(1) | ||
| } | ||
| }() | ||
| } | ||
| prefixWg := sync.WaitGroup{} | ||
| // Seperate go routines to add the files to the channel | ||
| for _, file := range filesToProcess { | ||
| prefixWg.Add(1) | ||
| go func(file File) { | ||
| defer prefixWg.Done() | ||
| fileChannel <- file | ||
| }(file) | ||
| } | ||
| prefixWg.Wait() | ||
| close(fileChannel) | ||
| fileProcessWg.Wait() | ||
| close(processedFiles) | ||
| return nil | ||
| } | ||
|
|
||
| func updateMetadataFile(filePath string, commitMap *[]CommitMapEntry) error { | ||
| // Read the JSON file | ||
| data, err := os.ReadFile(filePath) | ||
| // Updates each metadata file with the new commit shas | ||
| func updateMetadataFile(file File, commitMap map[string]string) error { | ||
| var dataMap []interface{} | ||
| data, err := os.ReadFile(file.FilePath) | ||
| if err != nil { | ||
| return fmt.Errorf("Error reading data: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| var dataMap interface{} | ||
| err = json.Unmarshal(data, &dataMap) | ||
| if err != nil { | ||
| return fmt.Errorf("Error unmarshaling data: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| // Iterate over the commit map and replace the old commit hashes with the new ones | ||
| for _, commit := range *commitMap { | ||
| replaceSHA(dataMap, commit.Old, commit.New) | ||
| // Processes each of the different file types contained in the archive. | ||
| // The file types listed below are currently the only types that contain | ||
| // commit shas as a distinct field. | ||
| switch { | ||
| case file.Prefix == "pull_requests": | ||
| updatePullRequests(commitMap, &dataMap) | ||
| case file.Prefix == "pull_request_review_comments": | ||
| updatePullRequestReviewComments(commitMap, &dataMap) | ||
| case file.Prefix == "pull_request_reviews": | ||
| updatePullRequestReviews(commitMap, &dataMap) | ||
| case file.Prefix == "pull_request_review_threads": | ||
| updatePullRequestReviewThreads(commitMap, &dataMap) | ||
| case file.Prefix == "commit_comments": | ||
| updateCommitComments(commitMap, &dataMap) | ||
| default: | ||
| return fmt.Errorf("no supported rewrite found for file type: %s", file.Prefix) | ||
| } | ||
|
|
||
| // Marshal the updated data to JSON and pretty print it | ||
| // Pretty print the data | ||
| updatedData, err := json.MarshalIndent(dataMap, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("Error marshaling updated data: %v", err) | ||
| return fmt.Errorf("error marshaling updated data: %v", err) | ||
| } | ||
|
|
||
| // Overwrite the original file with the updated data | ||
| err = os.WriteFile(filePath, updatedData, 0644) | ||
| err = os.WriteFile(file.FilePath, updatedData, 0644) | ||
| if err != nil { | ||
| return fmt.Errorf("Error writing updated data: %v", err) | ||
| return fmt.Errorf("error writing updated data: %v", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func replaceSHA(data interface{}, oldSHA string, newSHA string) { | ||
| if data == nil { | ||
| return | ||
| } | ||
|
|
||
| switch v := data.(type) { | ||
| case map[string]interface{}: | ||
| for key, value := range v { | ||
| if str, ok := value.(string); ok && str == oldSHA { | ||
| v[key] = newSHA | ||
| } else { | ||
| replaceSHA(value, oldSHA, newSHA) | ||
| } | ||
| // Fetches all of the files to update based on the file prefixes | ||
| func getAllFilesToProcess(prefixes []string, archiveLocation string) []File { | ||
| var files []File | ||
| for _, prefix := range prefixes { | ||
| filePaths, err := filepath.Glob(filepath.Join(archiveLocation, prefix+"_*.json")) | ||
| for _, filePath := range filePaths { | ||
| files = append(files, File{ | ||
| FilePath: filePath, | ||
| Prefix: prefix, | ||
| }) | ||
| } | ||
| case []interface{}: | ||
| for i, value := range v { | ||
| if str, ok := value.(string); ok && str == oldSHA { | ||
| v[i] = newSHA | ||
| } else { | ||
| replaceSHA(value, oldSHA, newSHA) | ||
| } | ||
| if err != nil { | ||
| log.Fatalf("error getting files: %v", err) | ||
| } | ||
| default: | ||
| // Unsupported type, do nothing | ||
| } | ||
| return files | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.