Skip to content

Commit

Permalink
wip: commit additional parents from sources
Browse files Browse the repository at this point in the history
Issues:
- Subsources of projected sources always have tree hash as head, need to figure out a way to track commit hash
  • Loading branch information
themightychris committed Dec 24, 2019
1 parent 7d8e4c6 commit fcda02f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
17 changes: 11 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,30 @@

"name": "project",
"preLaunchTask": "purge hologit studios",
"cwd": "${workspaceFolder:hologit-debug-context}",
"cwd": "${workspaceFolder:brigade-project-index}",
"env": {
"DEBUG": "1"
"DEBUG": "1",
// "HOLO_CACHE_FROM": "local"
},
"outputCapture": "std",
"program": "${workspaceFolder:hologit}/bin/cli.js",
"args": [
"project",
"--working",
"--watch",
// "--watch",
"--debug",
// "--no-lens",
// "--fetch",
"--fetch=index-v1",
// "--ref=develop",
"--commit-to=refs/holo/branch/site",
"--commit-to=gh-pages",
// "--no-cache-from",
"--cache-from=origin",
"--cache-to=origin",
"--parent-source=index-v1"
// "--commit-message=foo",

// holobranch
"emergence-site"
"gh-pages"
]
},
{
Expand Down
16 changes: 15 additions & 1 deletion commands/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ exports.builder = {
type: 'boolean',
default: false
},
'parent-source': {
describe: 'One or more source names to use additional parent(s) with commit-to',
type: 'string'
},
'cache-to': {
describe: 'Set a remote to push caches to',
type: 'string'
Expand All @@ -53,11 +57,12 @@ exports.handler = async function project ({
watch = false,
commitTo = null,
commitMessage = null,
parentSource = null,
cacheFrom = null,
cacheTo = null
}) {
const logger = require('../lib/logger.js');
const { Repo, Projection } = require('../lib');
const { Repo, Projection, ParentsBag } = require('../lib');


// check inputs
Expand Down Expand Up @@ -111,6 +116,14 @@ exports.handler = async function project ({
}


// parse parent source
let parentsBag = null;
// TODO: append from env and split?
if (parentSource) {
parentsBag = new ParentsBag(parentSource);
}


// load holorepo
const repo = await Repo.getFromEnvironment({ ref, working });
const parentCommit = await repo.resolveRef();
Expand All @@ -137,6 +150,7 @@ exports.handler = async function project ({
commitTo,
commitMessage,
parentCommit,
parentsBag,
fetch,
cacheFrom,
cacheTo
Expand Down
31 changes: 25 additions & 6 deletions lib/Branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ class Branch extends Configurable {
outputTree = this.getRepo().createTree(),
fetch = false,
cacheFrom = null,
cacheTo = null
cacheTo = null,
parentsBag = null
}) {
const repo = this.getRepo();
const mappings = await this.getMappings();
Expand All @@ -216,25 +217,26 @@ class Branch extends Configurable {

// load source
const source = await this.workspace.getSource(holosource);
let sourceHead;

if (
fetch === true
|| (Array.isArray(fetch) && fetch.indexOf(source.name) >= 0)
) {
const originalHash = await source.getHead();
await source.fetch();
const hash = await source.getHead();
sourceHead = await source.getHead();
const { url, ref } = await source.getCachedConfig();

if (hash == originalHash) {
logger.info(`${source.name}@${hash.substr(0, 8)} up-to-date`);
if (sourceHead == originalHash) {
logger.info(`${source.name}@${sourceHead.substr(0, 8)} up-to-date`);
} else {
logger.info(`${source.name}@${originalHash.substr(0, 8)}..${hash.substr(0, 8)} fetched ${url}#${ref}`);
logger.info(`${source.name}@${originalHash.substr(0, 8)}..${sourceHead.substr(0, 8)} fetched ${url}#${ref}`);
}
}

// load tree
const sourceTreeHash = await source.getOutputTree({ fetch, cacheFrom, cacheTo });
const sourceTreeHash = await source.getOutputTree({ fetch, cacheFrom, cacheTo, parentsBag });
const sourceTree = await repo.createTreeFromRef(`${sourceTreeHash}:${root == '.' ? '' : root}`);

// merge source into target
Expand All @@ -243,6 +245,23 @@ class Branch extends Configurable {
await targetTree.merge(sourceTree, {
files: files
});

// track in parents bag
// TODO: problem -- sourceHead is always a tree hash subsources of projected source
if (parentsBag && parentsBag.sources.has(source.name)) {
const git = await repo.getGit();

if (!sourceHead) {
sourceHead = await source.getHead();
}

const type = await git.catFile(sourceHead, { t: true });
if (type == 'tree') {
sourceHead = await git.commitTree(sourceHead, { m: `working tree for holosource: ${source.name}` })
}

parentsBag.commits.add(sourceHead);
}
}


Expand Down
18 changes: 18 additions & 0 deletions lib/ParentsBag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ParentsBag
{
constructor (value) {
if (Array.isArray(value)) {
this.sources = new Set(value);
} else if (typeof value == 'string') {
this.sources = new Set([value]);
} else {
this.sources = new Set;
}

this.commits = new Set;

Object.freeze(this);
}
}

module.exports = ParentsBag;
15 changes: 10 additions & 5 deletions lib/Projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Projection {
commitTo = null,
commitMessage = null,
parentCommit = null,
parentsBag = null,
fetch = false,
cacheFrom = null,
cacheTo = null
Expand All @@ -24,7 +25,7 @@ class Projection {


// apply composition
await projection.composite({ fetch, cacheFrom, cacheTo });
await projection.composite({ fetch, cacheFrom, cacheTo, parentsBag });


// apply lensing
Expand Down Expand Up @@ -78,7 +79,7 @@ class Projection {
commitTo = `refs/heads/${commitTo}`;
}

outputHash = await projection.commit(commitTo, { parentCommit, commitMessage });
outputHash = await projection.commit(commitTo, { parentCommit, parentsBag, commitMessage });
}


Expand All @@ -101,7 +102,7 @@ class Projection {
Object.freeze(this);
}

async composite ({ fetch = false, cacheFrom = null, cacheTo = null }) {
async composite ({ fetch = false, cacheFrom = null, cacheTo = null, parentsBag = null }) {
const branchStack = [];

// merge extended holobranch onto output first
Expand All @@ -124,7 +125,7 @@ class Projection {


// merge projected holobranch onto output
await this.branch.composite({ outputTree: this.output.root, fetch, cacheFrom, cacheTo });
await this.branch.composite({ outputTree: this.output.root, fetch, cacheFrom, cacheTo, parentsBag });


// strip .holo/{branches,sources} from output
Expand Down Expand Up @@ -196,7 +197,7 @@ class Projection {
}
}

async commit (ref, { parentCommit=null, commitMessage = null } = {}) {
async commit (ref, { parentCommit=null, parentsBag=null, commitMessage=null } = {}) {
const repo = this.branch.getRepo();
const git = await repo.getGit();

Expand All @@ -212,6 +213,10 @@ class Projection {
parents.push(parentCommit);
}

if (parentsBag && parentsBag.commits.size) {
parents.push(...parentsBag.commits);
}

const commitHash = await git.commitTree(await this.output.root.write(), {
p: parents,
m: commitMessage || `☀ projected ${this.branch.name} from ${repo.workTree || await git.describe({ always: true, tags: true }, repo.ref)}`
Expand Down
5 changes: 3 additions & 2 deletions lib/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Source extends Configurable {
return treeHash;
}

async getOutputTree ({ working = null, fetch = false, cacheFrom = null, cacheTo = null } = {}) {
async getOutputTree ({ working = null, fetch = false, cacheFrom = null, cacheTo = null, parentsBag = null } = {}) {
const repo = this.getRepo();
const git = await repo.getGit();
const { project } = await this.getCachedConfig();
Expand Down Expand Up @@ -199,7 +199,8 @@ class Source extends Configurable {
lens,
fetch,
cacheFrom,
cacheTo
cacheTo,
parentsBag
});

logger.info(`using projection result for holobranch ${project.holobranch} as source ${this.name}: ${head}`);
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ exports.BlobObject = require('./BlobObject.js');
exports.TreeObject = require('./TreeObject.js');
exports.SpecObject = require('./SpecObject.js');

exports.ParentsBag = require('./ParentsBag.js');

exports.getGit = () => {
require('./logger.js').warn('hololib.getGit() is deprecated, use Git.get() instead');
return exports.Git.get();
Expand Down

0 comments on commit fcda02f

Please sign in to comment.