attempt to support short commit hashes (#18514)

* attempt to support short commit hashes

Not sure if that's the correct way and what happens when the short hash matches "head"

* need to remove #

* Output needs to be stripped of newlines
This commit is contained in:
Antonis Geralis 2021-07-18 16:29:49 +03:00 • committed by GitHub
commit 220b55c5d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -123,8 +123,12 @@ proc versionToCommit(d: Dependency): string =
return "" return ""
proc shortToCommit(short: string): string =
let (cc, status) = osproc.execCmdEx("git rev-parse " & quoteShell(short))
result = if status == 0: strutils.strip(cc) else: ""
proc checkoutGitCommit(c: var AtlasContext; p: PackageName; commit: string) = proc checkoutGitCommit(c: var AtlasContext; p: PackageName; commit: string) =
let (outp, status) = osproc.execCmdEx("git checkout " & quoteShell(commit)) let (_, status) = osproc.execCmdEx("git checkout " & quoteShell(commit))
if status != 0: if status != 0:
error(c, p, "could not checkout commit", commit) error(c, p, "could not checkout commit", commit)
@ -169,17 +173,23 @@ proc toName(p: string): PackageName =
proc needsCommitLookup(commit: string): bool {.inline} = proc needsCommitLookup(commit: string): bool {.inline} =
'.' in commit or commit == InvalidCommit '.' in commit or commit == InvalidCommit
proc isShortCommitHash(commit: string): bool {.inline.} =
commit.len >= 4 and commit.len < 40
proc checkoutCommit(c: var AtlasContext; w: Dependency) = proc checkoutCommit(c: var AtlasContext; w: Dependency) =
let dir = c.workspace / w.name.string let dir = c.workspace / w.name.string
withDir dir: withDir dir:
if w.commit.len == 0 or cmpIgnoreCase(w.commit, "#head") == 0: if w.commit.len == 0 or cmpIgnoreCase(w.commit, "head") == 0:
gitPull(c, w.name) gitPull(c, w.name)
else: else:
let err = isCleanGit(dir) let err = isCleanGit(dir)
if err != "": if err != "":
warn c, w.name, err warn c, w.name, err
else: else:
let requiredCommit = if needsCommitLookup(w.commit): versionToCommit(w) else: w.commit let requiredCommit =
if needsCommitLookup(w.commit): versionToCommit(w)
elif isShortCommitHash(w.commit): shortToCommit(w.commit)
else: w.commit
let (cc, status) = osproc.execCmdEx("git log -n 1 --format=%H") let (cc, status) = osproc.execCmdEx("git log -n 1 --format=%H")
let currentCommit = strutils.strip(cc) let currentCommit = strutils.strip(cc)
if requiredCommit == "" or status != 0: if requiredCommit == "" or status != 0:
@ -191,8 +201,9 @@ proc checkoutCommit(c: var AtlasContext; w: Dependency) =
if currentCommit != requiredCommit: if currentCommit != requiredCommit:
# checkout the later commit: # checkout the later commit:
# git merge-base --is-ancestor <commit> <commit> # git merge-base --is-ancestor <commit> <commit>
let (mergeBase, status) = osproc.execCmdEx("git merge-base " & let (cc, status) = osproc.execCmdEx("git merge-base " &
currentCommit.quoteShell & " " & requiredCommit.quoteShell) currentCommit.quoteShell & " " & requiredCommit.quoteShell)
let mergeBase = strutils.strip(cc)
if status == 0 and (mergeBase == currentCommit or mergeBase == requiredCommit): if status == 0 and (mergeBase == currentCommit or mergeBase == requiredCommit):
# conflict resolution: pick the later commit: # conflict resolution: pick the later commit:
if mergeBase == currentCommit: if mergeBase == currentCommit:
@ -242,7 +253,7 @@ proc collectNewDeps(c: var AtlasContext; work: var seq[Dependency];
tokens.add InvalidCommit tokens.add InvalidCommit
elif tokens.len == 2 and tokens[1].startsWith("#"): elif tokens.len == 2 and tokens[1].startsWith("#"):
# Dependencies can also look like 'requires "sdl2#head" # Dependencies can also look like 'requires "sdl2#head"
var commit = tokens[1] var commit = tokens[1][1 .. ^1]
tokens[1] = "==" tokens[1] = "=="
tokens.add commit tokens.add commit