From b8fb066228707cb4ab1eafb72903928efa0ec575 Mon Sep 17 00:00:00 2001 From: hotoo Date: Fri, 1 Jul 2016 12:35:04 +0800 Subject: [PATCH] fix(symbolic-ref): get description for symbolic-ref. --- branches.sh | 3 +++ test/git-br.test.js | 48 ++++++++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/branches.sh b/branches.sh index edb2394..f37d3d6 100755 --- a/branches.sh +++ b/branches.sh @@ -19,6 +19,9 @@ function listBranchWithDescription() { clean_branch_name=${branch//\*\ /} # replace colors clean_branch_name=`echo $clean_branch_name | tr -d '[:cntrl:]' | sed -E "s/\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"` + # replace symbolic-ref like `HEAD -> master` + clean_branch_name=`echo $clean_branch_name | sed -E "s/^.+ -> //g"` + description=`git config branch.$clean_branch_name.description` if [ "${branch::1}" == "*" ]; then printf "$branch $description\n" diff --git a/test/git-br.test.js b/test/git-br.test.js index fb4298f..953f2c5 100644 --- a/test/git-br.test.js +++ b/test/git-br.test.js @@ -5,35 +5,53 @@ const child_process = require('child_process'); const expect = require('expect.js'); describe('git-br', () => { + + let currentBranch; + beforeEach(done => { - console.log('switching to master branch'); - exeq(['git checkout master']).then(() => done()).catch(console.error); + exeq([`git checkout ${currentBranch}`]).then(() => done()).catch(console.error); }); before(done => { - exeq([ - 'git checkout -b test-with-description || true', - 'git checkout -b test-without-description || true', - 'git config branch.test-with-description.description "description text"', - 'git checkout -b test-delete-branch || true', - 'git checkout master', // Switch to master branch for test delete branch. - ]).then(() => done()).catch(console.error); + child_process.exec('git symbolic-ref --short HEAD', (err, stdout) => { + + currentBranch = stdout.trim(); + if (!currentBranch) { + currentBranch = 'test-ci'; + // travis-ci just checkout FETCH_HEAD without branch. + child_process.execSync(`git checkout -b ${currentBranch}`); + } + + exeq([ + 'git checkout -b test-with-description', + 'git checkout -b test-without-description', + 'git config branch.test-with-description.description "description text"', + 'git checkout -b test-delete-branch', + 'git symbolic-ref refs/heads/test-symbolic-ref-with-description refs/heads/test-with-description', + 'git symbolic-ref refs/heads/test-symbolic-ref-without-description refs/heads/test-without-description', + `git checkout ${currentBranch}`, // Switch to current branch for test delete branch. + ]).then(() => done()).catch(console.error); + }); }); after(done => { exeq([ - 'git checkout master', - 'git branch -D test-with-description || true', - 'git branch -D test-without-description || true', + `git checkout ${currentBranch}`, + 'git branch -D test-with-description', + 'git branch -D test-without-description', + 'git symbolic-ref -d refs/heads/test-symbolic-ref-with-description', + 'git symbolic-ref -d refs/heads/test-symbolic-ref-without-description', ]).then(() => done()).catch(console.error); }); it('list branches with description', done => { child_process.exec('./branches.sh --no-color', function(err, stdout) { expect(err).to.eql(null); - expect(stdout).to.contain('* master '); + expect(stdout).to.contain(`* ${currentBranch} `); expect(stdout).to.contain(' test-with-description description text\n'); expect(stdout).to.contain(' test-without-description \n'); + expect(stdout).to.contain(' test-symbolic-ref-with-description -> test-with-description description text\n'); + expect(stdout).to.contain(' test-symbolic-ref-without-description -> test-without-description \n'); done(); }); }); @@ -45,9 +63,11 @@ describe('git-br', () => { child_process.exec('./branches.sh --no-color', function(er, stdout) { expect(er).to.eql(null); - expect(stdout).to.contain('* master '); + expect(stdout).to.contain(`* ${currentBranch} `); expect(stdout).to.contain(' test-with-description description text\n'); expect(stdout).to.contain(' test-without-description \n'); + expect(stdout).to.contain(' test-symbolic-ref-with-description -> test-with-description description text\n'); + expect(stdout).to.contain(' test-symbolic-ref-without-description -> test-without-description \n'); expect(stdout).to.not.contain(' test-delete-branch \n'); done(); });