Skip to content

Commit b2dfdf3

Browse files
committed
Update pull with stash command
1 parent 35daa8b commit b2dfdf3

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/commands/checkoutToCommand/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class CheckoutToCommand extends BaseCommand {
297297
try {
298298
if (isWorkdirHasChanges) {
299299
const stashMessage = getStashMessage(currentBranch);
300-
await git.createStash(stashMessage, true);
300+
await git.createStash(stashMessage);
301301
}
302302
} catch (e) {
303303
if (e instanceof Error) {
@@ -350,7 +350,7 @@ export class CheckoutToCommand extends BaseCommand {
350350

351351
try {
352352
if (isWorkdirHasChanges) {
353-
await git.createStash(stashMessage, true);
353+
await git.createStash(stashMessage);
354354
}
355355
} catch (e) {
356356
if (e instanceof Error) {

src/commands/pullWithStashCommand/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ export class PullWithStashCommand extends BaseCommand {
1818
const currentBranch = await git.getCurrentBranch();
1919
const stashMessage = getStashMessage(currentBranch, true);
2020
// stash
21-
const isWorkdirHasChanges = await git.isWorkdirHasChanges();
22-
if (isWorkdirHasChanges) {
23-
await git.createStash(stashMessage, true);
21+
const isWorkdirHasChangesBeforeStash = await git.isWorkdirHasChanges();
22+
if (isWorkdirHasChangesBeforeStash) {
23+
await git.createStash(stashMessage);
2424
}
2525

2626
// pull
2727
await git.pullFromRemoteBranch();
2828

2929
// pop latest stash
30-
if (isWorkdirHasChanges) {
30+
if (isWorkdirHasChangesBeforeStash) {
31+
// check for auto generated files that running application might generate after pull
32+
const isWorkdirHasChanges = await git.isWorkdirHasChanges();
33+
if (isWorkdirHasChanges) {
34+
// if such changes present, reset them
35+
await git.resetLocalChanges();
36+
}
37+
38+
// .. proceed to stash pop
3139
await git.popStash(stashMessage);
3240
}
3341
}

src/common/git/gitExecutor.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,14 @@ export class GitExecutor {
8484
return stdout;
8585
}
8686

87-
async createStash(stashName: string, includeUntracked: boolean) {
88-
const command = `git stash push -m "${stashName}" ${includeUntracked ? '-u' : ''}`;
87+
async createStash(stashName: string, include: 'all' | 'untracked' | 'none' = 'untracked') {
88+
const commandArr = [
89+
`git stash push -m "${stashName}"`,
90+
...(include === 'all' ? ['-a'] : []),
91+
...(include === 'untracked' ? ['-u'] : []),
92+
];
93+
94+
const command = commandArr.join(' ');
8995

9096
const { stdout } = await this.#execGitCommand(command);
9197

@@ -94,6 +100,13 @@ export class GitExecutor {
94100
}
95101
}
96102

103+
async resetLocalChanges() {
104+
// Discard all local changes
105+
const command = 'git restore .';
106+
107+
await this.#execGitCommand(command);
108+
}
109+
97110
async getAllRefListExtended(fetchRemotes = false): Promise<IGitRef[]> {
98111
if (fetchRemotes) {
99112
await this.fetchAllRemoteBranchesAndTags();

0 commit comments

Comments
 (0)