Handler for the `git checkout` command.
(rawArgs: string[])
| 168 | |
| 169 | /** Handler for the `git checkout` command. */ |
| 170 | private _checkout(rawArgs: string[]) { |
| 171 | const { |
| 172 | target, |
| 173 | B: createBranch, |
| 174 | detach: detached, |
| 175 | } = yargs(rawArgs) |
| 176 | .command('$0 <target>', false) |
| 177 | .positional('target', {demandOption: true, type: 'string'}) |
| 178 | .option('detach', {boolean: true}) |
| 179 | .option('q', {boolean: true}) |
| 180 | .option('B', {boolean: true}) |
| 181 | .parseSync(); |
| 182 | |
| 183 | if (target === 'FETCH_HEAD') { |
| 184 | if (this.fetchHeadRef === null) { |
| 185 | throw Error('Unexpectedly trying to check out "FETCH_HEAD". Not fetch head set.'); |
| 186 | } |
| 187 | this.head = {ref: this.fetchHeadRef, newCommits: []}; |
| 188 | } else if (this.branches[target]) { |
| 189 | this.head = this._cloneHead(this.branches[target], detached); |
| 190 | } else if (createBranch) { |
| 191 | this.head = this.branches[target] = { |
| 192 | branch: target, |
| 193 | ...this._cloneHead(this.head, detached), |
| 194 | }; |
| 195 | } else { |
| 196 | throw Error(`Unexpected branch checked out: ${target}`); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Unwraps a refspec into the base and target ref names. |