Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git log graph into array

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!

like image 744
Nulji Avatar asked Aug 01 '26 08:08

Nulji


2 Answers

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);
like image 78
Jeto Avatar answered Aug 03 '26 22:08

Jeto


You can do something like this with the regex.

  1. alt regex
    • ^\W+ - This matches all the non word character at start of string.
  2. commitHash regex
    • \w+ - This matches all the word characters and we select only first match
  3. branches regex
    • /\(.*?\)/ - This matches all the branches ( branches here )
    • now from the matches branches we replace (Head -> and () with replace
  4. commit 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

like image 32
Code Maniac Avatar answered Aug 03 '26 21:08

Code Maniac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!