Skip to content

Commit

Permalink
Merge pull request #36 from INSO-TUWien/feature/34
Browse files Browse the repository at this point in the history
Feature/34
  • Loading branch information
nuberion committed Sep 17, 2021
2 parents 64225f3 + 719d33c commit 1c03d82
Show file tree
Hide file tree
Showing 42 changed files with 3,747 additions and 3,580 deletions.
8 changes: 8 additions & 0 deletions foxx/types/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ module.exports = new gql.GraphQLObjectType({
type: gql.GraphQLString,
description: "The commit author's signature"
},
branch: {
type: gql.GraphQLString,
description: 'The commit branch'
},
parents: {
type: gql.GraphQLString,
description: 'Parents of the commit'
},
date: {
type: Timestamp,
description: 'The date of the commit'
Expand Down
1 change: 1 addition & 0 deletions foxx/types/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = new gql.GraphQLObjectType({
IN
OUTBOUND ${file} ${commitsToFiles}
${limit}
SORT commit.date ASC
RETURN commit`
})
};
Expand Down
17 changes: 17 additions & 0 deletions lib/endpoints/get-fileSourceCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const path = require('path');
const fs = require('fs');

module.exports = function(req, res) {
return fs.readFile(path.join(process.cwd(), req.body.path), 'utf8', (err, data) => {
if (err) {
console.error(err);
res.json({ sourceCode: 'Could not read Source Code!' });
return;
}
console.log(data);
res.json({ sourceCode: data });
});
//return res.json({ sourceCode: fs.readFile(path.join(process.cwd(), req.body.path)) });
};
25 changes: 11 additions & 14 deletions lib/indexers/vcs/GitIndexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,18 @@ class GitIndexer {
this.reporter.setFilesLanguageCount(this.counter.langFile.total);
}

Promise.resolve(this.repo.getCurrentBranch()).then(currentBranch => {
const currentBranchName = currentBranch.shorthand();
Promise.resolve(this.repo.getAllBranches()).then(branches => {
for (const i in branches) {
if (branches[i].startsWith('refs/remotes/origin/')) {
const name = branches[i].substring(20);
if (name !== 'HEAD') {
const branch = { branchName: name, id: i, currentActive: name === currentBranchName };
Branch.persist(branch);
}
}
const currentBranch = await this.repo.getCurrentBranch();
const currentBranchName = currentBranch.shorthand();
const branches = await this.repo.getAllBranches();
for (const i in branches) {
if (branches[i].startsWith('refs/remotes/origin/')) {
const name = branches[i].substring(20);
if (name !== 'HEAD') {
const branch = { branchName: name, id: i, currentActive: name === currentBranchName };
Branch.persist(branch);
}
});
});

}
}
log('Processing', this.counter.commits.total, 'commits');
await this.repo.walk(processCommit.bind(this)).catch({ stop: true }, () => null);

Expand Down
87 changes: 59 additions & 28 deletions lib/models/Commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const aql = require('arangojs').aql;
const Model = require('./Model.js');
const File = require('./File.js');
const IllegalArgumentError = require('../errors/IllegalArgumentError');
const { exec } = require('child_process');

const Commit = Model.define('Commit', {
attributes: ['sha', 'message', 'signature', 'date', 'stats', 'webUrl'],
attributes: ['sha', 'message', 'signature', 'date', 'stats', 'branch', 'parents', 'webUrl'],
keyAttribute: 'sha'
});

Expand All @@ -21,7 +22,7 @@ const Commit = Model.define('Commit', {
* @param urlProvider contains the given remote vcs webapp provider to link them
* @returns Commit returns an already existing or newly created commit
*/
Commit.persist = function(repo, nCommit, urlProvider) {
Commit.persist = async function(repo, nCommit, urlProvider) {
if (!repo || !nCommit) {
throw IllegalArgumentError('repository and git-commit has to be set!');
}
Expand All @@ -36,36 +37,66 @@ Commit.persist = function(repo, nCommit, urlProvider) {
}

log('Processing', sha);

const parentShas = [];
for (let i = 0; 'parentcount' in nCommit && i < nCommit.parentcount(); i++) {
parentShas.push(nCommit.parentId(i).toString());
}

// create new commit and link it to its parent commits
return Commit.create(
{
sha,
signature: nCommit.committer().toString(),
date: nCommit.date(),
message: nCommit.message(),
webUrl: urlProvider ? urlProvider.getCommitUrl(sha) : '',
stats: {
additions: 0,
deletions: 0
}
},
{ isNew: true }
).tap(function(commit) {
return Promise.all(
Promise.map(parentShas, parentSha => {
return Commit.findById(parentSha).then(parentCommit => commit.connect(parentCommit));
})
);
return getBranchForCommit(sha).then(branch => {
const parentShas = [];
for (let i = 0; 'parentcount' in nCommit && i < nCommit.parentcount(); i++) {
parentShas.push(nCommit.parentId(i).toString());
}
const parents = [];
for (let i = 0; i < nCommit.parents().length; i++) {
parents.push(nCommit.parentId(i).toString());
}
// create new commit and link it to its parent commits
return Commit.create(
{
sha,
signature: nCommit.committer().toString(),
date: nCommit.date(),
message: nCommit.message(),
webUrl: urlProvider ? urlProvider.getCommitUrl(sha) : '',
branch: branch,
parents: parents.toString(),
stats: {
additions: 0,
deletions: 0
}
},
{ isNew: true }
).tap(function(commit) {
return Promise.all(
Promise.map(parentShas, parentSha => {
return Commit.findById(parentSha).then(parentCommit => commit.connect(parentCommit));
})
);
});
});
});
};

/**
* returns the branch name of a given sha repository name and owner name
* check with git shell command because this function is not jet implemented in nodegit
*
* @param sha sha of commit to check
*
* @retruns branch
*/
async function getBranchForCommit(sha) {
return (await new Promise(resolve => {
exec('git name-rev --name-only ' + sha, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
resolve('');
}
if (stderr) {
console.log(`stderr: ${stderr}`);
resolve('');
}
resolve(stdout);
});
})).split('~')[0];
}

/**
* process and store a commit and its associated data objects
*
Expand Down
Loading

0 comments on commit 1c03d82

Please sign in to comment.