Skip to content
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

feat(auto-push): add auto-push option #34

Merged
merged 2 commits into from
Sep 9, 2023
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ To select a workspace, simply choose from the list of available workspaces when
| vsCodeGitWorktrees.remove.stalledBranches | boolean | false | Removes local(stalled) branches that do not exist on remote |
| vsCodeGitWorktrees.move.openNewVscodeWindow | boolean | true | Open new vscode window when you switch or create a worktree |
| vsCodeGitWorktrees.worktrees.dir.path | string | null | Define a directory for all worktrees between your projects |
| vsCodeGitWorktrees.add.autoPush | boolean | true | Auto push worktree branch after its creation |
| vsCodeGitWorktrees.add.autoPull | boolean | true | Auto pull worktree branch after its creation |

## Contribution

Expand Down
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@
"description": "Define a directory for all worktrees between your projects"
}
}
},
{
"id": "auto-push",
"title": "Auto push",
"properties": {
"vsCodeGitWorktrees.add.autoPush": {
"type": "boolean",
"default": true,
"description": "Auto push worktree branch after its creation"
}
}
},
{
"id": "auto-pull",
"title": "Auto pull",
"properties": {
"vsCodeGitWorktrees.add.autoPull": {
"type": "boolean",
"default": true,
"description": "Auto pull worktree branch after its creation"
}
}
}
]
},
Expand Down
22 changes: 18 additions & 4 deletions src/helpers/gitWorktreeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
worktreesDirPath,
getWorkspaceFilePath,
shouldOpenNewVscodeWindow,
shouldAutoPushAfterWorktreeCreation,
shouldAutoPullAfterWorktreeCreation,
} from "./helpers";
import { existsRemoteBranch, isBareRepository } from "./gitHelpers";
import { MAIN_WORKTREES } from "../constants/constants";
Expand Down Expand Up @@ -302,6 +304,11 @@ export const existsWorktree = async (workspaceFolder: string, worktree: string)
}
};

export const pushBranch = async (branch: string, workspaceFolder: string) => {
const pushCommand = `git push --set-upstream origin ${branch}`;
await executeCommand(pushCommand, { cwd: workspaceFolder });
};

export const addNewWorktree = async (
workspaceFolder: string,
remoteBranch: string,
Expand All @@ -316,8 +323,9 @@ export const addNewWorktree = async (
const worktreeAddCommand = `git worktree add --track -b ${newBranch} ${newWorktreePath} origin/${remoteBranch}`;
await executeCommand(worktreeAddCommand, { cwd: workspaceFolder });

const pushCommand = `git push --set-upstream origin ${newBranch}`;
await executeCommand(pushCommand, { cwd: workspaceFolder });
if (shouldAutoPushAfterWorktreeCreation) {
await pushBranch(newBranch, workspaceFolder);
}

const newWtInfo = await moveIntoWorktree(workspaceFolder, newWorktreePath);

Expand All @@ -329,6 +337,11 @@ export const addNewWorktree = async (
}
};

export const pullBranch = async (worktreePath: string, workspaceFolder: string) => {
const pullCommand = `git -C ${worktreePath} pull`;
await executeCommand(pullCommand, { cwd: workspaceFolder });
};

export const addRemoteWorktree = async (
workspaceFolder: string,
remoteBranch: string,
Expand All @@ -343,8 +356,9 @@ export const addRemoteWorktree = async (
const worktreeAddCommand = `git worktree add ${newWorktreePath} ${newBranch}`;
await executeCommand(worktreeAddCommand, { cwd: workspaceFolder });

const pullCommand = `git -C ${newWorktreePath} pull`;
await executeCommand(pullCommand, { cwd: workspaceFolder });
if (shouldAutoPullAfterWorktreeCreation) {
await pullBranch(newWorktreePath, workspaceFolder);
}

const newWtInfo = await moveIntoWorktree(workspaceFolder, newWorktreePath);

Expand Down
6 changes: 6 additions & 0 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,9 @@ export const shouldOpenNewVscodeWindow =

export const worktreesDirPath =
vscode.workspace.getConfiguration().get("vsCodeGitWorktrees.worktrees.dir.path") ?? null;

export const shouldAutoPushAfterWorktreeCreation =
vscode.workspace.getConfiguration().get("vsCodeGitWorktrees.add.autoPush") ?? true;

export const shouldAutoPullAfterWorktreeCreation =
vscode.workspace.getConfiguration().get("vsCodeGitWorktrees.add.autoPull") ?? true;