Compare commits
2 Commits
edae32dc86
...
671fa4b6b1
| Author | SHA1 | Date | |
|---|---|---|---|
| 671fa4b6b1 | |||
| 63dcbeb9e7 |
22
how_to/Change_23.md
Normal file
22
how_to/Change_23.md
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
- tag: Resolve name to oid in argparse
|
||||||
|
|
||||||
|
It's nice that we can create tags, but now let's actually make them usable from
|
||||||
|
the CLI.
|
||||||
|
|
||||||
|
In *base.py*, we'll create `get_oid` to resolve a "name" to an OID. A name can
|
||||||
|
either be a ref (in which case `get_oid` will return the OID that the ref points
|
||||||
|
to) or an OID (in which case `get_oid` will just return that same OID).
|
||||||
|
|
||||||
|
Next, we'll modify the argument parser in *cli.py* to call `get_oid` on all
|
||||||
|
arguments which are expected to be an OID. This way we can pass a ref there
|
||||||
|
instead of an OID.
|
||||||
|
|
||||||
|
At this point we can do something like:
|
||||||
|
```
|
||||||
|
$ ugit tag mytag d8d43b0e3a21df0c845e185d08be8e4028787069
|
||||||
|
$ ugit log refs/tags/mytag
|
||||||
|
# Will print log of commits starting at d8d43b0e...
|
||||||
|
$ ugit checkout refs/tags/mytag
|
||||||
|
# Will checkout commit d8d43b0e...
|
||||||
|
etc...
|
||||||
|
```
|
||||||
@@ -96,9 +96,11 @@ def commit(message):
|
|||||||
|
|
||||||
return oid
|
return oid
|
||||||
|
|
||||||
|
|
||||||
def create_tag(name, oid):
|
def create_tag(name, oid):
|
||||||
data.update_ref(f"refs/tags/{name}", oid)
|
data.update_ref(f"refs/tags/{name}", oid)
|
||||||
|
|
||||||
|
|
||||||
def checkout(oid):
|
def checkout(oid):
|
||||||
commit = get_commit(oid)
|
commit = get_commit(oid)
|
||||||
read_tree(commit.tree)
|
read_tree(commit.tree)
|
||||||
@@ -126,5 +128,9 @@ def get_commit(oid):
|
|||||||
return Commit(tree=tree, parent=parent, message=message)
|
return Commit(tree=tree, parent=parent, message=message)
|
||||||
|
|
||||||
|
|
||||||
|
def get_oid(name):
|
||||||
|
return data.get_ref(name) or name
|
||||||
|
|
||||||
|
|
||||||
def is_ignored(path):
|
def is_ignored(path):
|
||||||
return ".ugit" in path.split("/")
|
return ".ugit" in path.split("/")
|
||||||
|
|||||||
12
ugit/cli.py
12
ugit/cli.py
@@ -19,6 +19,8 @@ def parse_args():
|
|||||||
commands = parser.add_subparsers(dest="command")
|
commands = parser.add_subparsers(dest="command")
|
||||||
commands.required = True
|
commands.required = True
|
||||||
|
|
||||||
|
oid = base.get_oid
|
||||||
|
|
||||||
init_parser = commands.add_parser("init")
|
init_parser = commands.add_parser("init")
|
||||||
init_parser.set_defaults(func=init)
|
init_parser.set_defaults(func=init)
|
||||||
|
|
||||||
@@ -28,14 +30,14 @@ def parse_args():
|
|||||||
|
|
||||||
cat_file_parser = commands.add_parser("cat-file")
|
cat_file_parser = commands.add_parser("cat-file")
|
||||||
cat_file_parser.set_defaults(func=cat_file)
|
cat_file_parser.set_defaults(func=cat_file)
|
||||||
cat_file_parser.add_argument("object")
|
cat_file_parser.add_argument("object", type=oid)
|
||||||
|
|
||||||
write_tree_parser = commands.add_parser("write-tree")
|
write_tree_parser = commands.add_parser("write-tree")
|
||||||
write_tree_parser.set_defaults(func=write_tree)
|
write_tree_parser.set_defaults(func=write_tree)
|
||||||
|
|
||||||
read_tree_parser = commands.add_parser("read-tree")
|
read_tree_parser = commands.add_parser("read-tree")
|
||||||
read_tree_parser.set_defaults(func=read_tree)
|
read_tree_parser.set_defaults(func=read_tree)
|
||||||
read_tree_parser.add_argument("tree")
|
read_tree_parser.add_argument("tree", type=oid)
|
||||||
|
|
||||||
commit_parser = commands.add_parser("commit")
|
commit_parser = commands.add_parser("commit")
|
||||||
commit_parser.set_defaults(func=commit)
|
commit_parser.set_defaults(func=commit)
|
||||||
@@ -43,16 +45,16 @@ def parse_args():
|
|||||||
|
|
||||||
log_parser = commands.add_parser("log")
|
log_parser = commands.add_parser("log")
|
||||||
log_parser.set_defaults(func=log)
|
log_parser.set_defaults(func=log)
|
||||||
log_parser.add_argument("oid", nargs="?")
|
log_parser.add_argument("oid", type=oid, nargs="?")
|
||||||
|
|
||||||
checkout_parser = commands.add_parser("checkout")
|
checkout_parser = commands.add_parser("checkout")
|
||||||
checkout_parser.set_defaults(func=checkout)
|
checkout_parser.set_defaults(func=checkout)
|
||||||
checkout_parser.add_argument("oid")
|
checkout_parser.add_argument("oid", type=oid)
|
||||||
|
|
||||||
tag_parser = commands.add_parser("tag")
|
tag_parser = commands.add_parser("tag")
|
||||||
tag_parser.set_defaults(func=tag)
|
tag_parser.set_defaults(func=tag)
|
||||||
tag_parser.add_argument("name")
|
tag_parser.add_argument("name")
|
||||||
tag_parser.add_argument("oid", nargs="?")
|
tag_parser.add_argument("oid", type=oid, nargs="?")
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user