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

Add --long-id Option to hmm lock Command #21

Merged
merged 4 commits into from
Jul 30, 2023
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
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