| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Dag} from '../previews'; |
| 9 | import type {ExactRevset, OptimisticRevset, SucceedableRevset} from '../types'; |
| 10 | |
| 11 | import {Operation} from './Operation'; |
| 12 | |
| 13 | export class BookmarkCreateOperation extends Operation { |
| 14 | /** |
| 15 | * @param bookmark local bookmark name to create. Should NOT be a remote bookmark or stable location. |
| 16 | */ |
| 17 | constructor( |
| 18 | private revset: SucceedableRevset | ExactRevset | OptimisticRevset, |
| 19 | private bookmark: string, |
| 20 | ) { |
| 21 | super('BookmarkCreateOperation'); |
| 22 | } |
| 23 | |
| 24 | static opName = 'BookmarkCreate'; |
| 25 | |
| 26 | getArgs() { |
| 27 | return ['bookmark', this.bookmark, '--rev', this.revset]; |
| 28 | } |
| 29 | |
| 30 | optimisticDag(dag: Dag): Dag { |
| 31 | const commit = dag.resolve(this.bookmark); |
| 32 | if (commit) { |
| 33 | return dag.replaceWith(commit.hash, (_h, c) => |
| 34 | c?.merge({ |
| 35 | bookmarks: [...commit.bookmarks, this.bookmark], |
| 36 | }), |
| 37 | ); |
| 38 | } |
| 39 | return dag; |
| 40 | } |
| 41 | } |
| 42 | |