This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0103dd7
commit 3fbaf28
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |