Skip to content

[WIP] bin: custom git commands #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bin/git/git-node
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node
'use strict';

const CMD = process.argv[2];
const path = require('path');
const { execSync } = require('child_process');
const fs = require('fs');

if (!CMD) {
console.log('Run `git node help` to see how to use this');
process.exit(1);
}

const script = path.join(__dirname, `git-node-${CMD}`);
if (!fs.existsSync(script)) {
console.log(`No such command: git node ${CMD}`);
process.exit(1);
}

execSync(script, process.argv.slice(2), {
stdio: 'inherit'
});
23 changes: 23 additions & 0 deletions bin/git/git-node-ammend
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

if [[ $1 -eq 0 ]] ; then
echo 'no patch number specified!'
exit 1
fi

set -x;
REV=$(git rev-parse HEAD)
SHORT_REV=${REV:0:7}
MSG=.ncu/msg-$SHORT_REV.txt

if [ ! -e $MSG ] ; then
git node msg $1;
fi;

git show $REV -s --format=%B | git node check-msg $MSG
if [ ! $? -eq 0 ]; then
echo "commit message is already ammended"
else
git commit --amend -F $MSG
fi

10 changes: 10 additions & 0 deletions bin/git/git-node-apply
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -x;

if [[ $1 -eq 0 ]] ; then
echo 'no patch number specified!'
exit 1
fi

curl -L "https://github.com/nodejs/node/pull/$1.patch" | git am --whitespace=fix
14 changes: 14 additions & 0 deletions bin/git/git-node-check-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

const fs = require('fs');

const file = process.argv[2];
const msg = fs.readFileSync(0, 'utf8');
const toAmmend = fs.readFileSync(file, 'utf8');
const META_RE = /PR-URL:|Reviewed-By:/;

for (const line of msg.split('\n')) {
if (line.match(META_RE)) {
throw new Error(`Found ${line}`);
}
}
9 changes: 9 additions & 0 deletions bin/git/git-node-final
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

UPSTREAM=$(git node-rc upstream)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
git rev-list $UPSTREAM/$BRANCH...HEAD | xargs core-validate-commit

# clean up when it passes
# rm .ncu/msg-*.txt .ncu/metadata-*.txt

14 changes: 14 additions & 0 deletions bin/git/git-node-help
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

echo "
1) To land single a commit:
git node just <PRID>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung just want to know am i mistaken or this should be git-node instead of git node
as we have git-node in package.json?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The git custom commands work like this: if you have an executable named "git-*" on your path, the it will be run when you do "git *"


2) To land multiple commits:
git node apply <PRID>
git rebase -i upstream/master # edit every commit that's gonna stay
# on each stay
git node ammend <PRID>
git rebase --continue
# when the rebase is done
git node final"
43 changes: 43 additions & 0 deletions bin/git/git-node-just
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

if [[ $1 -eq 0 ]] ; then
echo 'no patch number specified!'
exit 1
fi

set -x

# grab the patch
mkdir -p .ncu
PATCH=.ncu/patch-$1.patch
curl -L "https://github.com/nodejs/node/pull/$1.patch" > $PATCH
NUM_COMMITS=`cat $PATCH | grep -c "Subject: \[PATCH\]"`

if [[ ! $NUM_COMMITS -eq 1 ]] ; then
echo 'There are multiple commits in this patch, can not land using `git node just`'
exit 1
fi

# abort any previous session
git am --abort

# apply the patch
cat $PATCH | git am --whitespace=fix

# build message
git node msg $1;

REV=$(git rev-parse HEAD)
SHORT_REV=${REV:0:7}
MSG=.ncu/msg-$SHORT_REV.txt
META=.ncu/metadata-$1.txt;

# ammend the message
git show $REV -s --format=%B | git node check-msg $MSG
if [ ! $? -eq 0 ]; then
echo "commit message is already ammended"
else
git commit --amend -F $MSG
fi

rm $MSG $PATCH $META
10 changes: 10 additions & 0 deletions bin/git/git-node-meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

if [[ $1 -eq 0 ]] ; then
echo 'no patch number specified!'
exit 1
fi

mkdir -p .ncu
META=.ncu/metadata-$1.txt;
get-metadata $1 -f $META
25 changes: 25 additions & 0 deletions bin/git/git-node-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

if [[ $1 -eq 0 ]] ; then
echo 'no patch number specified!'
exit 1
fi

META=.ncu/metadata-$1.txt;

if [ ! -e $META ] ; then
git node meta $1 -f $META
fi;

set -x;
UPSTREAM=$(git node-rc upstream)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
REV=$(git rev-parse HEAD)
#REVS=$(git rev-list $UPSTREAM/$BRANCH...HEAD)

#for rev in $REVS; do
SHORT_REV=${REV:0:7}
MSG=.ncu/msg-$SHORT_REV.txt
echo -e "$(git show $rev -s --format=%B)\n\n$(cat $META)" > $MSG;
#done

14 changes: 14 additions & 0 deletions bin/git/git-node-rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

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

const field = process.argv[2];
if (!field) {
throw Error('no field specified!');
}

const ncurcPath = path.join(os.homedir(), '.ncurc');
const ncurc = JSON.parse(fs.readFileSync(ncurcPath, 'utf8'));
console.log(ncurc[field]);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Utilities for Node.js core collaborators",
"main": "./bin/metadata.js",
"bin": {
"get-metadata": "./bin/get_metadata.js"
"get-metadata": "./bin/get_metadata.js",
"git-node": "./bin/git/git-node"
},
"scripts": {
"test": "npm run test-unit && npm run lint",
Expand Down