Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Commit

Permalink
git helper functions (WIP) #47
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-guggisberg committed Dec 7, 2018
1 parent 0103dd7 commit 3fbaf28
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

'use strict';

const git = require('isomorphic-git');

git.plugins.set('fs', require('fs'));

/**
* Various helper functions reading git content
*/

/**
* Returns the contents of the file at revision `ref` and `pathName`
*
* @param {string} dir git repo path
* @param {string} ref reference (branch, tag or commit sha)
* @param {string} filePath relative path to file
* @param {boolean} dirty whether modified file should be returned
* @returns {Promise<Buffer>} content of specified file
*/
async function getRawContent(dir, ref, pathName, dirty) {
// TODO: implement serveUncommitted logic
let commitSha;
try {
commitSha = await git.resolveRef({ dir, ref });
} catch (err) {
if (err.code === 'ResolveRefError') {
// fallback: is ref a shortened oid prefix?
const oid = await git.expandOid({ dir, oid: ref });
commitSha = await git.resolveRef({ dir, ref: oid });
} else {
throw err;
}
}
const content = await git.readObject({
dir,
oid: commitSha,
filepath: pathName,
format: 'content',
});
return content;
}

module.exports = {
getRawContent,
};

0 comments on commit 3fbaf28

Please sign in to comment.