20200628のGitに関する記事は6件です。

【2020年度版】税金と手取りの計算を全部Pythonにやらせてみる

概要

税金の計算って難しいですよね。
社会人になると毎年なんとなく税金やら保険料やらが引かれてますが、僕は税金について全然詳しくないのでなにがなんやらという状態です。
さらに聞いた話だと今年から控除やら所得税やらのルールが変わるみたいです。
退屈なことは全部Pythonにやらせよう」とう格言(?)があるので、今回は勉強を兼ねてPythonで税金計算用のクラスを作ってみようと思います。
ゼロからいろいろ調べながら書いたので知ってる人にとっては当たり前の説明が多いかもしれません。

Github

Githubに税金計算用のクラスを作ってあげました。

コードを書くにあたって参考にしたサイト

使い方

税金の難しい話をする前に、まずは簡単に使い方を説明しようと思います。
今回作ったTaxクラスはtax.pyの中で実装されています。
例として年収500万の独身男性の場合を考えます。
親のディレクトリにtax.pyがある状態で以下を実行します。

# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 親ディレクトリのファイルをインポートするための設定
from common.tax import Tax

gross_salary = 5000000
tax = Tax(gross_salary)
print("gross_salary, income = ", tax.gross_salary, tax.income)
print("income_tax, inhabitant_tax, insurance_premiun = ", tax.income_tax(), tax.inhabitant_tax(), tax.social_insurance_premium())
print("net_salary = ", tax.net_salary())
print("max_hurusato_donation = ", tax.max_hurusato_donation())
出力
gross_salary, income =  5000000 3560000.0
income_tax, inhabitant_tax, insurance_premiun =  110575.0 215575.0 619249.9999999999
net_salary =  4054600.0
max_hurusato_donation =  56035.593432760994
  • gross_salary : 収入
  • income : 給与所得
  • inhabitant_tax : 住民税
  • income_tax : 所得税
  • insurance_premium : 社会保険料
  • net_salary : 手取り
  • max_hurusato_donation : ふるさと納税で自己負担2000円で全額控除できる上限

です。このケースだと額面が500万円、税金や保険料などでいろいろ引かれて手取りがだいたい400万円くらいってことです。
上の例だとtaxの引数は収入(=gross_salary)しか与えていませんが、扶養家族がいたり障害者である場合は控除されるので、引数を指定することでそういったケースも計算可能です。
では、個々について順番に説明していきます。

給与所得

まずは給与所得についてです。僕は税金に関して無知だったので、「なんとなく収入から税金が引かれて手取りが減る」ことまでは理解していたものの、給与所得とはなんぞ?という状態でした。
実は諸々の税金は額面の収入から直接計算されるわけではなく、給与所得控除という控除が行われた後の金額に適用されます。
今回は会社員を前提で話していますが、本来仕事をするときには必要経費が発生するので

(収入) - (経費) = (所得) 

の図式が成り立ちます。雇われの身である会社員の場合はあまりピンとこないかもしれませんが、働いていれば経費にあたる出費があるだろうという前提で、すべての会社員が収入から引かれます。
ちなみにこれは源泉徴収表にも会社からの額面の収入のとなりに給与所得控除後の金額という名前で控除後の金額が記載されています↓

スクリーンショット 2020-06-28 15.52.26.png

Pythonのコードは以下になります。
2020年以降はルールが変わってこの給与所得控除が10万円減り、のちに出てくる所得税が10万円増えました。
なのでプラマイゼロで引かれる金額は変わらないのですが、年収850万円以上の人は今回の改正で払う金額が多くなっています。
僕は年収850万もないですが、個人的には頑張って年収が増えても所得税でいっぱい引かれると思うとモチベーションがいまいち上がらないのでこの改正は別に嬉しくはないですね。。
こうして計算した給与所得控除後の金額をincomeとして返します。
このincomeの値は他のクラス関数でもよく使うので、コンストラクタの中でself.incomeに代入しています。

Tax.income()
def income(self):
    """
    給与所得控除の計算, 収入を引数として控除額を返す
    2020年以降は控除が10万減額し、所得税で10万増えたので実質変わらない
    ただし、850万以上は給与所得控除の額が減ったで実質的には増税
    """
    employment_income_deduction = 0

    if self.gross_salary < 550000:
        employment_income_deduction = self.gross_salary

    elif self.gross_salary <1800000:
        employment_income_deduction = self.gross_salary * 0.4 - 100000

    elif self.gross_salary <3600000:
        employment_income_deduction = self.gross_salary*0.3 + 80000

    elif self.gross_salary <6600000:
        employment_income_deduction = self.gross_salary*0.2 + 440000

    elif self.gross_salary <8500000:
        employment_income_deduction = self.gross_salary*0.1 + 1100000

    else:
        employment_income_deduction = 1950000

    income = self.gross_salary - employment_income_deduction
    return income

社会保険料

