Skip to content

Git

Git helper functions.

ChangeType

Bases: Enum

Git change kinds, retaining the original public enum values.

Source code in databooks/git_utils.py
21
22
23
24
25
26
27
28
29
30
class ChangeType(Enum):
    """Git change kinds, retaining the original public enum values."""

    A = 1
    C = 2
    D = 3
    R = 4
    M = 5
    T = 6
    U = 7

ConflictFile dataclass

Container for path and different versions of conflicted notebooks.

Source code in databooks/git_utils.py
41
42
43
44
45
46
47
48
49
@dataclass
class ConflictFile:
    """Container for path and different versions of conflicted notebooks."""

    filename: Path
    first_log: str
    last_log: str
    first_contents: str | bytes
    last_contents: str | bytes

Contents dataclass

Container for path of file versions.

Source code in databooks/git_utils.py
52
53
54
55
56
57
@dataclass
class Contents:
    """Container for path of file versions."""

    path: Optional[Path]
    contents: Optional[str | bytes]

DiffContents dataclass

Container for path and different versions of conflicted notebooks.

Source code in databooks/git_utils.py
60
61
62
63
64
65
66
@dataclass
class DiffContents:
    """Container for path and different versions of conflicted notebooks."""

    a: Contents
    b: Contents
    change_type: ChangeType

UnmergedBlob dataclass

Container for git unmerged blobs.

Source code in databooks/git_utils.py
33
34
35
36
37
38
@dataclass
class UnmergedBlob:
    """Container for git unmerged blobs."""

    filename: Path
    stage: Dict[int, Blob]

blob2commit(blob, repo)

Get the short commit message from blob hash.

Source code in databooks/git_utils.py
82
83
84
85
86
87
88
89
90
def blob2commit(blob: Blob, repo: Repo) -> str:
    """Get the short commit message from blob hash."""
    _git = Git(working_dir=repo.working_dir)
    commit_id = _git.log(find_object=blob, max_count=1, all=True, oneline=True)
    return (
        commit_id
        if len(commit_id) > 0
        else _git.stash("list", "--oneline", "--max-count", "1", "--find-object", blob)
    )

blob2str(blob)

blob2str(blob: None) -> None
blob2str(blob: Blob) -> bytes

Get the blob contents if they exist (otherwise return None).

Source code in databooks/git_utils.py
77
78
79
def blob2str(blob: Optional[Blob]) -> Optional[bytes]:
    """Get the blob contents if they exist (otherwise return `None`)."""
    return blob.data_stream.read() if blob is not None else None

diff2contents(blob, ref, path, not_exists=False)

Get the blob contents from the diff.

Depends on whether we are diffing against current working tree and if object exists at diff time (added or deleted objects only exist at one side). If comparing against working tree (ref=None) we return the current file contents.

Parameters:

Name Type Description Default
blob Blob

git diff blob

required
ref Optional[Union[IndexFile, Tree, Commit, str]]

git reference

required
path Path

path to object

required
not_exists bool

whether object exists at 'diff time' (added or removed objects do not exist)

False

Returns:

Type Description
Optional[str | bytes]

blob contents as a string (if exists)

Source code in databooks/git_utils.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def diff2contents(
    blob: Blob,
    ref: Optional[Union[IndexFile, Tree, Commit, str]],
    path: Path,
    not_exists: bool = False,
) -> Optional[str | bytes]:
    """
    Get the blob contents from the diff.

    Depends on whether we are diffing against current working tree and if object exists
     at diff time (added or deleted objects only exist at one side). If comparing
     against working tree (`ref=None`) we return the current file contents.
    :param blob: git diff blob
    :param ref: git reference
    :param path: path to object
    :param not_exists: whether object exists at 'diff time' (added or removed objects
     do not exist)
    :return: blob contents as a string (if exists)
    """
    if not_exists:
        return None
    elif ref is None:
        return path.read_text(encoding="utf-8")
    else:
        return blob2str(blob)

