Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
In [2]:
!pip3 install pygit2
In [3]:
import pygit2
In [4]:
pygit2.InvalidSpecError
Out[4]:
Clone a Repository¶
In [22]:
url = "https://github.com/dclong/docker-ubuntu_b.git"
dir_local = "/tmp/test_pygit2"
!ls {dir_local}
In [25]:
!rm -rf {dir_local}
!ls {dir_local}
In [29]:
repo = pygit2.clone_repository(url, dir_local)
In [30]:
!ls {dir_local}
In [26]:
url2 = "https://github.com/dclong/docker-ubuntu_b"
dir_local2 = "/tmp/test_pygit2_2"
In [27]:
pygit2.clone_repository(url, dir_local2)
Out[27]:
In [28]:
ls /tmp/test_pygit2_2/
Methods of pygit2.Repository¶
In [28]:
[m for m in dir(repo) if not m.startswith("_")]
Out[28]:
Branches¶
In [31]:
repo.listall_branches()
Out[31]:
In [104]:
set(repo.branches)
Out[104]:
In [103]:
repo.branches
Out[103]:
In [106]:
[m for m in dir(repo.branches) if not m.startswith("_")]
Out[106]:
In [110]:
repo.branches.get("non_exist_branch") is None
Out[110]:
In [112]:
br = repo.branches.get("dev")
br
Out[112]:
In [50]:
repo.branches.get("origin/main")
Out[50]:
Create a Branch¶
- Unlike the
--force
parameter ofgit checkout -b
,repo.create_branch(..., force=True)
does not throw away changes in the current directory. Notice that it does overwrite an existing branch.
In [129]:
repo.create_branch(
"centos7", repo.references["refs/remotes/origin/centos7"].peel(), True
)
Out[129]:
References¶
In [51]:
repo.references
Out[51]:
In [141]:
repo.listall_references()
Out[141]:
In [142]:
set(repo.references)
Out[142]:
In [72]:
repo.lookup_reference("centos7")
In [145]:
repo.lookup_reference("refs/remotes/origin/centos7")
Out[145]:
In [146]:
repo.references.get("refs/remotes/origin/centos7")
Out[146]:
Checkout a Reference¶
In [36]:
!git -C {dir_local} status
In [71]:
repo.checkout("nima")
In [77]:
repo.checkout("refs/heads/dev")
In [74]:
repo.checkout("refs/heads/main")
In [75]:
!git -C {dir_local} status
In [53]:
repo.checkout("refs/remotes/origin/main")
In [54]:
!git -C {dir_local} status
In [55]:
!git -C {dir_local} branch
In [135]:
repo.listall_references()
Out[135]:
In [136]:
!git -C {dir_local} branch
In [138]:
repo.checkout(repo.references["refs/heads/centos7"])
In [139]:
!git -C {dir_local} branch
Status of the Repository¶
In [78]:
repo.status()
Out[78]:
Stash Changes¶
In [86]:
repo.default_signature
Out[86]:
In [87]:
repo.stash(repo.default_signature)
Create a Commit¶
In [27]:
?repo.create_commit
Index¶
In [150]:
index = repo.index
index
Out[150]:
In [158]:
[m for m in dir(index) if not m.startswith("_")]
Out[158]:
git reset
/ Repository.reset
¶
Reset current HEAD to the specified state.
In [37]:
!git -C {dir_local} status
In [180]:
repo.reset(repo.head.peel().oid, pygit2.GIT_RESET_HARD)
In [38]:
repo.reset(repo.head.peel().id, pygit2.GIT_RESET_HARD)
In [39]:
!git -C {dir_local} status
git diff / Repository.diff¶
In [13]:
repo.listall_references()
Out[13]:
In [15]:
diff = repo.diff("refs/heads/dev", "refs/heads/main")
diff
Out[15]:
In [36]:
not any(True for _ in diff.deltas)
Out[36]:
In [38]:
not any(True for _ in repo.diff("refs/heads/main", "refs/heads/dev").deltas)
Out[38]:
In [40]:
diff = repo.diff("refs/heads/dev", "refs/heads/centos7")
diff
Out[40]:
In [43]:
deltas = list(diff.deltas)
deltas
Out[43]:
In [59]:
deltas[0].old_file.path
Out[59]:
In [60]:
deltas[0].new_file.path
Out[60]:
In [61]:
deltas[1].old_file.path
Out[61]:
In [62]:
deltas[1].new_file.path
Out[62]: