diff --git a/libtft.py b/libtft.py index 18986d0..5b26497 100644 --- a/libtft.py +++ b/libtft.py @@ -380,3 +380,44 @@ def cmd_hash_object(args): with open(args.path, "rb") as fd: sha = object_hash(fd, args.type.encode(), repo) print(sha) + +argsp = argsubparsers.add_parser("add", help = "Add files contents to the index.") +argsp.add_argument("path", nargs="+", help="Files to add") + +def cmd_add(args): + repo = repo_find() + add(repo, args.path) + +def add(repo, paths, delete=True, skip_missing=False): + rm(repo, paths, delete=False, skip_missing=True) + worktree = repo.worktree + os.sep + + clean_paths = list() + for path in paths: + abspath = os.path.abspath(path) + if not (abspath.startswith(worktree) and os.path.isfile(abspath)): + raise Exception("Not a file, or outside the worktree: {}".format(paths)) + relpath = os.path.relpath(abspath, repo.worktree) + clean_paths.append((abspath, relpath)) + + index = index_read(repo) + + for (abspath, relpath) in clean_paths: + with open(abspath, "rb") as fd: + sha = object_hash(fd, b"blob", repo) + + stat = os.stat(abspath) # to get metadata of files + + ctime_s = int(stat.st_ctime) + ctime_ns = stat.st_ctime_ns % 10**9 + mtime_s = int(stat.st_mtime) + mtime_ns = stat.st_mtime_ns % 10**9 + + entry = GitIndexEntry(ctime=(ctime_s, ctime_ns), mtime=(mtime_s, mtime_ns), dev=stat.st_dev, ino=stat.st_ino, + mode_type=0b1000, mode_perms=0o644, uid=stat.st_uid, gid=stat.st_gid, + fsize=stat.st_size, sha=sha, flag_assume_valid=False, + flag_stage=False, name=relpath) + index.entries.append(entry) + + index_write(repo, index) + \ No newline at end of file