Skip to content

Commit

Permalink
WIP: start building git mob api
Browse files Browse the repository at this point in the history
list all authors in a Map, make it easy to access and iterate through authors.
  • Loading branch information
rkotze committed Aug 7, 2021
1 parent 9527fe4 commit 2522d38
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/commands/solo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { mob } = require("../git/commands");

function soloCommand() {
return vscode.commands.registerCommand("gitmob.solo", function () {
mob.solo();
mob.removeGitMobSection();
vscode.commands.executeCommand("gitmob.reload");
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/ext-config/solo-after-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let watch = null;
function soloSwitch(coAuthorProvider, afterCommitOn) {
if (afterCommitOn) {
watch = watchForCommit(function () {
mob.solo();
mob.removeGitMobSection();
coAuthorProvider.reloadData();
});
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/git/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { promisify } = require("util");
const { logIssue } = require("../errors/log-issue");
const { compare } = require("../semver/compare");
const { GitExt } = require("../vscode-git-extension/git-ext");
// const { gitMessage, gitMessagePath } = require("./git-mob-api/git-message");
// const { commitTemplatePath } = require("./git-mob-api/git-message");
const { silentRun } = require("./silent-run");

Expand Down Expand Up @@ -122,12 +123,12 @@ function addRepoAuthor({ commandKey, name, email }) {

function setCurrent(coAuthorList) {
// setCommitTemplate();
solo();
removeGitMobSection();
for (const author of coAuthorList) {
addCoAuthor(author.toString());
}

gitMessage(gitMessagePath()).writeCoAuthors(coAuthorList);
// gitMessage(gitMessagePath()).writeCoAuthors(coAuthorList);

return current();
}
Expand All @@ -136,7 +137,7 @@ function changeAuthor(authorKey) {
return format(handleResponse(`npx git mob -o ${authorKey}`));
}

function solo() {
function removeGitMobSection() {
return silentRun(`git config --remove-section git-mob`);
}

Expand Down Expand Up @@ -171,12 +172,13 @@ module.exports = {
config: {
get,
has,
set,
},
mob: {
current,
setCurrent,
listAll,
solo,
removeGitMobSection,
gitMobLatest,
installGitMob,
changeAuthor,
Expand Down
19 changes: 19 additions & 0 deletions src/git/git-mob-api/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Author {
constructor(key, name, email) {
this.key = key;
this.name = name;
this.email = email;
}

format() {
return `Co-authored-by: ${this.toString()}`;
}

toString() {
return `${this.name} <${this.email}>`;
}
}

module.exports = {
Author,
};
10 changes: 6 additions & 4 deletions src/git/git-mob-api/git-authors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs");
const os = require("os");
const path = require("path");
const { promisify } = require("util");
const { Author } = require("../author");

const gitCoauthorsPath =
process.env.GITMOB_COAUTHORS_PATH ||
Expand Down Expand Up @@ -105,10 +106,11 @@ function gitAuthors(readFilePromise, writeFilePromise, overwriteFilePromise) {

toList(authors) {
const entries = Object.entries(authors.coauthors);
return entries.map((authorParts) => {
const [initials, { name, email }] = authorParts;
return [initials, name, email].join(" ");
});
const authorMap = new Map();
for (let [key, { name, email }] of entries) {
authorMap.set(key, new Author(key, name, email));
}
return authorMap;
},
};
}
Expand Down
9 changes: 5 additions & 4 deletions src/git/git-mob-api/git-authors/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { gitAuthors } = require(".");
const { Author } = require("../author");

const validJsonString = `
{
Expand Down Expand Up @@ -74,10 +75,10 @@ test("create an organised string list of .git-coauthors", async () => {

const json = await authors.read();
const authorList = authors.toList(json);
const expectAuthorList = [
"jd Jane Doe jane@findmypast.com",
"fb Frances Bar frances-bar@findmypast.com",
];
const expectAuthorList = new Map([
["jd", new Author("jd", "Jane Doe", "jane@findmypast.com")],
["fb", new Author("fb", "Frances Bar", "frances-bar@findmypast.com")],
]);
expect(expectAuthorList).toEqual(authorList);
});

Expand Down
16 changes: 16 additions & 0 deletions src/git/git-mob-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { mob } = require("../commands");
const { gitAuthors } = require("./git-authors");

async function getAllAuthors() {
const gitMobAuthors = gitAuthors();
return gitMobAuthors.toList(await gitMobAuthors.read());
}

function addCoAuthors(keys) {
mob.removeGitMobSection();
}

module.exports = {
addCoAuthors,
getAllAuthors,
};
2 changes: 1 addition & 1 deletion src/mob-authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MobAuthors {
gitTemplate.writeCoAuthors(authorsSelected);
setMob = list.filter((author) => currentMob.includes(author.email));
} else {
mob.solo();
mob.removeGitMobSection();
gitTemplate.removeCoAuthors();
setMob = [];
}
Expand Down

0 comments on commit 2522d38

Please sign in to comment.