次に社会保険料について見ていきます。
これは税金とは別枠の厚生年金とか雇用保険とかそのへんです。
計算式は以下に書いていますが、健康保険料率免除保険料率雇用保険料率なるものが出てきますね。
これらから計算した保険料率を収入にかけたものが最終的な保険料になります。
健康保険料率は地域によって違いますが、東京の場合は9.87%なので、
self.health_insurance_premium_rateにはデフォルトで0.0987が入っています。
免除保険料率は厚生年金がどれくらい免除されるかを決めるものです。
免除保険料率は年齢と共に上がる傾向がありますが、平均で0.4%くらいなのでself.rebate_contribution_rateはデフォルトで0.04です。
これらの値はコンストラクタで個別に設定可能です。
これらの値が2で割られているのは会社と半分ずつ負担しているためです。
雇用保険料率は一律0.3%なので、それらを足し合わせて保険料率を計算します。

Tax.social_insurance_preium()
def social_insurance_premium(self):
    """
    保険料の計算
    保険料率 = 健康保険料率/2
    + (厚生年金保険料率18.3%-免除保険料率)/2
    + 雇用保険料率0.3%
    """
    # 保険料率
    insurance_premium_rate = (self.health_insurance_premium_rate)/2 \
                             + (0.183 - self.rebate_contribution_rate)/2 \
                             + 0.003

    # 社会保険料 = 年収 x 保険料率
    social_insurance_premium = self.gross_salary*insurance_premium_rate
    return social_insurance_premium

所得税

次に所得税です。
所得税income()で計算した所得からまず基礎控除を引きます。
基礎控除は今までは一律38万円でしたが、2020年からは48万円に引き上げられました。
(上でも述べたようにその分給与所得控除が10万円引き下げられました。)
ただし、今回から新しく加わったルールとして年収2400万を超えると段階的に基礎控除の額が減り、2500万円を越えるとゼロになります。
さらにそこから扶養控除(dependents_deduction)、障害者控除(handicapped_deduction)が引かれます。
コンストラクタで特に指定しない場合はdependents_deductionもhandicapped_deductionもゼロに設定されています。

こうして諸々の控除された後の所得をtarget_of_income_taxとし、ここから所得税を計算します。
年収が高いほどたくさん取られる仕組みになっています。
なお、ここで出てくる所得税率self.income_tax_rate)はふるさと納税をする場合の計算にも使います。

Tax.income_tax()
def income_tax(self):
    """
    所得税の計算
    """

    # 所得税における扶養控除
    dependents_deduction = self.high_school_student * 480000 + self.college_student * 630000
    # 所得税における障害者控除
    handicapped_deduction = self.handicapped * 260000
    # 基礎控除、2019年までは一律38万円だったが、2020年からは48万円に。ただし2000万を超えると段階的に減る
    basic_deduction = 0
    if self.income < 24000000:
        basic_deduction = 480000
    elif self.income < 24500000:
        basic_deduction = 320000
    elif self.income < 25000000:
        basic_deduction = 160000

    # 所得税の対象となる金額、所得から控除や保険料を引いたもの
    target_of_income_tax = (self.income \
                            - self.social_insurance_premium() \
                            - self.spousal_deduction() \
                            - dependents_deduction \
                            - handicapped_deduction \
                            - basic_deduction)
    # 年収別の所得税率と控除額
    if target_of_income_tax < 1950000:
        self.income_tax_rate = 0.05
        deduction = 0

    elif target_of_income_tax < 3300000:
        self.income_tax_rate = 0.1
        deduction = 97500

    elif target_of_income_tax < 6950000:
        self.income_tax_rate = 0.2
        deduction = 427500

    elif target_of_income_tax < 9000000:
        self.income_tax_rate = 0.23
        deduction = 636000

    elif target_of_income_tax < 18000000:
        self.income_tax_rate = 0.33
        deduction = 1536000

    elif target_of_income_tax < 40000000:
        self.income_tax_rate = 0.40
        deduction = 2796000

    else:
        self.income_tax_rate = 0.45
        deduction = 479.6

    # 所得税の計算
    income_tax = target_of_income_tax * self.income_tax_rate - deduction
    # 所得税がマイナスにになった場合はゼロにする
    if income_tax <= 0:
        income_tax = 0
    return income_tax

住民税

住民税の計算は所得税と似ているので、所得税の仕組みがわかればそれほど難しくないと思います。
所得税とは控除の額が違う他、前年度の収入に対して税金がかかる点でも違います。
(プログラム上では前年度、今年度の区別は特にしていません)

Tax.inhabitant_tax()
def inhabitant_tax(self):
    """
    住民税の計算, 課税所得を引数に住民税を計算する
    """

    # 住民税における扶養控除
    dependents_deduction = self.high_school_student * 330000 + self.college_student * 450000
    # 住民税における障害者控除
    handicapped_deduction = self.handicapped * 270000
    # 基礎控除、所得税と同様2020年から変更
    basic_deduction = 0
    if self.income < 24000000:
        basic_deduction = 430000
    elif self.income < 24500000:
        basic_deduction = 190000
    elif self.income < 25000000:
        basic_deduction = 150000

    # 所得から各種控除、基礎控除(43万円)を引き、税率10%をかける
    # さらに均等割5000円を足して、調整控除2500円を引く
    inhabitant_tax = (self.income
                      - self.social_insurance_premium()
                      - self.spousal_deduction()
                      - dependents_deduction
                      - handicapped_deduction
                      - basic_deduction) * 0.1 + 5000 - 2500

    # 計算した住民税がマイナスになった場合はゼロとする
    if inhabitant_tax <=0:
        inhabitant_tax = 0
    return inhabitant_tax

