I need to convert a git log output into object.
I'm using the command git log --all --graph --oneline --date-order.
* 8896805 (origin/firstbranch) test(firstbranch): this is my commit on this branch
| * 5fef2da (HEAD -> testbranch, origin/another, another) refactor(another): this is a change
| * 2a34d72 refactor(another): typo
| * 683d704 refactor(another): changes
* | 0120274 Merge remote-tracking branch 'origin/develop' into firstbranch
|\ \
After that I created an array where each element of the array corresponds to a line of this output.
And with this, I would like to have an array like this :
[
{
art: "*",
commitHash: "8896805",
branches: ["origin/firstbranch"],
commitString: "test(firstbranch): this is my commit on this branch"
},
{
art: "| *",
commitHash: "5fef2da",
branches: ["testbranch", "origin/another", "another"],
commitString: "refactor(another): this is a change"
},
{
art: "| *",
commitHash: "2a34d72",
branches: [],
commitString: "refactor(another): typo"
},
{
art: "| *",
commitHash: "683d704",
branches: [],
commitString: "refactor(another): changes"
},
{
art: "* |",
commitHash: "0120274",
branches: [],
commitString: "Merge remote-tracking branch 'origin/develop' into firstbranch"
},
{
art: "|\ \",
commitHash: "",
branches: [],
commitString: ""
}
]
But I can't obtain this result.
Thanks by advance!
Here's a possible solution using a single regular expression, then trimming each value, and finally splitting branches into an array:
const input = [
'* 8896805 (origin/firstbranch) test(firstbranch): this is my commit on this branch',
'| * 5fef2da (HEAD -> testbranch, origin/another, another) refactor(another): this is a change',
'| * 2a34d72 refactor(another): typo',
'| * 683d704 refactor(another): changes',
'* | 0120274 Merge remote-tracking branch \'origin/develop\' into firstbranch',
'|\\ \\'
];
const result = input.map(entry => {
let [, alt, commitHash, branches, commitString] = entry
.match(/^([|*\s\\]+)(?:\s+([a-f0-9]+)?(?:\s+\((?:HEAD\s+->\s+)?([^)]+)\))?(?:\s+(.*))?)?$/)
.map(value => value ? value.trim() : '');
branches = branches ? branches.split(/\s*,\s*/) : [];
return {alt, commitHash, branches, commitString};
});
console.log(result);
You can do something like this with the regex.
alt regex
^\W+ - This matches all the non word character at start of string.commitHash regex
\w+ - This matches all the word characters and we select only first matchbranches regex
/\(.*?\)/ - This matches all the branches ( branches here )(Head -> and () with replacecommit regex
/.*?\)|\W+/ - Replace all the character upto first ) or all the non word characters.let str = `* 8896805 (origin/firstbranch) test(firstbranch): this is my commit on this branch
| * 5fef2da (HEAD -> testbranch, origin\/another, another) refactor(another): this is a change
| * 2a34d72 refactor(another): typo
| * 683d704 refactor(another): changes
* | 0120274 Merge remote-tracking branch 'origin/develop' into firstbranch
|\\ \\`
let splited = str.split('\n')
let op = splited.map(inp=>{
let alt = ""
inp = inp.replace(/^\W+/,(match)=>{alt=match; return ''})
let commitHash = ''
inp = inp.replace(/\s*(\w+)\s*/,(match,g1)=>{commitHash = g1;return ''})
let branches = ''
inp = inp.replace(/^\s*(\(.*?\))/,(match,g1)=>{branches = g1; return ''})
branches = branches ? branches.replace(/^\s*\(HEAD\s*->\s*|[)(]/g,'').split(/\s*,\s*/) : []
let commit = inp.trim()
return {alt,commitHash,branches,commit}
})
console.log(op)
On Side Note:- Please note this is not full proof. i have considered cases as per provided input but there can be many more cases
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With