Skip to content

Commit

Permalink
Change use of "origin" to "remote"
Browse files Browse the repository at this point in the history
"origin" doesn't really make sense, as it is just the name of the default remote for most repos
"remote" is more correct in the context of the code
  • Loading branch information
cookpete committed Jan 15, 2018
1 parent a5c3f75 commit 7c9c175
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 116 deletions.
8 changes: 4 additions & 4 deletions scripts/generate-test-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const parseCommits = __get__('parseCommits')

const DATA_DIR = join(__dirname, '..', 'test', 'data')

const origin = {
const remote = {
hostname: 'github.com',
url: 'https://github.com/user/repo'
}

async function run () {
const gitLog = await readFile(join(DATA_DIR, 'git-log.txt'), 'utf-8')
const commits = parseCommits(gitLog, origin, { tagPrefix: 'v' })
const releases = parseReleases(commits, origin, null, { unreleased: false, commitLimit: 3 })
const commits = parseCommits(gitLog, remote, { tagPrefix: 'v' })
const releases = parseReleases(commits, remote, null, { unreleased: false, commitLimit: 3 })
await writeFile(join(DATA_DIR, 'commits.js'), 'export default ' + JSON.stringify(commits, null, 2))
await writeFile(join(DATA_DIR, 'commits-no-origin.js'), 'export default ' + JSON.stringify(commitsWithoutLinks(commits), null, 2))
await writeFile(join(DATA_DIR, 'commits-no-remote.js'), 'export default ' + JSON.stringify(commitsWithoutLinks(commits), null, 2))
await writeFile(join(DATA_DIR, 'releases.js'), 'export default ' + JSON.stringify(releases, null, 2))
await writeFile(join(DATA_DIR, 'template-compact.md'), await compileTemplate('compact', { releases }))
await writeFile(join(DATA_DIR, 'template-keepachangelog.md'), await compileTemplate('keepachangelog', { releases }))
Expand Down
54 changes: 27 additions & 27 deletions src/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ const MERGE_PATTERNS = [
/Merge branch .+ into .+\n\n(.+)[\S\s]+See merge request !(\d+)/ // GitLab merge
]

export async function fetchCommits (origin, options) {
export async function fetchCommits (remote, options) {
const log = await cmd(`git log --shortstat --pretty=format:${LOG_FORMAT}`)
return parseCommits(log, origin, options)
return parseCommits(log, remote, options)
}

function parseCommits (string, origin, options = {}) {
function parseCommits (string, remote, options = {}) {
const commits = string
.split(COMMIT_SEPARATOR)
.slice(1)
.map(commit => parseCommit(commit, origin, options))
.map(commit => parseCommit(commit, remote, options))
.filter(commit => {
if (options.ignoreCommitPattern) {
return new RegExp(options.ignoreCommitPattern).test(commit.subject) === false
Expand All @@ -44,7 +44,7 @@ function parseCommits (string, origin, options = {}) {
return commits
}

function parseCommit (commit, origin, options = {}) {
function parseCommit (commit, remote, options = {}) {
const [, hash, refs, date, author, email, tail] = commit.match(MATCH_COMMIT)
const [message, stats] = tail.split(MESSAGE_SEPARATOR)
return {
Expand All @@ -56,9 +56,9 @@ function parseCommit (commit, origin, options = {}) {
tag: getTag(refs, options),
subject: getSubject(message),
message: message.trim(),
fixes: getFixes(message, origin, options),
merge: getMerge(message, origin),
href: getCommitLink(hash, origin),
fixes: getFixes(message, remote, options),
merge: getMerge(message, remote),
href: getCommitLink(hash, remote),
...getStats(stats.trim())
}
}
Expand Down Expand Up @@ -87,14 +87,14 @@ function getStats (stats) {
}
}

function getFixes (message, origin, options = {}) {
function getFixes (message, remote, options = {}) {
const pattern = getFixPattern(options)
let fixes = []
let match = pattern.exec(message)
if (!match) return null
while (match) {
const id = getFixID(match)
const href = getIssueLink(match, id, origin, options.issueUrl)
const href = getIssueLink(match, id, remote, options.issueUrl)
fixes.push({ id, href })
match = pattern.exec(message)
}
Expand All @@ -117,7 +117,7 @@ function getFixPattern (options) {
return DEFAULT_FIX_PATTERN
}

function getMerge (message, origin, mergeUrl) {
function getMerge (message, remote, mergeUrl) {
for (let pattern of MERGE_PATTERNS) {
const match = message.match(pattern)
if (match) {
Expand All @@ -126,25 +126,25 @@ function getMerge (message, origin, mergeUrl) {
return {
id,
message,
href: getMergeLink(id, origin, mergeUrl)
href: getMergeLink(id, remote, mergeUrl)
}
}
}
return null
}

function getCommitLink (hash, origin) {
if (!origin) {
function getCommitLink (hash, remote) {
if (!remote) {
return null
}
if (origin.hostname === 'bitbucket.org') {
return `${origin.url}/commits/${hash}`
if (remote.hostname === 'bitbucket.org') {
return `${remote.url}/commits/${hash}`
}
return `${origin.url}/commit/${hash}`
return `${remote.url}/commit/${hash}`
}

function getIssueLink (match, id, origin, issueUrl) {
if (!origin) {
function getIssueLink (match, id, remote, issueUrl) {
if (!remote) {
return null
}
if (isLink(match[2])) {
Expand All @@ -153,18 +153,18 @@ function getIssueLink (match, id, origin, issueUrl) {
if (issueUrl) {
return issueUrl.replace('{id}', id)
}
return `${origin.url}/issues/${id}`
return `${remote.url}/issues/${id}`
}

function getMergeLink (id, origin) {
if (!origin) {
function getMergeLink (id, remote) {
if (!remote) {
return null
}
if (origin.hostname === 'bitbucket.org') {
return `${origin.url}/pull-requests/${id}`
if (remote.hostname === 'bitbucket.org') {
return `${remote.url}/pull-requests/${id}`
}
if (origin.hostname === 'gitlab.com') {
return `${origin.url}/merge_requests/${id}`
if (remote.hostname === 'gitlab.com') {
return `${remote.url}/merge_requests/${id}`
}
return `${origin.url}/pull/${id}`
return `${remote.url}/pull/${id}`
}
19 changes: 0 additions & 19 deletions src/origin.js

This file was deleted.

14 changes: 7 additions & 7 deletions src/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { niceDate } from './utils'

const MERGE_COMMIT_PATTERN = /^Merge (remote-tracking )?branch '.+'/

export function parseReleases (commits, origin, latestVersion, options) {
export function parseReleases (commits, remote, latestVersion, options) {
let release = newRelease(latestVersion)
const releases = []
for (let commit of commits) {
if (commit.tag && semver.valid(commit.tag)) {
if (release.tag || options.unreleased) {
releases.push({
...release,
href: getCompareLink(commit.tag, release.tag || 'HEAD', origin),
href: getCompareLink(commit.tag, release.tag || 'HEAD', remote),
commits: release.commits.sort(sortCommits),
major: commit.tag && release.tag && semver.diff(commit.tag, release.tag) === 'major'
})
Expand Down Expand Up @@ -67,14 +67,14 @@ function filterCommit (commit, release, limit) {
return release.commits.length < limit
}

function getCompareLink (from, to, origin) {
if (!origin) {
function getCompareLink (from, to, remote) {
if (!remote) {
return null
}
if (origin.hostname === 'bitbucket.org') {
return `${origin.url}/compare/${to}%0D${from}`
if (remote.hostname === 'bitbucket.org') {
return `${remote.url}/compare/${to}%0D${from}`
}
return `${origin.url}/compare/${from}...${to}`
return `${remote.url}/compare/${from}...${to}`
}

function sortCommits (a, b) {
Expand Down
19 changes: 19 additions & 0 deletions src/remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import parseRepoURL from 'parse-github-url'

import { cmd } from './utils'

export async function fetchRemote (name) {
const remoteURL = await cmd(`git config --get remote.${name}.url`)
if (!remoteURL) {
console.warn(`Warning: Git remote ${name} was not found`)
console.warn(`Warning: Changelog will not contain links to commits, issues, or PRs`)
return null
}
const remote = parseRepoURL(remoteURL)
const protocol = remote.protocol === 'http:' ? 'http:' : 'https:'
const hostname = remote.hostname || remote.host
return {
hostname,
url: `${protocol}//${hostname}/${remote.repo}`
}
}
8 changes: 4 additions & 4 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readJson, writeFile, pathExists } from 'fs-extra'
import semver from 'semver'

import { version } from '../package.json'
import { fetchOrigin } from './origin'
import { fetchRemote } from './remote'
import { fetchCommits } from './commits'
import { parseReleases } from './releases'
import { compileTemplate } from './template'
Expand Down Expand Up @@ -68,10 +68,10 @@ function getLatestVersion (options, pkg) {
export default async function run (argv) {
const pkg = await pathExists('package.json') && await readJson('package.json')
const options = getOptions(argv, pkg)
const origin = await fetchOrigin(options.remote)
const commits = await fetchCommits(origin, options)
const remote = await fetchRemote(options.remote)
const commits = await fetchCommits(remote, options)
const latestVersion = getLatestVersion(options, pkg)
const releases = parseReleases(commits, origin, latestVersion, options)
const releases = parseReleases(commits, remote, latestVersion, options)
const log = await compileTemplate(options.template, { releases })
await writeFile(options.output, log)
return `${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}`
Expand Down
Loading

0 comments on commit 7c9c175

Please sign in to comment.