手取り

ここまでくれば手取りを計算することができます。
額面の収入から今まで計算してきた社会保険料、所得税、住民税を引きます。

Tax.net_salary()
def net_salary(self):
    """
    手取りの計算、収入から所得税、住民税、社会保険料を引く
    """
    total_tax = self.inhabitant_tax() + self.income_tax()
    net_salary = self.gross_salary - total_tax - self.social_insurance_premium()
    return net_salary

ふるさと納税

今まで出てきたものとは少し毛色が違いますが、ふるさと納税についても実装しました。
ふるさと納税は地方に寄付することで返礼品を受け取れるというシステムです。
その際に寄付に応じて所得税、住民税を減税することができます。
上限額までは寄付した金額のうち2000円のみが自己負担で、残りの金額すべてを控除にあてることができます。
以下の関数では控除に全額あてることができる寄付の金額の上限を計算しています。
計算式はコメントやコードを参考にしてください。

Tax.max_hurusato_donation()
    def max_hurusato_donation(self):
        """
        ふるさと納税で自己負担2000円で全額控除される上限の計算
        言い換えるとreturnの金額から2000円引いたものが所得税および住民税から控除される
        """
        # 住民税所得割額(=住民税)から計算する
        # ふるさと納税控除額の上限
        hurusato_deduction = self.inhabitant_tax() * 0.2

        # (控除金額) =(寄附金額-2000)×(90%-所得税の税率×1.021)
        # (寄付金額) = (控除金額)/(90%-所得税の税率×1.021)+2000
        max_hurusato_donation = hurusato_deduction / (0.9 - self.income_tax_rate * 1.021) + 2000
        return max_hurusato_donation

可視化

これだけだとPythonで書く意味ある?と言われてしまいそうなので、matplotlibを使って可視化してみます。
まずは必要なライブラリをimportします。

Tax.show_ipynb
# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 親ディレクトリのファイルをインポートするための設定
from common.tax import Tax
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
%matplotlib inline
pd.options.display.precision = 0
plt.rcParams['font.family'] = 'AppleGothic'

年収200-1200万円まで100万円刻みで計算し、listに突っ込みます。

Tax.show_ipynb2
# 額面年収、200〜1,200万まで1万刻みで
gross_salaries = [i for i in range(2000000,12010000, 1000000)]
# 所得
incomes = []
# 税金
total_taxes = []
# 住民税
inhabitant_taxes = []
# 所得税
income_taxes = []
#社会保険料(=社会保険料控除)
social_insurance_premiums = []
# 手取り年収
net_salaries = []
# ふるさと納税控除上限とそのときの寄付額
max_hurusato_donation = []

for gross_salary in gross_salaries:
    tax = Tax(gross_salary)

    # リストに値を追加
    incomes.append(tax.income)
    total_taxes.append(tax.income_tax() + tax.inhabitant_tax())
    inhabitant_taxes.append(tax.inhabitant_tax())
    income_taxes.append(tax.income_tax())
    social_insurance_premiums.append(tax.social_insurance_premium())
    net_salaries.append(tax.net_salary())
    max_hurusato_donation.append(tax.max_hurusato_donation() - 2000) #自己負担2000円を引く

dataframeに格納します。df.head()とかで中身を見れます。

Tax.show_ipynb3
df = pd.DataFrame()
df['年収'] = gross_salaries
df['手取り'] = net_salaries
df['税金'] = total_taxes
df['住民税'] = inhabitant_taxes
df['所得税'] = income_taxes
df['社会保険料'] = social_insurance_premiums
df['ふるさと納税控除上限'] = max_hurusato_donation

棒グラフで可視化します。

Tax.show_ipynb4
df.index = ['200', '300', '400', '500', '600', '700',  '800', '900', '1000', '1100', '1200']
plt.figure(figsize=(12,7))
plt.rcParams["font.size"] = 14
plt.grid(axis='y')
yticks = [0, 1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000, 11000000, 12000000]
plt.ylim(0, 12500000)
plt.yticks(yticks)
plt.bar(df.index, df['手取り'])
plt.bar(df.index, df['税金'], bottom= df['手取り'])
plt.bar(df.index, df['ふるさと納税控除上限'], bottom= df['手取り'] + df['税金'] - df['ふるさと納税控除上限'])
plt.bar(df.index, df['社会保険料'], bottom= df['手取り'] + df['税金'])
plt.xlabel('Salary (yen*10000)')
plt.ylabel('Breakdown (yen)')
plt.title('')
plt.legend(['net-salary', 'tax','hurusato_deduction', 'insurance'])
plt.show()

ダウンロード (1).png

グラフの説明

  • 横軸が年収、縦軸のうち青が手取り、(オレンジ+緑)が税金で、緑がそのうちふるさと納税で控除できる上限、赤が社会保険料

グラフを見た僕の感想

  • 年収800万円くらいまでは税金よりも社会保険料の方が多いが、それ以上だと税金の方が多くなってくる
  • 今の日本が高額納税者(=高収入の人)に支えられていることがわかる
  • 年収1000万円でも手取り700万ちょっとしかもらえないのか...
  • ふるさと納税はやった方が良い

