Skip to content

Commit

Permalink
Merge pull request #34 from alexiszamanidis/feat-auto-push
Browse files Browse the repository at this point in the history
feat(auto-push): add auto-push option
  • Loading branch information
alexiszamanidis authored Sep 9, 2023
2 parents c2b2a7a + d22f5be commit affa9cc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
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;

1 comment on commit affa9cc

@github-actions
Copy link

Choose a reason for hiding this comment

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

Lines Statements Branches Functions
Coverage: 81%
88.23% (15/17) 100% (0/0) 80% (4/5)

Please sign in to comment.