やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

GitHubアップローダで使うコマンドをGitPythonで使えるか試してみた

使えないコマンドがあった。

成果物

GitHubGitPython.201706221222

GitHubアップローダで使用しているコマンド

  • git init
  • git config --local user.name {0}
  • git config --local user.email {0}
  • git remote add origin git@{0}:{1}/{2}.git
  • git add -n .
  • git add .
  • git commit -m {0}
  • git push origin master

GitPythonに存在するコマンド

GitPythonに存在しないコマンド

  • git add -n .
  • git config --local
    • git config --local user.name {0}
    • git config --local user.email {0}

git add -n .に該当するAPIがない

$ git add -n .
add '02_add.py'
import git
git.Repo.init()
my_repo = git.repo.base.Repo(path=os.path.abspath(os.path.dirname(__file__)))

print(my_repo.untracked_files) # github管理されていないファイル一覧と思われる
my_repo.git.add(n=True, A=True) # https://github.com/gitpython-developers/GitPython/issues/292
  • untracked_filesはgit未管理ファイル一覧である。add対象ファイル一覧ではない
  • git.add(n=True, A=True)ではgit add -n .に近い動作をする
    • addはされず表示だけされる
      • ただし表示形式が異なる
        • add '02_add.py'の形式で欲しい

望む形式

add '02_add.py'
add '02_add.py'
add '02_add.py'
add '...'

もしくはuntracked_filesのようにlist型データでも良かった。

返される形式

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   00_init.py
    new file:   01_repoObj.py
    new file:   02_add.py
    new file:   memo.md
    new file:   memo_git_command.md

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   02_add.py

これをテキスト解析せねばならないのであれば、自前でgit add -n .を叩いた方がマシ。わざわざGitPythonを使う価値なし。

見つからない

StackOverflowでも「できる」という回答がなかった。無理やり自力実装しかないっぽい。

https://stackoverflow.com/questions/31540449/how-to-check-if-a-git-repo-has-uncommitted-changes-using-python

git config --localに該当するAPIがない

http://gitpython.readthedocs.io/en/stable/reference.html?highlight=config#git.config.GitConfigParser

import git
import os.path
my_repo = git.repo.base.Repo(path=os.path.abspath(os.path.dirname(__file__)))
print(my_repo.config_reader())
print(dir(git.config.GitConfigParser))
print(my_repo.config_reader().read())
<git.config.GitConfigParser object at 0xb6b40d8c>
['BOOLEAN_STATES', 'NONSPACECRE', 'OPTCRE', 'OPTCRE_NV', 'OPTVALUEONLY', 'SECTCRE', '_DEFAULT_INTERPOLATION', '_MutableMapping__marker', '_OPT_NV_TMPL', '_OPT_TMPL', '_SECT_TMPL', '__abstractmethods__', '__class__', '__contains__', '__del__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_acquire_lock', '_assure_writable', '_convert_to_boolean', '_get', '_get_conv', '_handle_error', '_has_includes', '_join_multiline_values', '_mutating_methods_', '_read', '_unify_values', '_validate_value_types', '_value_to_string', '_write', '_write_section', 'add_section', 'clear', 'converters', 'defaults', 'get', 'get_value', 'getboolean', 'getfloat', 'getint', 'has_option', 'has_section', 'items', 'keys', 'options', 'optionxform', 'pop', 'popitem', 're_comment', 'read', 'read_dict', 'read_file', 'read_only', 'read_string', 'readfp', 'release', 'remove_option', 'remove_section', 'rename_section', 'sections', 'set', 'set_value', 'setdefault', 't_lock', 'update', 'values', 'write']
  • my_repo.config_reader()
  • my_repo.config_writer()

どうやって使うのか分からない。

所感

GitPythonを使う気が失せた。わざわざ置き換える手間をかけても、結局は素のgitコマンドを叩く必要があるのだから。

gitコマンドでのエラー確認はあきらめるか。