まとめ

  • Pythonを使って税金や手取りを計算するクラスを作り、可視化してみた
  • 計算や解釈の誤りがあれば随時直す予定なので、もし見つけた方がいたら教えてもらえると嬉しいです
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Git】ワークツリーには残しておきたいけど、リポジトリからは削除したい

実際に遭遇したことはないのですが、試しにpassをベタ書きしたファイルを謝ってcommitした時などの対処法

git rm --cached <ファイル名>

こうすることで自分の手元にはファイルを残して、リポジトリからはファイルを消すことができる。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Git】git add ./git add -a / git add -u/をしっかり。

 git add .

カレントディレクトリ以下で変更があったファイルをaddする。つまり、下記全てがaddされる

  • 変更ファイル
  • 新ファイル
  • 削除されたファイル

全がてステージにあげられる(addされる)

 git add -u (updateの略)

バージョン管理されていて変更されたファイルがaddされる。つまり、下記がaddされる。

  • 変更ファイル
  • 削除ファイル

新規ファイルはaddされない。バージョン管理されていないから。

 git add -A (allの略)

変更されたファイル全てがaddされる。つまり、

  • 変更ファイル
  • 新ファイル
  • 削除されたファイル

 git add .とgit add -Aの違い

git add .は現在いるディレクトリ(カレントディレクトリ)以下だけaddされる。
git add -Aはレポジトリ全てが処理される。(この点はgit add -uと同様。)

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

これくらいは知っておかないとまずいGitコマンド

1.目的

転職してから初めてGitの存在を知り、超基礎レベルのコマンドは打てるようになりました。

ただ、たまに忘れるので、自分用にまとめておくことが目的。
これくらいはさすがにできないとまずいよね、という観点。

使うのはGitHubと、GitBash。

2.よく使うコマンド

(1)事前準備

(ⅰ)GitのDL

下記はwindows用
https://gitforwindows.org/

(ⅱ)GitHubへの登録

https://github.com/

(ⅲ)GitHubへの登録その2

Git Bashから下記コマンドで、GitHubへ登録したusernameとメールアドレスを登録する

$git config --global user.name"××××"
$git config --global user.email"△△△△"

(2)コマンド集

基本的に時系列に沿って書いていきます。

●今いるディレクトリを確認

$ pwd

●ディレクトリの変更

$ cd 変更したい場所

●今いるディレクトリ内にあるファイルを確認

$ ls

隠れファイルといった全てのファイルを確認するには下記。

$ ls -a

●gitファイルをディレクトリ内に作成

$ git init

●add

$ git add ファイル名

ディレクトリ内のすべてのファイルをaddする場合は下記

$ git add .

●commit

$ git commit

※補足※
Git Bashでcommitメッセージを入力する際は、まずiを押して挿入モード
→commitメッセージを書いたら、Escキー→「:wq」→Enterを押すと画面を閉じれる

●(コマンドではない)リポジトリの登録

GitHub上でリポジトリを作っておく

●GitHubへのpush

まずはGitHubで作ったリポジトリをGitBash上で登録する。

$ git remote add origin https://github.com/●●(username)/リポジトリ名

GitHubへpushする

$ git push -u origin master

●接続先のリポジトリを変更する場合

現在の接続先を確認

$ git remote -v

接続先を変更

$ git remote set-url origin ●●●(接続先)

以上。
ぼちぼち追記していきます!

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

ghqで対応しているVCSのソースを取得する

概要

ghq がサポートしている各VCS(Version Control Systems)での動作を見てみようと考えた。VCS自体のソースはセルフホストしてるはずなので、それらをサンプルとして取得、更新の動作を確認する。

確認環境:

yoichinakayama@penguin:~$ uname -a
Linux penguin 5.4.40-04224-g891a6cce2d44 #1 SMP PREEMPT Tue Jun 23 20:13:49 PDT 2020 aarch64 GNU/Linux
yoichinakayama@penguin:~$ ghq --version
ghq version 1.1.3 (rev:HEAD)

Git

Kernel.org git repositories からgit.gitのリンク先の https://git.kernel.org/pub/scm/git/git.git/ を指定する。

get

yoichinakayama@penguin:~$ ghq get https://git.kernel.org/pub/scm/git/git.git/
     clone https://git.kernel.org/pub/scm/git/git.git/ -> /home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git
       git clone --recursive https://git.kernel.org/pub/scm/git/git.git/ /home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git.git
