1
\$\begingroup\$

I'm looking for improvements of a code that filter the output of git status --ignored and git status --ignored -bs commands. I feel that it could be more readable but I'm not sure how since I'm a beginner. Please, all your critics and advice are welcome.

This is my code:

function filter(str, regExp) {
  const reg = new RegExp(`(?<=${regExp}\\s+)\\S+`, 'gm');
  return str.match(reg);
}

function excludingFilter(str, regExp) {
  const reg = new RegExp(
    `(?<=(^(?!\\s+(?:${regExp}))(?:\\s*.+:\\s+))|(^(?!${regExp}|#)..\\s))[^\\s].+`,
    'gm',
  );
  return str.match(reg);
}

function extractData(str) {
  return str.match(/\b.+/gm);
}

function gitStatusToJsonCase1(str) {
  const localBranch = str.match(/(?<=## )\w+\S?\w+/g);
  const remoteBranch = str.match(/(?<=##.+\.{3}).+/g);
  const addedIndex = filter(str, 'A.');
  const modifiedIndex = filter(str, 'M.');
  const otherIndex = excludingFilter(str, '(A.|M.| .|!!|\\?\\?)');
  const modifiedTree = filter(str, '.M');
  const otherTree = excludingFilter(str, '(.M|. |!!|\\?\\?)');
  const untracked = filter(str, '\\?\\?');
  const ignored = filter(str, '!!');

  const json = {
    branch: {
      local: localBranch,
      remote: remoteBranch,
    },
    index: {
      added: addedIndex,
      modified: modifiedIndex,
      other: otherIndex,
    },
    tree: {
      modified: modifiedTree,
      untracked,
      ignored,
      other: otherTree,
    },
  };
  return json;
}

function gitStatusToJsonCase2(str) {
  const gitStatusRegExp = new RegExp(
    `On\\sbranch\\s(?<local>.+)\n(?:Your.*'(?<remote>.+)'.)?(?:\\s+)?(?:Changes\\st.+\\s+.+e\\)\n+(?<stage>(?:.+\n)+))?(?:\\s+)?(?:Changes\\sn.+\\s+.+\\s+.+y\\)\n+(?<tree>(?:.+\n)+))?(?:\\s+)?(?:Untracked.+\n\\s+.+\n+(?<untracked>(?:.+\n)+))?(?:\\s+)(?:Ignored.+\n\\s+.+\n+(?<ignored>(?:.+\n)+))?`,
    'gm',
  );
  const groupedData = gitStatusRegExp.exec(str).groups;
  const localBranch = groupedData.local;
  const remoteBranch = groupedData.remote;
  const addedIndex = filter(groupedData.stage, 'new file:');
  const modifiedIndex = filter(groupedData.stage, 'modified:');
  const otherIndex = excludingFilter(groupedData.stage, '(modified|new)');
  const modifiedTree = filter(groupedData.tree, 'modified:');
  const otherTree = excludingFilter(groupedData.tree, 'modified:');
  const untracked = extractData(groupedData.untracked);
  const ignored = extractData(groupedData.ignored);

  const json = {
    branch: {
      local: localBranch,
      remote: remoteBranch,
    },
    index: {
      added: addedIndex,
      modified: modifiedIndex,
      other: otherIndex,
    },
    tree: {
      modified: modifiedTree,
      untracked,
      ignored,
      other: otherTree,
    },
  };
  return json;
}

console.log('TTTTTTTTTTTT git status  --ignored -bs TTTTTTTTTTTTTTTT');
console.log(gitStatusToJsonCase1(cases.C));
console.log('TTTTTTTTTTTT git status  --ignored TTTTTTTTTTTTTTTT');
console.log(gitStatusToJson(cases.D));

module.exports = { gitStatusToJsonCase1, gitStatusToJson };

The git status outputs that I'm filtering are like the following:

git status --ignored:

On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    new file:   a1
    new file:   prueba2
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   package.json
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    prueba.txt
    test2
Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
    node_modules/

git status --ignored -bs:

## master...origin/master
A  a1
 M package.json
A  prueba2
?? prueba.txt
?? test1
!! node_modules/

And my results are like this:

{ branch: { local: [ 'feature/cleanup' ], remote: undefined },
  index:
   { added:
      [ 'backend/.eslintrc.json.old', 'frontend/.eslintrc.json.old' ],
     modified: [ 'README.md', 'parser-preset.js' ],
     other: [ 'frontend/.editorconfig -> frontend/.editorconfig.old' ] },
  tree:
   { modified:
      [ '.eslintignore',
        'deploy/docker-compose.yml',
        'frontend/.eslintrc.json.old' ],
     untracked: [ 'my-app/', 'package-lock.json', 'test3.txt' ],
     ignored:
      [ 'backend/dist/',
        'backend/node_modules/',

        'node_modules/' ],
     other: null } }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.