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

Do not automatically stage files #220

Merged
merged 5 commits into from
Jul 23, 2020
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
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"prepublishOnly": "tsc --project ./tsconfig.prod.json",
"test": "jest",
"test-private": "jest --config ./jest.config.private.js",
"test-all": "yarn test && yarn test-private",
"test-all": "yarn lint && yarn test && yarn test-private",
"start": "ts-node --transpile-only ./src/entrypoint.cli.ts"
},
"lint-staged": {
Expand Down Expand Up @@ -63,26 +63,28 @@
"node": ">=10.0.0"
},
"dependencies": {
"@types/lodash.difference": "^4.5.6",
"axios": "^0.19.2",
"dedent": "^0.7.0",
"del": "^5.1.0",
"find-up": "^4.1.0",
"inquirer": "^7.3.1",
"inquirer": "^7.3.3",
"lodash.difference": "^4.5.0",
"lodash.flatmap": "^4.5.0",
"lodash.isempty": "^4.4.0",
"lodash.isstring": "^4.0.1",
"lodash.uniq": "^4.5.0",
"make-dir": "^3.1.0",
"ora": "^4.0.4",
"safe-json-stringify": "^1.2.0",
"strip-json-comments": "^3.1.0",
"strip-json-comments": "^3.1.1",
"winston": "^3.3.3",
"yargs": "^15.4.0"
},
"devDependencies": {
"@types/core-js": "^2.5.2",
"@types/dedent": "^0.7.0",
"@types/inquirer": "^6.5.0",
"@types/inquirer": "^7.3.0",
"@types/jest": "^26.0.4",
"@types/lodash": "^4.14.157",
"@types/lodash.flatmap": "^4.5.6",
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/runWithOptions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Array [
},
],
Array [
"git checkout mySourceBranch && git branch -D backport/6.x/commit-2e63475c",
"git reset --hard && git checkout mySourceBranch && git branch -D backport/6.x/commit-2e63475c",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
Expand Down
22 changes: 11 additions & 11 deletions src/services/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { BackportOptions } from '../options/options';
import * as childProcess from '../services/child-process-promisified';
import {
addRemote,
getUnmergedFiles,
getUnstagedFiles,
finalizeCherrypick,
deleteRemote,
cherrypick,
getFilesWithConflicts,
getConflictingFiles,
createBackportBranch,
} from '../services/git';
import { ExecError } from '../test/ExecError';
Expand All @@ -15,7 +15,7 @@ beforeEach(() => {
jest.clearAllMocks();
});

describe('getUnmergedFiles', () => {
describe('getUnstagedFiles', () => {
it('should split lines and remove empty', async () => {
jest.spyOn(childProcess, 'exec').mockResolvedValueOnce({
stdout: 'conflicting-file.txt\nconflicting-file2.txt\n',
Expand All @@ -27,7 +27,7 @@ describe('getUnmergedFiles', () => {
repoName: 'kibana',
} as BackportOptions;

await expect(await getUnmergedFiles(options)).toEqual([
await expect(await getUnstagedFiles(options)).toEqual([
'/myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt',
'/myHomeDir/.backport/repositories/elastic/kibana/conflicting-file2.txt',
]);
Expand All @@ -44,11 +44,11 @@ describe('getUnmergedFiles', () => {
repoName: 'kibana',
} as BackportOptions;

await expect(await getUnmergedFiles(options)).toEqual([]);
await expect(await getUnstagedFiles(options)).toEqual([]);
});
});

describe('getFilesWithConflicts', () => {
describe('getConflictingFiles', () => {
it('should split by linebreak and remove empty and duplicate items', async () => {
const err = {
killed: false,
Expand All @@ -66,7 +66,7 @@ describe('getFilesWithConflicts', () => {
repoName: 'kibana',
} as BackportOptions;

expect(await getFilesWithConflicts(options)).toEqual([
expect(await getConflictingFiles(options)).toEqual([
'/myHomeDir/.backport/repositories/elastic/kibana/conflicting-file.txt',
]);
});
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('cherrypick', () => {
})
)

// mock getFilesWithConflicts
// mock getConflictingFiles
.mockRejectedValueOnce(
new ExecError({
code: 2,
Expand All @@ -237,7 +237,7 @@ describe('cherrypick', () => {
})
)

// mock getUnmergedFiles
// mock getUnstagedFiles
.mockResolvedValueOnce({ stdout: '', stderr: '' });

expect(await cherrypick(options, commit)).toEqual({
Expand Down Expand Up @@ -313,10 +313,10 @@ Or refer to the git documentation for more information: https://git-scm.com/docs
// mock cherry pick command
.mockRejectedValueOnce(new Error('non-cherrypick error'))

// getFilesWithConflicts
// getConflictingFiles
.mockResolvedValueOnce({ stdout: '', stderr: '' })

// getUnmergedFiles
// getUnstagedFiles
.mockResolvedValueOnce({ stdout: '', stderr: '' });

await expect(
Expand Down
22 changes: 10 additions & 12 deletions src/services/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ export async function cherrypick(
}

const isCherryPickError = e.cmd === cmd;
const hasConflicts = !isEmpty(await getFilesWithConflicts(options));
const hasUnmergedFiles = !isEmpty(await getUnmergedFiles(options));
const hasConflicts = !isEmpty(await getConflictingFiles(options));
const hasUnstagedFiles = !isEmpty(await getUnstagedFiles(options));

if (isCherryPickError && (hasConflicts || hasUnmergedFiles)) {
if (isCherryPickError && (hasConflicts || hasUnstagedFiles)) {
return { needsResolving: true };
}

Expand Down Expand Up @@ -170,7 +170,7 @@ export async function finalizeCherrypick(options: BackportOptions) {
}
}

export async function getFilesWithConflicts(options: BackportOptions) {
export async function getConflictingFiles(options: BackportOptions) {
const repoPath = getRepoPath(options);
try {
await exec(`git --no-pager diff --check`, { cwd: repoPath });
Expand Down Expand Up @@ -200,15 +200,17 @@ export async function getFilesWithConflicts(options: BackportOptions) {
}

// retrieve the list of files that could not be cleanly merged
export async function getUnmergedFiles(options: BackportOptions) {
export async function getUnstagedFiles(options: BackportOptions) {
const repoPath = getRepoPath(options);
const res = await exec(`git --no-pager diff --name-only --diff-filter=U`, {
const res = await exec(`git --no-pager diff --name-only`, {
cwd: repoPath,
});
return res.stdout
const files = res.stdout
.split('\n')
.filter((file) => !!file)
.map((file) => pathResolve(repoPath, file));

return uniq(files);
}

export async function setCommitAuthor(
Expand All @@ -229,10 +231,6 @@ export async function setCommitAuthor(
}
}

export async function addUnstagedFiles(options: BackportOptions) {
return exec(`git add --update`, { cwd: getRepoPath(options) });
}

// How the commit flows:
// ${sourceBranch} -> ${backportBranch} -> ${targetBranch}
// Example:
Expand Down Expand Up @@ -282,7 +280,7 @@ export async function deleteBackportBranch({
const spinner = ora().start();

await exec(
`git checkout ${options.sourceBranch} && git branch -D ${backportBranch}`,
`git reset --hard && git checkout ${options.sourceBranch} && git branch -D ${backportBranch}`,
{ cwd: getRepoPath(options) }
);

Expand Down
4 changes: 0 additions & 4 deletions src/services/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export async function promptForCommits({
});

const res = await prompt<CommitChoice[]>({
// loop was recently added and not yet part of the types
//@ts-expect-error
loop: false,
pageSize: 15,
choices: choices,
Expand All @@ -68,8 +66,6 @@ export async function promptForTargetBranches({
isMultipleChoice: boolean;
}): Promise<string[]> {
const res = await prompt<string | string[]>({
// loop was recently added and not yet part of the types
//@ts-expect-error
loop: false,
pageSize: 15,
choices: targetBranchChoices,
Expand Down
5 changes: 3 additions & 2 deletions src/test/integration/__snapshots__/integration.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,9 @@ fatal: No such remote: 'elastic'
"",
],
Array [
"[VERBOSE] exec success 'git checkout master && git branch -D backport/6.0/pr-85':",
"Deleted branch backport/6.0/pr-85 (was <COMMIT HASH>).
"[VERBOSE] exec success 'git reset --hard && git checkout master && git branch -D backport/6.0/pr-85':",
"HEAD is now at <COMMIT HASH> Add witch (#85)
Deleted branch backport/6.0/pr-85 (was <COMMIT HASH>).
",
],
Array [
Expand Down
28 changes: 23 additions & 5 deletions src/ui/__snapshots__/cherrypickAndCreatePullRequest.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Array [
},
],
Array [
"git --no-pager diff --name-only --diff-filter=U",
"git --no-pager diff --name-only",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
Expand All @@ -38,26 +38,44 @@ Array [
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git --no-pager diff --name-only",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git --no-pager diff --check",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git --no-pager diff --name-only",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git --no-pager diff --check",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git --no-pager diff --name-only --diff-filter=U",
"git --no-pager diff --name-only",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git --no-pager diff --check",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
],
Array [
"git add --update",
"git --no-pager diff --name-only",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
Expand All @@ -75,7 +93,7 @@ Array [
},
],
Array [
"git checkout myDefaultSourceBranch && git branch -D backport/6.x/commit-mySha",
"git reset --hard && git checkout myDefaultSourceBranch && git branch -D backport/6.x/commit-mySha",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
Expand Down Expand Up @@ -122,7 +140,7 @@ Array [
},
],
Array [
"git checkout myDefaultSourceBranch && git branch -D backport/6.x/pr-1000_pr-2000",
"git reset --hard && git checkout myDefaultSourceBranch && git branch -D backport/6.x/pr-1000_pr-2000",
Object {
"cwd": "/myHomeDir/.backport/repositories/elastic/kibana",
},
Expand Down
Loading