get_conflict_blobs(repo)

Get the source files for conflicts.

Source code in databooks/git_utils.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def get_conflict_blobs(repo: Repo) -> List[ConflictFile]:
    """Get the source files for conflicts."""
    unmerged_blobs = repo.index.unmerged_blobs()
    blobs = (
        UnmergedBlob(filename=Path(k), stage=dict(v))
        for k, v in unmerged_blobs.items()
        if 0 not in dict(v).keys()  # only get blobs that could not be merged
    )

    if not isinstance(repo.working_dir, (Path, str)):
        raise RuntimeError(
            "Expected `repo` to be `pathlib.Path` or `str`, got"
            f" {type(repo.working_dir)}."
        )
    return [
        ConflictFile(
            filename=repo.working_dir / blob.filename,
            first_log=blob2commit(blob=blob.stage[2], repo=repo),
            last_log=blob2commit(blob=blob.stage[3], repo=repo),
            first_contents=blob2str(blob.stage[2]),
            last_contents=blob2str(blob.stage[3]),
        )
        for blob in blobs
    ]

get_nb_diffs(ref_base=None, ref_remote=None, paths=(), *, repo=None, verbose=False)

Get the noteebook(s) git diff(s).

By default, diffs are compared with the current working directory. That is, staged files will still show up in the diffs. Only return the diffs for notebook files.

Source code in databooks/git_utils.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def get_nb_diffs(
    ref_base: Optional[str] = None,
    ref_remote: Optional[str] = None,
    paths: Sequence[Path] = (),
    *,
    repo: Optional[Repo] = None,
    verbose: bool = False,
) -> List[DiffContents]:
    """
    Get the noteebook(s) git diff(s).

    By default, diffs are compared with the current working directory. That is, staged
     files will still show up in the diffs. Only return the diffs for notebook files.
    """
    if verbose:
        set_verbose(logger)

    common_path = find_common_parent(paths or [Path.cwd()])
    repo = get_repo(path=common_path) if repo is None else repo
    if repo is None or repo.working_dir is None:
        raise ValueError("No repo found - cannot compute diffs.")

    base = repo.index if ref_base is None else repo.tree(ref_base)
    remote = None if ref_remote is None else repo.tree(ref_remote)

    logger.debug(
        f"Looking for diffs on path(s) {[p.resolve() for p in paths]}.\n"
        f"Comparing `{base}` and `{remote}`."
    )
    repo_root_dir = Path(repo.working_dir)
    return [
        DiffContents(
            a=Contents(
                path=Path(cast(str, d.a_path)),
                contents=diff2contents(
                    blob=cast(Blob, d.a_blob),
                    ref=base,
                    path=repo_root_dir / cast(str, d.a_path),
                    not_exists=d.change_type is ChangeType.A,
                ),
            ),
            b=Contents(
                path=Path(cast(str, d.b_path)),
                contents=diff2contents(
                    blob=cast(Blob, d.b_blob),
                    ref=remote,
                    path=repo_root_dir / cast(str, d.b_path),
                    not_exists=d.change_type is ChangeType.D,
                ),
            ),
            change_type=ChangeType[cast(str, d.change_type)],
        )
        for d in base.diff(
            other=remote, paths=list(paths) or list(repo_root_dir.rglob("*.ipynb"))
        )
    ]

get_repo(path)

Find git repo in current or parent directories.

Source code in databooks/git_utils.py
120
121
122
123
124
125
126
127
128
def get_repo(path: Path) -> Optional[Repo]:
    """Find git repo in current or parent directories."""
    try:
        repo = Repo(path=path, search_parent_directories=True)
    except (InvalidGitRepositoryError, NoSuchPathError):
        logger.debug(f"No repo found at {path}.")
        return None
    logger.debug(f"Repo found at: {repo.working_dir}.")
    return repo