Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ git tidy
git work add <name>
```

- list the current repo worktrees, select one to print its path

```bash
git work list
cd "$(git work list)" # jump to the selected worktree
```

- list the current repo worktrees, multi select the ones you want to remove

```bash
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@labset/git-workspaces",
"version": "5.0.0",
"version": "5.1.0",
"description": "git workspace cli extensions",
"preferGlobal": false,
"bin": {
Expand Down
11 changes: 10 additions & 1 deletion src/bin/git-work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
addWorktree,
handleCliError,
listWorktrees,
selectAndRemoveWorktrees
selectAndRemoveWorktrees,
selectWorktree
} from '../lib';

const program = new Command();
Expand All @@ -35,6 +36,14 @@ program
await addWorktree(name);
});

program
.command('list')
.description('list worktrees and select one to print its path')
.action(async () => {
const selected = await listWorktrees().then(selectWorktree);
if (selected) console.log(selected);
});

program
.command('remove')
.description('list worktrees and select the ones to remove')
Expand Down
1 change: 1 addition & 0 deletions src/lib/worktrees/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
export * from './list-worktrees';
export * from './add-worktree';
export * from './select-and-remove-worktrees';
export * from './select-worktree';
43 changes: 43 additions & 0 deletions src/lib/worktrees/select-worktree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2023 Hasnae Rehioui
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import inquirer from 'inquirer';

import { Worktree } from './list-worktrees';

export const selectWorktree = async (
worktrees: Worktree[]
): Promise<string | undefined> => {
if (worktrees.length === 0) return undefined;
// Prompt UI goes to stderr so stdout carries only the selected path and
// can be used as `cd "$(git work list)"`.
const answer = await inquirer.createPromptModule({
output: process.stderr
})([
{
name: 'selectedPath',
type: 'select',
message: 'choose a worktree',
choices: worktrees.map((worktree) => ({
name: worktree.branch
? `${worktree.path} (${worktree.branch})`
: worktree.path,
value: worktree.path
})),
pageSize: 10
}
]);
return answer.selectedPath;
};
Loading