Skip to content

Commit

Permalink
Add git info (#340)
Browse files Browse the repository at this point in the history
Add helper to fetch git repo info
  • Loading branch information
hinthornw authored Jan 3, 2024
1 parent dd368d8 commit 9c2b756
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 1 deletion.
28 changes: 28 additions & 0 deletions python/langsmith/env/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Utilities to get information about the runtime environment."""
from langsmith.env._runtime_env import (
get_docker_compose_command,
get_docker_compose_version,
get_docker_environment,
get_docker_version,
get_langchain_env_vars,
get_langchain_environment,
get_release_shas,
get_runtime_and_metrics,
get_runtime_environment,
get_system_metrics,
)
from langsmith.env._git import get_git_info

__all__ = [
"get_docker_compose_command",
"get_docker_compose_version",
"get_docker_environment",
"get_docker_version",
"get_langchain_env_vars",
"get_langchain_environment",
"get_release_shas",
"get_runtime_and_metrics",
"get_runtime_environment",
"get_system_metrics",
"get_git_info",
]
58 changes: 58 additions & 0 deletions python/langsmith/env/_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Fetch information about any current git repo."""

import functools
import logging
import subprocess
from typing import List, Optional, TypeVar

from typing_extensions import TypedDict

logger = logging.getLogger(__name__)

T = TypeVar("T")


def _exec_git(command: List[str]) -> Optional[str]:
try:
return subprocess.check_output(
["git"] + command, encoding="utf-8", stderr=subprocess.DEVNULL
).strip()
except FileNotFoundError:
logger.warning("git is not installed, or cannot be found in PATH")
return None
except subprocess.CalledProcessError as e:
logger.debug(f"Error running git command: {e}")
return None


class GitInfo(TypedDict, total=False):
remote_url: Optional[str]
commit: Optional[str]
branch: Optional[str]
author_name: Optional[str]
author_email: Optional[str]
commit_message: Optional[str]
commit_time: Optional[str]
dirty: Optional[bool]
tags: Optional[str]


@functools.lru_cache(maxsize=1)
def get_git_info(remote: str = "origin") -> Optional[GitInfo]:
"""Get information about the git repository."""

if not _exec_git(["rev-parse", "--is-inside-work-tree"]):
return None

return {
"remote_url": _exec_git(["remote", "get-url", remote]),
"commit": _exec_git(["rev-parse", "HEAD"]),
"commit_time": _exec_git(["log", "-1", "--format=%ct"]),
"branch": _exec_git(["rev-parse", "--abbrev-ref", "HEAD"]),
"tags": _exec_git(
["describe", "--tags", "--exact-match", "--always", "--dirty"]
),
"dirty": _exec_git(["status", "--porcelain"]) != "",
"author_name": _exec_git(["log", "-1", "--format=%an"]),
"author_email": _exec_git(["log", "-1", "--format=%ae"]),
}
File renamed without changes.
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langsmith"
version = "0.0.76"
version = "0.0.77"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down
28 changes: 28 additions & 0 deletions python/tests/unit_tests/test_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from langsmith.env import __all__ as env_all
from langsmith.env import get_git_info

_EXPECTED = [
"get_docker_compose_command",
"get_docker_compose_version",
"get_docker_environment",
"get_docker_version",
"get_langchain_env_vars",
"get_langchain_environment",
"get_release_shas",
"get_runtime_and_metrics",
"get_runtime_environment",
"get_system_metrics",
"get_git_info",
]


def test_public_api() -> None:
assert env_all == _EXPECTED


def test_git_info() -> None:
git_info = get_git_info()
assert git_info is not None
assert git_info["commit"] is not None
assert git_info["remote_url"] is not None
assert "langsmith-sdk" in git_info["remote_url"]

0 comments on commit 9c2b756

Please sign in to comment.