Skip to content

Commit

Permalink
feat: add 'phase' to progress events (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose committed Dec 1, 2018
1 parent 5d074e0 commit 951035c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 23 deletions.
17 changes: 17 additions & 0 deletions src/commands/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export async function checkout ({
dir,
gitdir = path.join(dir, '.git'),
fs: _fs = cores.get(core).get('fs'),
emitter = cores.get(core).get('emitter'),
emitterPrefix = '',
remote = 'origin',
ref
}) {
Expand Down Expand Up @@ -65,6 +67,7 @@ export async function checkout ({
}
let fullRef = await GitRefManager.expand({ fs, gitdir, ref })

let count = 0
// Acquire a lock on the index
await GitIndexManager.acquire(
{ fs, filepath: `${gitdir}/index` },
Expand All @@ -76,6 +79,13 @@ export async function checkout ({
for (let entry of index) {
try {
await fs.rm(path.join(dir, entry.path))
if (emitter) {
emitter.emit(`${emitterPrefix}progress`, {
phase: 'Updating workdir',
loaded: ++count,
lengthComputable: false
})
}
} catch (err) {}
}
index.clear()
Expand Down Expand Up @@ -136,6 +146,13 @@ export async function checkout ({
stats,
oid: head.oid
})
if (emitter) {
emitter.emit(`${emitterPrefix}progress`, {
phase: 'Updating workdir',
loaded: ++count,
lengthComputable: false
})
}
break
}
default: {
Expand Down
25 changes: 20 additions & 5 deletions src/commands/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import path from 'path'
import { FileSystem } from '../models/FileSystem.js'
import { cores } from '../utils/plugins.js'

import { checkout } from './checkout'
import { config } from './config'
import { fetch } from './fetch'
import { init } from './init'
import { checkout } from './checkout.js'
import { config } from './config.js'
import { fetch } from './fetch.js'
import { indexPack } from './indexPack.js'
import { init } from './init.js'

/**
* Clone a repository
Expand Down Expand Up @@ -71,7 +72,7 @@ export async function clone ({
})
}
// Fetch commits
let { defaultBranch } = await fetch({
let { defaultBranch, packfile } = await fetch({
gitdir,
fs,
emitter,
Expand All @@ -92,12 +93,26 @@ export async function clone ({
})
ref = ref || defaultBranch
ref = ref.replace('refs/heads/', '')
// Note: we're indexing the pack eagerly instead of lazily so
// we get the nice progress events
if (packfile) {
await indexPack({
dir: gitdir,
gitdir,
fs,
emitter,
emitterPrefix,
filepath: packfile
})
}
// Checkout that branch
if (!noCheckout) {
await checkout({
dir,
gitdir,
fs,
emitter,
emitterPrefix,
ref,
remote
})
Expand Down
29 changes: 14 additions & 15 deletions src/commands/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,18 @@ export async function fetch ({
if (emitter) {
emitter.emit(`${emitterPrefix}message`, line.trim())
}
let matches = line.match(/\((\d+?)\/(\d+?)\)/)
let matches = line.match(/([^:]*).*\((\d+?)\/(\d+?)\)/)
if (matches && emitter) {
emitter.emit(`${emitterPrefix}progress`, {
loaded: parseInt(matches[1], 10),
total: parseInt(matches[2], 10),
phase: matches[1].trim(),
loaded: parseInt(matches[2], 10),
total: parseInt(matches[3], 10),
lengthComputable: true
})
}
})
let packfile = await pify(concat)(response.packfile)
let packfileSha = packfile.slice(-20).toString('hex')
// This is a quick fix for the empty .git/objects/pack/pack-.pack file error,
// which due to the way `git-list-pack` works causes the program to hang when it tries to read it.
// TODO: Longer term, we should actually:
// a) NOT concatenate the entire packfile into memory (line 78),
// b) compute the SHA of the stream except for the last 20 bytes, using the same library used in push.js, and
// c) compare the computed SHA with the last 20 bytes of the stream before saving to disk, and throwing a "packfile got corrupted during download" error if the SHA doesn't match.
if (packfileSha !== '') {
await fs.write(
path.join(gitdir, `objects/pack/pack-${packfileSha}.pack`),
packfile
)
}
// TODO: Return more metadata?
let res = {
defaultBranch: response.HEAD,
Expand All @@ -115,6 +104,16 @@ export async function fetch ({
if (response.headers) {
res.headers = response.headers
}
// This is a quick fix for the empty .git/objects/pack/pack-.pack file error,
// which due to the way `git-list-pack` works causes the program to hang when it tries to read it.
// TODO: Longer term, we should actually:
// a) NOT concatenate the entire packfile into memory (line 78),
// b) compute the SHA of the stream except for the last 20 bytes, using the same library used in push.js, and
// c) compare the computed SHA with the last 20 bytes of the stream before saving to disk, and throwing a "packfile got corrupted during download" error if the SHA doesn't match.
if (packfileSha !== '') {
res.packfile = `objects/pack/pack-${packfileSha}.pack`
await fs.write(path.join(gitdir, res.packfile), packfile)
}
return res
} catch (err) {
err.caller = 'git.fetch'
Expand Down
7 changes: 5 additions & 2 deletions src/commands/indexPack.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ export async function indexPack ({
dir,
gitdir = path.join(dir, '.git'),
fs: _fs = cores.get(core).get('fs'),
emitter = cores.get(core).get('emitter'),
emitterPrefix = '',
filepath
}) {
try {
const fs = new FileSystem(_fs)
const pack = await fs.read(path.join(dir, filepath))
const idx = await GitPackIndex.fromPack({ pack })
filepath = path.join(dir, filepath)
const pack = await fs.read(filepath)
const idx = await GitPackIndex.fromPack({ pack, emitter, emitterPrefix })
await fs.write(filepath.replace(/\.pack$/, '.idx'), idx.toBuffer())
} catch (err) {
err.caller = 'git.indexPack'
Expand Down
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ export function checkout(args: {
fs?: any;
dir: string;
gitdir?: string;
emitter?: EventEmitter;
emitterPrefix?: string;
remote?: string;
ref?: string;
}): Promise<void>;
Expand Down Expand Up @@ -345,6 +347,8 @@ export function indexPack(args: {
fs?: any;
dir: string;
gitdir?: string;
emitter?: EventEmitter;
emitterPrefix?: string;
filepath: string;
}): Promise<void>;

Expand Down
18 changes: 17 additions & 1 deletion src/models/GitPackIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class GitPackIndex {
getExternalRefDelta
})
}
static async fromPack ({ pack, getExternalRefDelta }) {
static async fromPack ({ pack, getExternalRefDelta, emitter, emitterPrefix }) {
const listpackTypes = {
1: 'commit',
2: 'tree',
Expand Down Expand Up @@ -158,6 +158,14 @@ export class GitPackIndex {
((totalObjectCount - num) * 100) / totalObjectCount
)
if (percent !== lastPercent) {
if (emitter) {
emitter.emit(`${emitterPrefix}progress`, {
phase: 'Receiving objects',
loaded: totalObjectCount - num,
total: totalObjectCount,
lengthComputable: true
})
}
log(
`${percent}%\t${Math.floor(
marky.stop('percent').duration
Expand Down Expand Up @@ -249,6 +257,14 @@ export class GitPackIndex {
marky.stop('percent').duration
)}\t${callsToReadSlice}\t${callsToGetExternal}`
)
if (emitter) {
emitter.emit(`${emitterPrefix}progress`, {
phase: 'Resolving deltas',
loaded: count,
total: totalObjectCount,
lengthComputable: true
})
}
marky.mark('percent')
callsToReadSlice = 0
callsToGetExternal = 0
Expand Down

0 comments on commit 951035c

Please sign in to comment.