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

Check for diffBranch existence before fetch files #115

Merged
merged 3 commits into from
May 15, 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
45 changes: 37 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ if (process.stdout.isTTY) {
const { userConfig, execSyncProcess } = require("./utils/common");
const resolveMainTask = require("./utils/resolveMainTask");
const fetchGitDiff = require("./utils/fetchGitDiff");
const checkForBranchExistence = require('./utils/checkForBranchExistence');
const getAllTrackedFiles = require('./utils/getAllTrackedFiles');

if(!userConfig) {
process.exitCode = 1;
Expand Down Expand Up @@ -75,13 +77,14 @@ if (process.stdout.isTTY) {

// set base branch as the default diff branch
let diffBranch = baseBranch;
let remote = '';

// Finding the remote name, if current branch is base branch
if(currentBranch === baseBranch) {
debug('Fetching the remote of base branch since current branch and base branch is same');
try {
const getRemote = 'git remote | head -1';
let remote = execSyncProcess(getRemote);
remote = execSyncProcess(getRemote);
diffBranch = `${remote}/${baseBranch}`;
debug('Remote of base branch: ', remote);
}
Expand All @@ -91,19 +94,45 @@ if (process.stdout.isTTY) {
debug('Branch to Diff: ', diffBranch);
}

let committedGitFiles = [];

let isdiffBranchExisted = false;
try {
committedGitFiles = fetchGitDiff(diffBranch);
debug('Committed GIT files: ', committedGitFiles);
}
catch(err) {
isdiffBranchExisted = checkForBranchExistence(baseBranch,remote);
debug('Check whether branch is existed: ', diffBranch);
Copy link
Owner

Choose a reason for hiding this comment

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

This PR is good to go with a success log stating that the base branch is existing.

Copy link
Owner

Choose a reason for hiding this comment

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

It would make the debugging process a bit easier.

Copy link
Author

@WhiteYin WhiteYin May 14, 2020

Choose a reason for hiding this comment

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

How about now? 😊

This PR is good to go with a success log stating that the base branch is existing.

Copy link
Owner

Choose a reason for hiding this comment

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

Cool 👍

} catch (err) {
process.exitCode = 1;
log(warning('\nFetching committed file list process has been stopped with the following error\n'));
log(warning('\nCheck for diffBranch existence process has been stopped with the following error\n'));
log(err);
return;
}

let committedGitFiles = [];
if (isdiffBranchExisted) {
log(success('Great, we find the ' + diffBranch + ' branch.'));
Copy link
Owner

@theenadayalank theenadayalank May 15, 2020

Choose a reason for hiding this comment

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

Unfortunately, I have merged this PR without noticing this line. I meant to add the log in debug mode. It's my fault that I didn't communicate well. I'm sorry for the inconvenience.

If you don't mind, could you raise another PR with a log message which prints only in debug mode?

If the base branch is found, you can put a log in debug mode. (something like Base branch exists)
If not found, a warning log can be printed in normal mode. (something like below)

⚠️The base branch(branch_name) doesn't exist
Hence all the files will be considered

Copy link
Author

Choose a reason for hiding this comment

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

Well,that's my fault.I will fix it later.

try {
committedGitFiles = fetchGitDiff(diffBranch);
debug('Committed GIT files: ', committedGitFiles);
} catch (err) {
process.exitCode = 1;
log(warning('\nFetching committed file list process has been stopped with the following error\n'));
log(err);
return;
}
}
// if diffBranch is not existed,get all tracked files on currentBranch to lint
else {
log(warning('Sorry, we cannot find the ' + diffBranch + ' branch.'));
log(warning('So,we use ' + currentBranch + 'branch instead'));
try {
committedGitFiles = getAllTrackedFiles(currentBranch);
debug('Tracked files: ', committedGitFiles);
} catch (err) {
process.exitCode = 1;
log(warning('\nGetting tracked file list process has been stopped with the following error\n'));
log(err);
return;
}
}

new Listr(resolveMainTask({ tasks, committedGitFiles }), {
exitOnError: false,
concurrent: true,
Expand Down
12 changes: 12 additions & 0 deletions utils/checkForBranchExistence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { execSyncProcess } = require('./common');

module.exports = function checkForBranchExistence(branch = 'master', remote = '') {
// check whether branch exists local
let command = `git branch --list ${branch}`;
if (remote) {
// check whether branch exists in remote repo
command = `git ls-remote --heads ${remote} ${branch}`;
}
let isExisted = execSyncProcess(command) || '';
return !!isExisted;
};
9 changes: 9 additions & 0 deletions utils/getAllTrackedFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { execSyncProcess } = require('./common');

module.exports = function getAllTrackedFiles(branch = 'master') {
let command = `git ls-tree -r ${branch} --name-only`;

let committedGitFiles = execSyncProcess(command) || '';
committedGitFiles = committedGitFiles.split('\n');
return committedGitFiles;
};