Compare commits

...

2 Commits

Author SHA1 Message Date
41333f06bc pass HEAD by default to argparse 2024-05-05 21:04:28 +02:00
fe292c02c9 Add change 25 instructions 2024-05-05 21:04:02 +02:00
3 changed files with 19 additions and 5 deletions

12
how_to/Change_25.md Normal file
View File

@@ -0,0 +1,12 @@
- cli: pass HEAD by default in argparse
First, make "@" be an alias for HEAD. (Implemented in `get_oid`)
Second, do a little refactoring in *cli.py*. Some commands accept an optional
OID argument and if the argument isn't provided it defaults to HEAD. For example
`git log` can get an OID to start logging from, but by default it logs all
commits before HEAD.
Instead of having each command implement this logic, let's just make "@" (HEAD)
be the default value for those commands. The relevant commands at this stage
are `log` and `tag`. More will follow.

View File

@@ -130,6 +130,9 @@ def get_commit(oid):
def get_oid(name):
if name == "@":
name = "HEAD"
# Name is ref
refs_to_try = [
f"{name}",

View File

@@ -45,7 +45,7 @@ def parse_args():
log_parser = commands.add_parser("log")
log_parser.set_defaults(func=log)
log_parser.add_argument("oid", type=oid, nargs="?")
log_parser.add_argument("oid", default="@", type=oid, nargs="?")
checkout_parser = commands.add_parser("checkout")
checkout_parser.set_defaults(func=checkout)
@@ -54,7 +54,7 @@ def parse_args():
tag_parser = commands.add_parser("tag")
tag_parser.set_defaults(func=tag)
tag_parser.add_argument("name")
tag_parser.add_argument("oid", type=oid, nargs="?")
tag_parser.add_argument("oid", default="@", type=oid, nargs="?")
return parser.parse_args()
@@ -87,7 +87,7 @@ def commit(args):
def log(args):
oid = args.oid or data.get_ref("HEAD")
oid = args.oid
while oid:
commit = base.get_commit(oid)
@@ -103,5 +103,4 @@ def checkout(args):
def tag(args):
oid = args.oid or data.get_ref("HEAD")
base.create_tag(args.name, oid)
base.create_tag(args.name, args.oid)