Cloning into '/home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git.git'...
remote: Enumerating objects: 12656, done.
remote: Counting objects: 100% (12656/12656), done.
remote: Compressing objects: 100% (867/867), done.
remote: Total 288989 (delta 12128), reused 12011 (delta 11788), pack-reused 276333
Receiving objects: 100% (288989/288989), 66.96 MiB | 1.18 MiB/s, done.
Resolving deltas: 100% (218191/218191), done.
Checking out files: 100% (3777/3777), done.
Submodule 'sha1collisiondetection' (https://github.com/cr-marcstevens/sha1collisiondetection.git) registered for path 'sha1collisiondetection'
Cloning into '/home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git.git/sha1collisiondetection'...
remote: Enumerating objects: 6, done.        
remote: Counting objects: 100% (6/6), done.        
remote: Compressing objects: 100% (6/6), done.        
remote: Total 887 (delta 0), reused 4 (delta 0), pack-reused 881        
Receiving objects: 100% (887/887), 611.42 KiB | 317.00 KiB/s, done.
Resolving deltas: 100% (564/564), done.
Submodule path 'sha1collisiondetection': checked out '855827c583bc30645ba427885caa40c5b81764d2'

list

yoichinakayama@penguin:~$ ghq list|grep git.kernel.org
git.kernel.org/pub/scm/git/git.git

update

yoichinakayama@penguin:~$ ghq list|grep git.kernel.org|ghq get -u
     clone https://git.kernel.org/pub/scm/git/git.git -> /home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git
       git clone --recursive https://git.kernel.org/pub/scm/git/git.git /home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git
Cloning into '/home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git'...
remote: Enumerating objects: 12656, done.
remote: Counting objects: 100% (12656/12656), done.
remote: Compressing objects: 100% (867/867), done.
remote: Total 288989 (delta 12128), reused 12011 (delta 11788), pack-reused 276333
Receiving objects: 100% (288989/288989), 66.96 MiB | 1.39 MiB/s, done.
Resolving deltas: 100% (218191/218191), done.
Checking out files: 100% (3777/3777), done.
Submodule 'sha1collisiondetection' (https://github.com/cr-marcstevens/sha1collisiondetection.git) registered for path 'sha1collisiondetection'
Cloning into '/home/yoichinakayama/ghq/git.kernel.org/pub/scm/git/git/sha1collisiondetection'...
remote: Enumerating objects: 6, done.        
remote: Counting objects: 100% (6/6), done.        
remote: Compressing objects: 100% (6/6), done.        
remote: Total 887 (delta 0), reused 4 (delta 0), pack-reused 881        
Receiving objects: 100% (887/887), 611.42 KiB | 380.00 KiB/s, done.
Resolving deltas: 100% (564/564), done.
Submodule path 'sha1collisiondetection': checked out '855827c583bc30645ba427885caa40c5b81764d2'
yoichinakayama@penguin:~$ ghq list|grep git.kernel.org
git.kernel.org/pub/scm/git/git.git
git.kernel.org/pub/scm/git/git

更新しようとしたら別ディレクトリに再取得されてしまった。最初に ghq get するときに末尾の / を除いて指定すれば避けられるが、git コマンドだと

yoichinakayama@penguin:~$ git clone https://git.kernel.org/pub/scm/git/git.git/
Cloning into 'git'...

と末尾の .git/ を取り除いたパスに取得するので、ghqでも回避できそう。関連するgitの実装は

https://github.com/git/git/blob/101b3204f37606972b40fc17dec84560c22f69f6/builtin/clone.c#L241

のあたり。

Subversion

Source Code の Checking Out Subversion に書かれている svn co https://svn.apache.org/repos/asf/subversion/trunk subversion にあるURLを使う。

get

yoichinakayama@penguin:~$ ghq get https://svn.apache.org/repos/asf/subversion/trunk
     clone https://svn.apache.org/repos/asf/subversion/trunk -> /home/yoichinakayama/ghq/svn.apache.org/repos/asf/subversion/trunk
       svn checkout https://svn.apache.org/repos/asf/subversion/trunk /home/yoichinakayama/ghq/svn.apache.org/repos/asf/subversion
...
Checked out revision 1879249.

list and update

yoichinakayama@penguin:~$ ghq list|grep subversion
svn.apache.org/repos/asf/subversion
yoichinakayama@penguin:~$ ghq list|grep subversion|ghq get -u
    update /home/yoichinakayama/ghq/svn.apache.org/repos/asf/subversion
       svn update
Updating '.':
At revision 1879249.
yoichinakayama@penguin:~$ 

Mercurial

Mercurial downloads の The main development repository として記載されているURL https://www.mercurial-scm.org/repo/hg を使う。

get

yoichinakayama@penguin:~$ ghq get https://www.mercurial-scm.org/repo/hg
     clone https://www.mercurial-scm.org/repo/hg -> /home/yoichinakayama/ghq/www.mercurial-scm.org/repo/hg
        hg clone https://www.mercurial-scm.org/repo/hg /home/yoichinakayama/ghq/www.mercurial-scm.org/repo/hg
requesting all changes
adding changesets
adding manifests                                                                                                                                                              
adding file changes                                                                                                                                                           
added 45005 changesets with 86775 changes to 3569 files (+1 heads)                                                                                                            
new changesets 9117c6561b0b:2632c1ed8f34
updating to bookmark @
2102 files updated, 0 files merged, 0 files removed, 0 files unresolved     

list and update

yoichinakayama@penguin:~$ ghq list|grep mercurial
www.mercurial-scm.org/repo/hg
yoichinakayama@penguin:~$ ghq list|grep mercurial|ghq get -u
    update /home/yoichinakayama/ghq/www.mercurial-scm.org/repo/hg
        hg pull --update
pulling from https://www.mercurial-scm.org/repo/hg
searching for changes
no changes found

Bazaar

get

Bazaarbzr branch lp:bzr の引数をそのまま指定してみるがうまくいかない。

yoichinakayama@penguin:~$ ghq get lp:bzr
     clone ssh://lp/bzr -> /home/yoichinakayama/ghq/lp/bzr
     error failed to get "lp:bzr": unsupported VCS, url=ssh://lp/bzr: Get ssh://lp/bzr?go-get=1: unsupported protocol scheme "ssh"
yoichinakayama@penguin:~$ ghq get --vcs=bzr lp:bzr
     clone ssh://lp/bzr -> /home/yoichinakayama/ghq/lp/bzr
       bzr branch ssh://lp/bzr /home/yoichinakayama/ghq/lp/bzr
bzr: ERROR: Unsupported protocol for url "ssh://lp/bzr": bzr supports bzr+ssh to operate over ssh, use "bzr+ssh://lp/bzr".
     error failed to get "lp:bzr": /usr/bin/bzr: exit status 3

まずは bzr で取得してみる。

yoichinakayama@penguin:~$ bzr branch lp:bzr
You have not informed bzr of your Launchpad ID, and you must do this to
write to Launchpad or access private data.  See "bzr help launchpad-login".
...
yoichinakayama@penguin:~$ cat bzr/.bzr/branch/branch.conf 
parent_location = http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/

これかな?

yoichinakayama@penguin:~$ ghq get http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/
     clone http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/ -> /home/yoichinakayama/ghq/bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev
     error failed to get "http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/": unsupported VCS, url=http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/: no go-import meta tags detected
yoichinakayama@penguin:~$ ghq get --vcs=bzr http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/
     clone http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/ -> /home/yoichinakayama/ghq/bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev
       bzr branch http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/ /home/yoichinakayama/ghq/bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev
Branched 6622 revisions.

行けた。

list and update

yoichinakayama@penguin:~$ ghq list |grep bzr
bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev
yoichinakayama@penguin:~$ ghq list|grep bzr|ghq get -u
    update /home/yoichinakayama/ghq/bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev
       bzr pull --overwrite
Using saved parent location: http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev/
No revisions or tags to pull.   

Darcs

http://darcs.net/Development
darcs clone --lazy http://darcs.net を参考に

get

yoichinakayama@penguin:~$ ghq get http://darcs.net
     clone http://darcs.net -> /home/yoichinakayama/ghq/darcs.net
     error failed to get "http://darcs.net": unsupported VCS, url=http://darcs.net: no go-import meta tags detected

自動判定は失敗する

yoichinakayama@penguin:~$ ghq get --vcs=darcs http://darcs.net
     clone http://darcs.net -> /home/yoichinakayama/ghq/darcs.net
     darcs get http://darcs.net /home/yoichinakayama/ghq/darcs.net
Welcome to the darcs screened repository.

If you would like to contribute, please read our guide for contributors:
http://darcs.net/Development/GettingStarted

Thanks and happy hacking!
**********************
Copying patches, to get lazy repository hit ctrl-C...                           
^CUsing lazy repository.

Finished cloning.  

待ちきれなくて ctrl-C で止めたけど、待ってればいつか終わったのかな。ghqの実装を見ると、 --shallow オプションを付けると --lazy をつけて darcs clone するようだ。

list and update

yoichinakayama@penguin:~$ ghq list|grep darcs.net
darcs.net
yoichinakayama@penguin:~$ ghq list|grep darcs.net|ghq get -u
     clone https://github.com/yoichi/darcs.net -> /home/yoichinakayama/ghq/github.com/yoichi/darcs.net
       git clone --recursive https://github.com/yoichi/darcs.net /home/yoichinakayama/ghq/github.com/yoichi/darcs.net
Cloning into '/home/yoichinakayama/ghq/github.com/yoichi/darcs.net'...
Username for 'https://github.com': ^C

階層構造がないのでプロジェクト名と解釈されてしまっている。

Development/GettingStarted に書かれている http://darcs.net/releases/branch-2.12 とかだと大丈夫

yoichinakayama@penguin:~$ rm -rf ~/ghq/darcs.net
yoichinakayama@penguin:~$ ghq get --shallow --vcs=darcs http://darcs.net/releases/branch-2.12
     clone http://darcs.net/releases/branch-2.12 -> /home/yoichinakayama/ghq/darcs.net/releases/branch-2.12
     darcs get --lazy http://darcs.net/releases/branch-2.12 /home/yoichinakayama/ghq/darcs.net/releases/branch-2.12
Finished cloning.                                                               
yoichinakayama@penguin:~$ ghq list|grep darcs.net
darcs.net/releases/branch-2.12
yoichinakayama@penguin:~$ ghq list|grep darcs.net|ghq get -u
    update /home/yoichinakayama/ghq/darcs.net/releases/branch-2.12
     darcs pull
Pulling from "http://darcs.net/releases/branch-2.12"...
No remote patches to pull in!

Fossil

Fossil Self-Hosting Repositories の three publicly accessible repositories for the Fossil source code の一番上の https://www.fossil-scm.org/ を使う

get

yoichinakayama@penguin:~$ ghq get https://www.fossil-scm.org/
     clone https://www.fossil-scm.org/ -> /home/yoichinakayama/ghq/www.fossil-scm.org
     error failed to get "https://www.fossil-scm.org/": unsupported VCS, url=https://www.fossil-scm.org/: no go-import meta tags detected

自動判定できないので、vcsを明示的に指定する。

yoichinakayama@penguin:~$ ghq get --vcs=fossil https://www.fossil-scm.org/
     clone https://www.fossil-scm.org/ -> /home/yoichinakayama/ghq/www.fossil-scm.org
    fossil clone https://www.fossil-scm.org/ /home/yoichinakayama/ghq/www.fossil-scm.org/.fossil
Round-trips: 8   Artifacts sent: 0  received: 47004
Clone done, sent: 2102  received: 34054505  ip: 2.0.1.187
...
project-name: Fossil
repository:   /home/yoichinakayama/ghq/www.fossil-scm.org/.fossil
local-root:   /home/yoichinakayama/ghq/www.fossil-scm.org/
config-db:    /home/yoichinakayama/.fossil
project-code: CE59BB9F186226D80E49D1FA2DB29F935CCA0333
checkout:     cd061779d2c192c239e1eb6d0e9254d8193ffa7b 2020-06-27 17:05:41 UTC
parent:       9ef2e5e57b5db1f32141eff5d5aec0c96dee83d5 2020-06-27 15:51:45 UTC
child:        ff735265175830b0073804b395b2f90e6f0869a5 2020-06-27 17:15:31 UTC
tags:         trunk
comment:      Typos in the help text and the change log. (user: drh)
check-ins:    13965

list and update

yoichinakayama@penguin:~$ ghq list|grep fossil
www.fossil-scm.org
yoichinakayama@penguin:~$ ghq list|grep fossil|ghq get -u
     clone https://github.com/yoichi/www.fossil-scm.org -> /home/yoichinakayama/ghq/github.com/yoichi/www.fossil-scm.org
       git clone --recursive https://github.com/yoichi/www.fossil-scm.org /home/yoichinakayama/ghq/github.com/yoichi/www.fossil-scm.org
Cloning into '/home/yoichinakayama/ghq/github.com/yoichi/www.fossil-scm.org'...
Username for 'https://github.com': ^C

階層構造がないのでプロジェクト名と解釈されてしまっている。

yoichinakayama@penguin:~$ curl -v https://www.fossil-scm.org/
...
< HTTP/1.1 301 Permanent Redirect
< Connection: keep-alive
< Date: Sun, 28 Jun 2020 01:20:13 +0000
< Location: https://www.fossil-scm.org/home
< Content-length: 0
< 
* Curl_http_done: called premature == 0
* Connection #0 to host www.fossil-scm.org left intact

リダイレクトされてたのでそちらのURLで取得し直せば問題ない

yoichinakayama@penguin:~$ rm -rf ~/ghq/www.fossil-scm.org
yoichinakayama@penguin:~$ ghq get https://www.fossil-scm.org/home
     clone https://www.fossil-scm.org/home -> /home/yoichinakayama/ghq/www.fossil-scm.org/home
     error failed to get "https://www.fossil-scm.org/home": unsupported VCS, url=https://www.fossil-scm.org/home: no go-import meta tags detected
yoichinakayama@penguin:~$ ghq get --vcs=fossil https://www.fossil-scm.org/home
     clone https://www.fossil-scm.org/home -> /home/yoichinakayama/ghq/www.fossil-scm.org/home
    fossil clone https://www.fossil-scm.org/home /home/yoichinakayama/ghq/www.fossil-scm.org/home/.fossil
...
project-name: Fossil
repository:   /home/yoichinakayama/ghq/www.fossil-scm.org/home/.fossil
local-root:   /home/yoichinakayama/ghq/www.fossil-scm.org/home/
config-db:    /home/yoichinakayama/.fossil
project-code: CE59BB9F186226D80E49D1FA2DB29F935CCA0333
checkout:     cd061779d2c192c239e1eb6d0e9254d8193ffa7b 2020-06-27 17:05:41 UTC
parent:       9ef2e5e57b5db1f32141eff5d5aec0c96dee83d5 2020-06-27 15:51:45 UTC
child:        ff735265175830b0073804b395b2f90e6f0869a5 2020-06-27 17:15:31 UTC
tags:         trunk
comment:      Typos in the help text and the change log. (user: drh)
check-ins:    13965
yoichinakayama@penguin:~$ ghq list|grep fossil
www.fossil-scm.org/home
yoichinakayama@penguin:~$ ghq list|grep fossil|ghq get -u
    update /home/yoichinakayama/ghq/www.fossil-scm.org/home
    fossil update
Autosync:  https://www.fossil-scm.org/home
Round-trips: 1   Artifacts sent: 0  received: 0
Pull done, sent: 424  received: 1453  ip: 2.0.1.187
-------------------------------------------------------------------------------
checkout:     cd061779d2c192c239e1eb6d0e9254d8193ffa7b 2020-06-27 17:05:41 UTC
tags:         trunk
comment:      Typos in the help text and the change log. (user: drh)
changes:      None. Already up-to-date

CVS

Concurrent Versions System - CVS Repositoriescvs -z3 -d:pserver:anonymous@cvs.savannah.nongnu.org:/sources/cvs co <modulename> でソースを取得できる。Browse Sources Repositoryのリンク先からmodulename=ccvsを指定すればいいのだけど、ghqでは対応していない

Add a dummy CVS backend to recognize and skip CVS working directories #115

動作確認しておく。

yoichinakayama@penguin:~$ mkdir -p ghq/cvs.savannah.nongnu.org/sources/cvs
yoichinakayama@penguin:~$ cd $_
yoichinakayama@penguin:~/ghq/cvs.savannah.nongnu.org/sources/cvs$ cvs -z3 -d:pserver:anonymous@cvs.savannah.nongnu.org:/sources/cvs co ccvs
...
yoichinakayama@penguin:~$ ghq list|grep ccvs
cvs.savannah.nongnu.org/sources/cvs/ccvs
yoichinakayama@penguin:~$ ghq list|grep ccvs|ghq get -u
    update /home/yoichinakayama/ghq/cvs.savannah.nongnu.org/sources/cvs/ccvs
     error failed to get "cvs.savannah.nongnu.org/sources/cvs/ccvs": CVS update is not supported

考察

VCSの自動判定ができないものがあった。

  • Bazaar
  • Darcs
  • Fossil

ghq getで指定するURLが悩ましいものがあった。

  • Bazaar の lp:bzr みたいなの
    • →vcs固有のURLの推定ができればいいのかな
  • CVS (対応してないけど)

ghq getで取得されたものに対し、ghq list|ghq get -uがうまく動作しない場合があった。

  • URL末尾に / があるため、末尾の .git が取り除かれない場合
    • →末尾の / を取り除く処理を入れればよいかな
  • 階層構造にならないため、プロジェクト名指定と解釈されてしまう場合
    • →ghq getのときに階層構造を作ればよいかな
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Git/GitHubの基本用語

プログラミング初心者が
Git/GitHubに関する基本用語をアウトプット用に記述したものです。

Git/GitHubの基本用語

1.リポジトリ
Gitの管理化にあるファイルやディレクトリの変更履歴を保管しておく入れ物。

2.ローカルリポジトリ
自分のPC上(ローカル環境)に置くリポジトリのこと。
自分のPC上にあるファイルやディレクトリのバージョン管理をしたい場合に使う。

3.リモートリポジトリ
外部のサーバなどのネットワーク上に置くリポジトリのこと。
複数人で管理化のファイルやディレクトリを共有することができる。

4.git initコマンド
新たにGitで管理したいディレクトリで実行するとGitで管理できるようになる。

5.インデックス
パージョンを記録するためにファイルを一時的に登録する場所。
同じバージョンとして記録したい編集についてはまとめてインデックスに追加する。

6.git statusコマンド
インデックスに追加されている変更修正、されていない変更修正を確認することができる。

7.git addコマンド
インデックスに追加して、変更修正記録の対象とすることができる。

8.コミット
インデックスに追加された変更修正をバージョン記録する操作のこと。
git commitコマンドでコミットすることができる。
-mオプションはコミットメッセージをつけられる。

9.ログ
コミットの履歴のこと。
git logでログを表示することができる。

10.GitHub
Gitにおけるリモートリポジトリの役割を担う。
世界中のあらゆるプロダクトが保存、公開されている。
グラフィカルな画面からGitを扱うことができる。
チーム開発に便利な様々な機能が用意されている。

11.git remote addコマンド
ローカルリポジトリにリモートリポジトリの情報を付与して紐付けを行う。

12.origin
リモートリポジトリの場所の別名のこと。
一般的にGitHubのリモートリポジトリはoriginという名前をつける。

13.git pushコマンド
ローカルリポジトリでのコミットをリモートリポジトリに反映させる。
プッシュする。

14.GitHub Desktop
デスクトップ用のアプリケーション。
本来コンソールで行うGitHubに関する作業がグラフィカルにできる。

15.ブランチ
リポジトリで管理しているプロジェクトの流れを記録していく場。

16.masterブランチ
リポジトリに最初のコミットを行うと自動で作成される大元のブランチ。

17.トピックブランチ
masterブランチからコピーされたブランチ。
機能ごとにトピックブランチを作成していく。

18.プルリクエスト
ブランチでのコミットの変更修正履歴にコメントをつけることができる機能。
[WIP]、WhatとWhy、マークダウン記法がポイント。

19.コードレビュー
開発工程において見過ごされた誤りを検出・修正すること。
コードの質を上げることを目的としてソースコードの検査を行うこと。

20.LGTM
Looks good to meの略。
「コードに問題がないのでマージしていいですよ」という慣習。

21.マージ
機能実装のために作成したトピックブランチをmasterブランチに統合する作業のこと。

22.プル
リモートリポジトリの変更をローカルリポジトリに取り込む操作のこと。

23.git cloneコマンド
originのURLを指定することでアプリケーションのローカルリポジトリとしてダウンロードすることができる。

24.GitHub flow
GitHubが推奨する開発フローのこと。
GitHub Flow

25.デプロイ
アプリケーションをサーバ上で利用可能な状態にすること。

26.git revertコマンド
コミットを打ち消すコミットを生成するコマンド。
誤りであるコミットを打ち消したコミットの記録が残る。

27.Dependabot
脆弱性を解消するためのバージョン更新用ブランチとプルリクエストが自動生成される機能。

28.コーディング規約
言語やフレームワーク、もしくはチームごとに存在するコードの書き方のルール。
コード全体を統一した記述形式にすることで、可読性を向上させる。
変数のスコープの範囲など、実装上のルールを設けることで、潜在的なバグのリスクを減少させる。

29.リファクタリング
コードに冗長な部分がないかを確認する作業。

まとめ

Git/GitHubに関する用語をまとめてみました。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む