-
Notifications
You must be signed in to change notification settings - Fork 0
Add Merge Command #116
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
Merged
Add Merge Command #116
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| /* | ||
| * Copyright © 2025 Martin Strobel | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/marstr/baronial/internal/index" | ||
| "github.com/marstr/envelopes" | ||
| "github.com/marstr/envelopes/persist" | ||
| "github.com/marstr/envelopes/persist/filesystem" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type MergeParameters struct { | ||
| Comment string `json:"comment,omitempty"` | ||
| Parents []envelopes.ID `json:"parent_ids"` | ||
| ParentNames []persist.RefSpec `json:"parent_names"` | ||
| } | ||
|
|
||
| var mergeCmd = &cobra.Command{ | ||
| Use: "merge {refspec} [refspec]...", | ||
| Args: cobra.MinimumNArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| ctx, cancel := RootContext(cmd) | ||
| defer cancel() | ||
|
|
||
| root, err := index.RootDirectory(".") | ||
| if err != nil { | ||
| logrus.Fatal(err) | ||
| } | ||
| repoLoc := filepath.Join(root, index.RepoName) | ||
|
|
||
| var repo persist.RepositoryReader | ||
| repo, err = filesystem.OpenRepositoryWithCache(ctx, repoLoc, 10000) | ||
| if err != nil { | ||
| logrus.Fatal(err) | ||
| } | ||
|
|
||
| var currentHead persist.RefSpec | ||
| currentHead, err = repo.Current(ctx) | ||
| if err != nil { | ||
| logrus.Fatal("couldn't read what's currently checked out because: ", err) | ||
| } | ||
|
|
||
| heads := append([]persist.RefSpec{currentHead}, stringsToRefSpecs(args)...) | ||
|
|
||
| var inProg bool | ||
| inProg, err = MergeIsInProgress(ctx, repoLoc) | ||
| if err != nil { | ||
| logrus.Warn("couldn't see if previous merge is in progress because: ", err) | ||
| } | ||
|
|
||
| var mergeParams MergeParameters | ||
|
|
||
| if inProg { | ||
| err = MergeUnstowProgress(ctx, repoLoc, &mergeParams) | ||
| if err != nil { | ||
| logrus.Fatal("couldn't read the currently in-progress merge because: ", err) | ||
| } | ||
| } | ||
|
|
||
| mergeParams.ParentNames = append(mergeParams.ParentNames, heads...) | ||
| for _, head := range heads { | ||
| var id envelopes.ID | ||
| id, err = persist.Resolve(ctx, repo, head) | ||
| if err != nil { | ||
| logrus.Fatalf("couldn't resolve head %q because: %v", head, err) | ||
| } | ||
|
|
||
| mergeParams.Parents = append(mergeParams.Parents, id) | ||
| } | ||
|
|
||
| mergeParams.Comment = fmt.Sprintf( | ||
| "Merging %s into %s", | ||
| strings.Join(refSpecsToStrings(mergeParams.ParentNames)[1:], ", "), | ||
| string(mergeParams.ParentNames[0]), | ||
| ) | ||
|
|
||
| err = MergeStowProgress(ctx, repoLoc, mergeParams) | ||
| if err != nil { | ||
| logrus.Fatal(err) | ||
| } | ||
|
|
||
| var merged envelopes.State | ||
| merged, err = persist.Merge(ctx, repo, heads) | ||
| if err != nil { | ||
| logrus.Fatal(err) | ||
| } | ||
|
|
||
| err = index.CheckoutState(ctx, &merged, repoLoc, 0660) | ||
| if err != nil { | ||
| logrus.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Println("Merge complete. Please check balances for accuracy, make any necessary reverts, and commit.") | ||
| }, | ||
| } | ||
|
|
||
| func MergeIsInProgress(_ context.Context, repoLoc string) (bool, error) { | ||
| _, err := os.Stat(getMergeParamsLoc(repoLoc)) | ||
| if err == nil { | ||
| return true, nil | ||
| } else if os.IsNotExist(err) { | ||
| return false, nil | ||
| } else { | ||
| return false, err | ||
| } | ||
| } | ||
|
|
||
| func MergeStowProgress(_ context.Context, repoLoc string, parameters MergeParameters) error { | ||
| const filePermissions = 0660 | ||
| toWrite, err := json.Marshal(parameters) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't marshal merge parameters: %w", err) | ||
| } | ||
|
|
||
| err = os.WriteFile(getMergeParamsLoc(repoLoc), toWrite, filePermissions) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't write merge parameter file: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func MergeUnstowProgress(_ context.Context, repoLoc string, destination *MergeParameters) error { | ||
| contents, err := os.ReadFile(getMergeParamsLoc(repoLoc)) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't read merge parameter file: %w", err) | ||
| } | ||
|
|
||
| err = json.Unmarshal(contents, destination) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't parse the merge parameter json: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func MergeResetProgress(_ context.Context, repoLoc string) error { | ||
| return os.Remove(getMergeParamsLoc(repoLoc)) | ||
| } | ||
|
|
||
marstr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func getMergeParamsLoc(repoLoc string) string { | ||
| return filepath.Join(repoLoc, "merge.json") | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(mergeCmd) | ||
| } | ||
|
|
||
| func stringsToRefSpecs(before []string) []persist.RefSpec { | ||
| after := make([]persist.RefSpec, len(before)) | ||
| for i := range before { | ||
| after[i] = persist.RefSpec(before[i]) | ||
| } | ||
| return after | ||
| } | ||
|
|
||
| func refSpecsToStrings(before []persist.RefSpec) []string { | ||
| after := make([]string, len(before)) | ||
| for i := range before { | ||
| after[i] = string(before[i]) | ||
| } | ||
| return after | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ParentNames slice is being appended to even when a merge is already in progress. This will duplicate the current head and other refspecs on subsequent merge command invocations. The append should only happen when not in progress, or the slice should be reset first.