Skip to content
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

New commands, bug fixes and support for aliases #54

Merged
merged 20 commits into from
Aug 16, 2017
Merged
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
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ mgit exec 'echo `pwd`'
# /home/mgit/packages/organization/repository-2
```


### save-hashes

Saves hashes of packages in `mgit.json`. It allows to easily fix project to a specific state.
Expand All @@ -255,6 +254,64 @@ Example:
mgit save-hashes
```

### status (alias: `st`)

Prints a table which contains useful information about the status of repositories.

Example:

```bash
mgit status
# or
mgit st
```

In order to save space in your terminal, you can define the `packagesPrefix` option in your configuration file.
The prefix will be removed from packages' names. Full names of packages aren't needed so we can cat the names.

![An example response of `mgit status` command.](https://user-images.githubusercontent.com/2270764/28871104-5915289e-7783-11e7-8d06-9eac6d7d96ab.png)

### diff

Prints changes from packages where something has changed.

It accepts additional options which will be passed directly to the `git diff` command which is used to gathering the changes.

These options must be separated by a double dash `--`, in the same way as [`npm scripts`](https://docs.npmjs.com/cli/run-script#synopsis)
does.

Example:

Prints changes from all repositories:

```bash
mgit diff
```

Prints diffstat from all repositories:

```bash
mgit diff -- --stat
```

Prints staged changes from restricted scope:

```bash
mgit diff --scope=*@(engine|typing)* -- --staged
```

![An example response of `mgit diff` command.](https://user-images.githubusercontent.com/2270764/28918716-c6f90002-784a-11e7-95ae-8d08c47c5427.png)

### checkout (alias: `co`)

Changes branches in repositories according to the configuration file. It does not pull the changes. The command is helpful for bisecting.

```bash
mgit checkout
# or
mgit co
```

## Projects using mgit2

* [CKEditor 5](https://github.com/ckeditor/ckeditor5)
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
const meow = require( 'meow' );
const mgit = require( './lib/index' );

const meowOptions = {
alias: {
v: 'version'
}
};

const cli = meow( `
_ _
(_) |
Expand All @@ -28,6 +34,9 @@ const cli = meow( `
exec Executes shell command in each package.
update Updates packages to the latest versions (i.e. pull changes).
save-hashes Saves hashes of packages in mgit.json. It allows to easily fix project to a specific state.
status Prints a table which contains useful information about the status of repositories.
diff Prints changes from packages where something has changed.
checkout Changes branches in repositories according to the configuration file.

Options:
--recursive Whether to install dependencies recursively.
Expand Down Expand Up @@ -78,11 +87,7 @@ const cli = meow( `
--scope Restricts the command to packages which names match the given glob pattern.

Default: null
`, {
alias: {
v: 'version'
}
} );
`, meowOptions );

if ( cli.input.length === 0 ) {
cli.showHelp();
Expand Down
76 changes: 37 additions & 39 deletions lib/commands/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,55 @@ module.exports = {
execute( data ) {
const log = require( '../utils/log' )();

return new Promise( ( resolve, reject ) => {
const destinationPath = path.join( data.options.packages, data.repository.directory );
const destinationPath = path.join( data.options.packages, data.repository.directory );
Copy link
Member

Choose a reason for hiding this comment

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

Did you change anything in this file or is it just a code style?

Copy link
Member

Choose a reason for hiding this comment

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

If so – why changing anything?

Copy link
Member Author

Choose a reason for hiding this comment

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

Changes in bootstrap, savehashes and update will resolve #45 and #49.


let promise;
let promise;

// Package is already cloned.
if ( fs.existsSync( destinationPath ) ) {
log.info( `Package "${ data.packageName }" is already cloned.` );
// Package is already cloned.
if ( fs.existsSync( destinationPath ) ) {
log.info( `Package "${ data.packageName }" is already cloned.` );

promise = Promise.resolve();
} else {
const command = [
`git clone --progress ${ data.repository.url } ${ destinationPath }`,
`cd ${ destinationPath }`,
`git checkout --quiet ${ data.repository.branch }`
].join( ' && ' );
promise = Promise.resolve();
} else {
const command = [
`git clone --progress ${ data.repository.url } ${ destinationPath }`,
`cd ${ destinationPath }`,
`git checkout --quiet ${ data.repository.branch }`
].join( ' && ' );

promise = exec( command );
}
promise = exec( command );
}

promise
.then( output => {
log.info( output );
return promise
.then( output => {
log.info( output );

const commandOutput = {
logs: log.all()
};
const commandOutput = {
logs: log.all()
};

if ( data.options.recursive ) {
const packageJson = require( path.join( destinationPath, 'package.json' ) );
const packages = [];
if ( data.options.recursive ) {
const packageJson = require( path.join( destinationPath, 'package.json' ) );
const packages = [];

if ( packageJson.dependencies ) {
packages.push( ...Object.keys( packageJson.dependencies ) );
}

if ( packageJson.devDependencies ) {
packages.push( ...Object.keys( packageJson.devDependencies ) );
}
if ( packageJson.dependencies ) {
packages.push( ...Object.keys( packageJson.dependencies ) );
}

commandOutput.packages = packages;
if ( packageJson.devDependencies ) {
packages.push( ...Object.keys( packageJson.devDependencies ) );
}

resolve( commandOutput );
} )
.catch( error => {
log.error( error );
commandOutput.packages = packages;
}

return Promise.resolve( commandOutput );
} )
.catch( error => {
log.error( error );

reject( { logs: log.all() } );
} );
} );
return Promise.reject( { logs: log.all() } );
} );
},

/**
Expand Down
32 changes: 32 additions & 0 deletions lib/commands/checkout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

'use strict';

module.exports = {
/**
* @param {Object} data
* @param {Object} data.repository
* @param {String} data.repository.branch Name of branch (or commit hash) saved in `mgit.json` file.
* @returns {Promise}
*/
execute( data ) {
const execCommand = require( './exec' );
const checkoutCommand = `git checkout ${ data.repository.branch }`;

return execCommand.execute( getExecData( checkoutCommand ) )
.then( execResponse => {
execResponse.logs.info = execResponse.logs.info[ 0 ].split( '\n' ).slice( -1 );

return Promise.resolve( execResponse );
} );

function getExecData( command ) {
return Object.assign( {}, data, {
arguments: [ command ]
} );
}
}
};
41 changes: 41 additions & 0 deletions lib/commands/diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

'use strict';

const chalk = require( 'chalk' );

module.exports = {
/**
* @param {Object} data
* @param {Array.<String>} data.arguments The rest of arguments provided by the user. These options will modify the `git diff` command.
* @returns {Promise}
*/
execute( data ) {
const execCommand = require( './exec' );
const diffCommand = ( 'git diff --color ' + data.arguments.join( ' ' ) ).trim();

return execCommand.execute( getExecData( diffCommand ) )
.then( execResponse => {
if ( !execResponse.logs.info.length ) {
return Promise.resolve( {} );
}

return Promise.resolve( {
logs: execResponse.logs
} );
} );

function getExecData( command ) {
return Object.assign( {}, data, {
arguments: [ command ]
} );
}
},

afterExecute() {
console.log( chalk.blue.italic( 'Logs are displayed from repositories which contain any change.' ) );
}
};
31 changes: 12 additions & 19 deletions lib/commands/savehashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,22 @@ module.exports = {
const log = require( '../utils/log' )();
const execCommand = require( './exec' );

return new Promise( ( resolve, reject ) => {
execCommand.execute( getExecData( 'git rev-parse HEAD' ) )
.then( execResponse => {
const commitHash = execResponse.logs.info[ 0 ];
return execCommand.execute( getExecData( 'git rev-parse HEAD' ) )
Copy link
Member

Choose a reason for hiding this comment

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

The same question as about bootstrap.

.then( execResponse => {
const commitHash = execResponse.logs.info[ 0 ];

const commandResponse = {
packageName: data.packageName,
commit: commitHash.slice( 0, 7 ) // Short version of the commit hash.
};
const commandResponse = {
packageName: data.packageName,
commit: commitHash.slice( 0, 7 ) // Short version of the commit hash.
};

log.info( `Commit: "${ commitHash }".` );
log.info( `Commit: "${ commitHash }".` );

resolve( {
response: commandResponse,
logs: log.all()
} );
} )
.catch( error => {
log.error( error );

reject( { logs: log.all() } );
return Promise.resolve( {
response: commandResponse,
logs: log.all()
} );
} );
} );

function getExecData( command ) {
return Object.assign( {}, data, {
Expand Down
Loading