Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function getProjectGitStatus(
projectRoot: string
): Promise {
// branching
const currentBranch = await git.currentBranch({ fs, dir: projectRoot })
const branches = await git.listBranches({ fs, dir: projectRoot })
const history = await getGitHistory(projectRoot, { ref: currentBranch })
// staging
const trackingStatus = await getGitTrackingStatus(projectRoot)
const { tracked, untracked } = trackingStatus
const {
staged,
unstaged,
unmodified,
rawStatusList
} = await getFileStatusInProject(projectRoot, tracked)
return {
rawStatusList,
static async getBranch(dir) {
// current branch name
const currentBranch = await git.currentBranch({ dir, fullname: false });
// current commit sha
const rev = await git.resolveRef({ dir, ref: 'HEAD' });
// reverse-lookup tag from commit sha
const allTags = await git.listTags({ dir });
const tagCommitShas = await Promise.all(allTags.map(async (tag) => {
const oid = await git.resolveRef({ dir, ref: tag });
const obj = await git.readObject({ dir, oid });
// annotated or lightweight tag?
return obj.type === 'tag' ? {
tag,
sha: await git.resolveRef({ dir, ref: obj.object.object }),
} : { tag, sha: oid };
}));
const tag = tagCommitShas.find((entry) => entry.sha === rev);
return typeof tag === 'object' ? tag.tag : currentBranch;
onOpenProject(){
dir = DATA.projectPath;
if( !fs.existsSync(dir + path.sep + ".git") ){
noGit = true;
return;
git.init({dir}).then(result=>{
log("New git initialized");
wasInit = true;
}).catch(ex=>{
log("Error initializing git: " + ex);
});
}else{
wasInit = true;
git.currentBranch({dir, fullname:false})
.then(name=>log(`Currently on branch ${name}`));
}
}
});
export async function getBranchStatus(
projectRoot: string
): Promise<{
currentBranch: string
branches: string[]
history: CommitDescription[]
remotes: string[]
remoteBranches: string[]
}> {
const currentBranch = await git.currentBranch({ dir: projectRoot })
const branches = await git.listBranches({ dir: projectRoot })
const remotes: string[] = (await git.listRemotes({ dir: projectRoot })).map(
(a: { remote: string; url: string }) => a.remote
)
const remoteBranches = flatten(
await Promise.all(
remotes.map(remote => listRemoteBranches(projectRoot, remote))
)
)
const history = await getHistory(projectRoot, { ref: currentBranch })
return {
currentBranch,
branches,
remotes,
async function getFullBranch (branchName) {
const repo = await getRepo();
if (branchName === '') {
return git.currentBranch({ ...repo, fullname: true });
}
return git.expandRef({ ...repo, ref: branchName });
};
return { ...acc, unmodified: [...acc.unmodified, fileStatus.relpath] }
}
default: {
return acc
}
}
},
{
added: [],
modified: [],
staged: [],
unmodified: []
}
)
const currentBranch = await git.currentBranch({ fs, dir: projectRoot })
const branches = await git.listBranches({ fs, dir: projectRoot })
const history = await getLogInRepository(projectRoot, { ref: currentBranch })
return {
branches,
currentBranch,
stagingStatus,
trackingStatus,
history
}
}