-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} 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.')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
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; | ||
}; |
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; | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about now? 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool 👍