Skip to content

Commit

Permalink
Merge pull request #21 from steviegt6/features/long-id
Browse files Browse the repository at this point in the history
Add `--long-id` Option to `hmm lock` Command
  • Loading branch information
andywhite37 authored Jul 30, 2023
2 parents b3edf34 + 4900cb9 commit f26d3a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Binary file modified run.n
Binary file not shown.
23 changes: 16 additions & 7 deletions src/hmm/commands/LockVersionCommand.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import thx.Validation;
import hmm.HmmConfig;
import hmm.LibraryConfig;
import hmm.errors.ValidationError;
import hmm.utils.Args;
import hmm.utils.Shell;
import hmm.utils.Log;

Expand All @@ -26,7 +27,11 @@ class LockVersionCommand implements ICommand {
var config = HmmConfigs.readHmmJsonOrThrow();

var libs = config.dependencies;
if (args.length > 0) {
var longId = Args.hasAny(args, ["--long-id", "-l"]);

// Filter out options.
var possibleLibs = args.filter(function (arg) return !arg.startsWith("--") && !arg.startsWith("-"));
if (possibleLibs.length > 0) {
libs = libs.filter(function (lib) return args.indexOf(LibraryConfigs.getName(lib)) >= 0);
}

Expand All @@ -36,7 +41,7 @@ class LockVersionCommand implements ICommand {
checks++;
if (switch library {
case Haxelib(name, version) : lockHaxelibVersion(name, version);
case Git(name, url, ref, dir) : lockGitVersion(name, url, ref, dir);
case Git(name, url, ref, dir) : lockGitVersion(name, url, ref, dir, longId);
case Mercurial(name, url, ref, dir) : lockHgVersion(name, url, ref, dir);
case Dev(name, path) : false;
}) successes.push(library);
Expand All @@ -51,7 +56,10 @@ class LockVersionCommand implements ICommand {
public function getUsage() {
return 'update libraries versions or refs in hmm.json.
usage: hmm lock [<lib1> [... <libn>]]
usage: hmm lock [-l|--long-id] [<lib1> [... <libn>]]

options:
(optional) -l or --long-id - use the full commit hash (id) for git libraries

arguments: zero or more library names

Expand Down Expand Up @@ -100,10 +108,10 @@ class LockVersionCommand implements ICommand {
return newRef;
}

function lockGitVersion(name : String, url : String, ref : Option<String>, dir : Option<String>) {
function lockGitVersion(name : String, url : String, ref : Option<String>, dir : Option<String>, longId : Bool) {
if (!FileSystem.exists('.haxelib/$name/git/.git')) throw new ValidationError('Library $name is not checked out', 1);

var newRef = getGitRef(name);
var newRef = getGitRef(name, longId);

if (switch ref {
case None: true;
Expand All @@ -116,13 +124,14 @@ class LockVersionCommand implements ICommand {
return false;
}

function getGitRef(name : String) {
function getGitRef(name : String, longId : Bool) {
// Attempt to find a tag pointing to the current commit,
// otherwise get the current commit hash
var cwd = Sys.getCwd();
Sys.setCwd('.haxelib/$name/git');
var tag = Shell.readCommand("git", ["tag", "--points-at", "HEAD"], { log: false, throwError: true });
var result = Shell.readCommand("git", ["rev-parse", "--short", "HEAD"], { log: false, throwError: true });
var revParseArgs = longId ? ["rev-parse", "HEAD"] : ["rev-parse", "--short", "HEAD"];
var result = Shell.readCommand("git", revParseArgs, { log: false, throwError: true });
Sys.setCwd(cwd);

var newTag = tag.stdout.trim();
Expand Down

0 comments on commit f26d3a8

Please sign in to comment.