Skip to content

Enable flow to reorder commits - #12384

Merged
sergiou87 merged 5 commits into
developmentfrom
reorder-commits-flow
Jun 7, 2021
Merged

sergiou87 merged 5 commits into
developmentfrom
reorder-commits-flow

Conversation

@sergiou87

@sergiou87 sergiou87 commented Jun 7, 2021 •

Copy link
Copy Markdown
Member

Closes #12299

Description

This PR enables the flow to reorder commits, handling conflicts and undo. I refactored a few things around undoing squash commits, just to be able to reuse that code for reordering.

However, there are other functions that seemed less easy to reuse, so for now I duplicated them and we'll look for opportunities to refactor later (I'm open to suggestions of course! 😄 )

Screenshots

Screen.Recording.2021-06-07.at.11.52.01.mov

Release notes

Notes: [New] Reorder commits from the History tab

@sergiou87
sergiou87 requested a review from tidy-dev June 7, 2021 09:06
Comment thread app/src/lib/app-state.ts
* Undo state associated with a multi commit operation being performed on a
* repository.
*/
export interface IMultiCommitOperationUndoState {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super happy with the name since it's only used for squash and reordering, but maybe it can be used for others? I haven't spent much time looking into that yet 😳

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It should be able to be used for cherry-picking when it is refactored to multi-commit-operation. I like it!

if (beforeCommit !== null && commit.sha === beforeCommit.sha) {
foundBaseCommitInLog = true
toReplayBeforeBaseCommit.unshift(commit)
toReplayAfterReorder.push(commit)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a mistake here with my logic in #12357 which was putting the "base" commit before the commits you're moving. I adapted the tests in consequence to reflect the actual expected behavior (moved commits should be placed before the base commit).

Comment on lines +6531 to +6546
switch (kind) {
case MultiCommitOperationKind.Squash:
bannerType = BannerType.SquashUndone
break
case MultiCommitOperationKind.Reorder:
bannerType = BannerType.ReorderUndone
break
case MultiCommitOperationKind.Rebase:
case MultiCommitOperationKind.CherryPick:
case MultiCommitOperationKind.Merge:
throw new Error(
`Unexpected multi commit operation kind to undo ${kind}`
)
default:
assertNever(kind, `Unsupported multi operation kind to undo ${kind}`)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, maybe we can use this for rebase/cherry-picking/merge? Haven't looked into it yet! 😳

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for cherry-picking.. rebase and merge currently don't support undo.. but maybe they could...

Comment thread app/src/models/banner.ts
Comment on lines +97 to +108
| {
readonly type: BannerType.SuccessfulReorder
/** number of commits reordered */
readonly count: number
/** callback to run when user clicks undo link in banner */
readonly onUndo: () => void
}
| {
readonly type: BannerType.ReorderUndone
/** number of commits reordered */
readonly commitsCount: number
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could have just one banner for all the multi commit operations too? 🤔

@tidy-dev tidy-dev Jun 7, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I think I was heading that way.. but didn't get there on the refactor. :P I am all for it. (Maybe a future PR tho)

}

private logHowToRevertSquash(tip: IValidBranch): void {
private logHowToRevertMultiCommitOperation(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to reuse this one for rebase but I saw some differences, so I stopped there since I didn't want to break anything (else) 😂

Comment on lines +387 to +401
// The base commit index will be in row - 1, because row is the position
// where the new item should be inserted, and commits have a reverse order
// (newer commits are in lower row values) in the list.
const baseCommitIndex = row === 0 ? null : row - 1

if (
this.props.commitSHAs.length === 0 ||
(baseCommitIndex !== null &&
baseCommitIndex > this.props.commitSHAs.length)
) {
return
}

const baseCommitSHA =
row < this.props.commitSHAs.length ? this.props.commitSHAs[row] : null
baseCommitIndex === null ? null : this.props.commitSHAs[baseCommitIndex]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also part of the mistake in the logic I mentioned earlier. This logic was wrong, but since the reorder.ts logic was wrong too, everything seemed to work… until you try to put a commit at the beginning or the end of the history 😅

Comment thread app/src/lib/app-state.ts
* This will be set to null if no multi-commit operation has been initiated.
*/
readonly squashBranchName: string | null
readonly branchName: string | null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null discussion again.. since we refactored MultiCommitOperation to be | null should we do that with multiCommitOperationUndoState

@tidy-dev tidy-dev left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Awesome! - Love how you changed the squashing stuff to multi commit (where my mind was going with it, but wasn't sure yet if those things were reusable) Tested it out and works great!

I had one commit about changing the multiCommitOperationUndoState to a | null like we did for multiCommitOperation ... but this would involve creating an initializer function (and maybe a clear state function) which since this state only has 2 properties may be this is just as "messy" as the null checks. Thus, I good either way with that.

@sergiou87

Copy link
Copy Markdown
Member Author

I thought about it but didn't actually do it for some reason 🤔 I'll raise another PR with it if you don't mind 😄

@sergiou87
sergiou87 merged commit 10f5596 into development Jun 7, 2021
@sergiou87
sergiou87 deleted the reorder-commits-flow branch June 7, 2021 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reorder commits in commit history

2 participants