Skip to content

Commit

Permalink
feat(git): Add start-revision and file arguments to git log
Browse files Browse the repository at this point in the history
  • Loading branch information
AtkinsSJ authored and KernelDeimos committed Jun 28, 2024
1 parent eb2b6a0 commit 49c2f16
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
31 changes: 30 additions & 1 deletion packages/git/src/git-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import path from 'path-browserify';
import git from 'isomorphic-git';

export const PROXY_URL = 'https://cors.isomorphic-git.org';

Expand Down Expand Up @@ -110,3 +109,33 @@ export const determine_fetch_remote = (remote_name_or_url, remotes) => {
throw new Error(`'${remote_name_or_url}' does not appear to be a git repository`);
return remote_data;
}

/**
* Divide up the positional arguments into those before the `--` separator, and those after it.
* @param arg_tokens Tokens array from parseArgs({ tokens: true })
* @returns {{before: string[], after: string[]}}
*/
export const group_positional_arguments = (arg_tokens) => {
let saw_separator = false;
const result = {
before: [],
after: [],
};

for (const token of arg_tokens) {
if (token.kind === 'option-terminator') {
saw_separator = true;
continue;
}
if (token.kind === 'positional') {
if (saw_separator) {
result.after.push(token.value);
} else {
result.before.push(token.value);
}
continue;
}
}

return result;
}
22 changes: 14 additions & 8 deletions packages/git/src/subcommands/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import git from 'isomorphic-git';
import { find_repo_root } from '../git-helpers.js';
import { find_repo_root, group_positional_arguments } from '../git-helpers.js';
import { commit_formatting_options, format_commit, process_commit_formatting_options } from '../format.js';
import { SHOW_USAGE } from '../help.js';

export default {
name: 'log',
usage: 'git log [<formatting-option>...] [--max-count <n>] <revision>',
usage: 'git log [<formatting-option>...] [--max-count <n>] [<revision>] [[--] <path>]',
description: 'Show commit logs, starting at the given revision.',
args: {
allowPositionals: false,
allowPositionals: true,
tokens: true,
options: {
...commit_formatting_options,
'max-count': {
Expand All @@ -38,23 +40,27 @@ export default {
execute: async (ctx) => {
const { io, fs, env, args } = ctx;
const { stdout, stderr } = io;
const { options, positionals } = args;
const { options, positionals, tokens } = args;

process_commit_formatting_options(options);

// TODO: Log of a specific file
// TODO: Log of a specific branch
// TODO: Log of a specific commit

const depth = Number(options['max-count']) || undefined;

const { dir, gitdir } = await find_repo_root(fs, env.PWD);

const { before: refs, after: paths } = group_positional_arguments(tokens);
if (refs.length > 1 || paths.length > 1) {
stderr('error: Too many revisions or paths given. Expected [<revision>] [[--] <path>]');
throw SHOW_USAGE;
}

const log = await git.log({
fs,
dir,
gitdir,
depth,
ref: refs[0],
filepath: paths[0],
});

for (const commit of log) {
Expand Down

0 comments on commit 49c2f16

Please sign in to comment.