Skip to content

Commit

Permalink
fix(plugin-stage): stage and unstage correctly (#1873)
Browse files Browse the repository at this point in the history
* fix(plugin-stage): implement staging

* don't use -N when just staging

* fix unstaging

* fix undefined check

* fix releases

* remove extra line

* refactor boolean parameter
  • Loading branch information
andreialecu authored Sep 27, 2020
1 parent 4c1c320 commit 8e4c097
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .yarn/versions/13d07961.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
releases:
"@yarnpkg/plugin-stage": patch

declined:
- "@yarnpkg/plugin-node-modules"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-pnp"
14 changes: 11 additions & 3 deletions packages/plugin-stage/sources/commands/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,20 @@ export default class StageCommand extends BaseCommand {
}
}
} else {
if (changeList.length === 0) {
if (this.reset) {
const stagedChangeList = await driver.filterChanges(root, yarnPaths, yarnNames, {staged: true});
if (stagedChangeList.length === 0) {
this.context.stdout.write(`No staged changes found!`);
} else {
await driver.makeReset(root, stagedChangeList);
}
} else if (changeList.length === 0) {
this.context.stdout.write(`No changes found!`);
} else if (this.commit) {
await driver.makeCommit(root, changeList, commitMessage);
} else if (this.reset) {
await driver.makeReset(root, changeList);
} else {
await driver.makeStage(root, changeList);
this.context.stdout.write(commitMessage);
}
}
}
Expand Down
52 changes: 33 additions & 19 deletions packages/plugin-stage/sources/drivers/GitDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,29 @@ async function genCommitMessage(cwd: PortablePath, changes: Array<stageUtils.Fil
return message;
}

const unstagedPrefixes = {
[stageUtils.ActionType.CREATE]: [` A `, `?? `],
[stageUtils.ActionType.MODIFY]: [` M `],
[stageUtils.ActionType.DELETE]: [` D `],
};

const stagedPrefixes = {
[stageUtils.ActionType.CREATE]: [`A `],
[stageUtils.ActionType.MODIFY]: [`M `],
[stageUtils.ActionType.DELETE]: [`D `],
};

export const Driver = {
async findRoot(cwd: PortablePath) {
return await stageUtils.findVcsRoot(cwd, {marker: `.git` as Filename});
},

async filterChanges(cwd: PortablePath, yarnRoots: Set<PortablePath>, yarnNames: Set<string>) {
async filterChanges(cwd: PortablePath, yarnRoots: Set<PortablePath>, yarnNames: Set<string>, opts?: { staged?: boolean }) {
const {stdout} = await execUtils.execvp(`git`, [`status`, `-s`], {cwd, strict: true});
const lines = stdout.toString().split(/\n/g);

const changePrefix = opts?.staged ? stagedPrefixes : unstagedPrefixes;

const changes = ([] as Array<stageUtils.FileAction>).concat(...lines.map((line: string) => {
if (line === ``)
return [];
Expand All @@ -107,28 +121,22 @@ export const Driver = {
const path = ppath.resolve(cwd, line.slice(3) as PortablePath);

// New directories need to be expanded to their content
if (prefix === `?? ` && line.endsWith(`/`)) {
if (!opts?.staged && prefix === `?? ` && line.endsWith(`/`)) {
return stageUtils.expandDirectory(path).map(path => ({
action: stageUtils.ActionType.CREATE,
path,
}));
} else if (prefix === ` A ` || prefix === `?? `) {
return [{
action: stageUtils.ActionType.CREATE,
path,
}];
} else if (prefix === ` M `) {
return [{
action: stageUtils.ActionType.MODIFY,
path,
}];
} else if (prefix === ` D `) {
return [{
action: stageUtils.ActionType.DELETE,
path,
}];
}
else {
} else {
const actions = [stageUtils.ActionType.CREATE, stageUtils.ActionType.MODIFY, stageUtils.ActionType.DELETE] as const;
const action = actions.find(action => changePrefix[action].includes(prefix));

if (action !== undefined) {
return [{
action,
path,
}];
}

return [];
}
}));
Expand All @@ -145,6 +153,12 @@ export const Driver = {
return await genCommitMessage(cwd, changeList);
},

async makeStage(cwd: PortablePath, changeList: Array<stageUtils.FileAction>) {
const localPaths = changeList.map(file => npath.fromPortablePath(file.path));

await execUtils.execvp(`git`, [`add`, `--`, ...localPaths], {cwd, strict: true});
},

async makeCommit(cwd: PortablePath, changeList: Array<stageUtils.FileAction>, commitMessage: string) {
const localPaths = changeList.map(file => npath.fromPortablePath(file.path));

Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-stage/sources/drivers/MercurialDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const Driver = {
return ``;
},

async makeStage(cwd: PortablePath, changeList: Array<stageUtils.FileAction>) {
},

async makeCommit(cwd: PortablePath, changeList: Array<stageUtils.FileAction>, commitMessage: string) {
},

Expand Down

0 comments on commit 8e4c097

Please sign in to comment.