Giải quyết xung đột hợp nhất với Git (PHẦN 4) -

Giải quyết xung đột hợp nhất với Git (PHẦN 4) -

Giải quyết xung đột hợp nhất với Git (PHẦN 4) -

Giải quyết xung đột hợp nhất với Git (PHẦN 4) -

Giải quyết xung đột hợp nhất với Git (PHẦN 4) -
Giải quyết xung đột hợp nhất với Git (PHẦN 4) -
(028) 35124257 - 0933 427 079

Giải quyết xung đột hợp nhất với Git (PHẦN 4)

04-01-2022

Hợp nhất các branch và quan sát kết hợp tự động không chính xác.

Trong hoạt động này, bạn sẽ lại hợp nhất hai branch. Branch tái cấu trúc (refactor branch) vẫn giữ nguyên chức năng nhưng đơn giản hóa code một chút.

Bước 21

So sánh masterrefactor branches.

student@student-workstation:~/working_directory$ git diff master origin/refactor

diff --git a/calculator.py b/calculator.py

index 4e1e513..71982e8 100644

--- a/calculator.py

+++ b/calculator.py

@@ -13,6 +13,21 @@ class IPCalculator:

     def __init__(self, network):

         self.network = ipaddress.IPv4Network(network)

 

+    @property

+    def broadcast_address(self):

+        if self.network:

+            return self.network.broadcast_address

+

+    @property

+    def network_address(self):

+        if self.network:

+            return self.network.network_address

+

+    @property

+    def netmask(self):

+        if self.network:

+            return self.network.netmask

+

     def get_network(self):

         """ Returns the network. """

 

@@ -21,40 +36,23 @@ class IPCalculator:

     def get_first_ip(self):

         """ Calculates the first IP address. """

 

-        network_address = self.get_network_address()

-        first_ip = network_address + 1

+        first_ip = list(self.network.hosts())[0]

         return str(first_ip)

 

     def get_last_ip(self):

         """ Calculates the last IP address. """

 

-        broadcast_address = self.get_broadcast_address()

-        last_ip = broadcast_address - 1

+        last_ip = list(self.network.hosts())[-1]

         return str(last_ip)

-    def get_broadcast_address(self):

-        """ Calculates the broadcast address. """

-

-        return self.network.broadcast_address

-

-    def get_network_address(self):

-        """ Calculates the last address. """

-

-        return self.network.network_address

-

-    def get_netmask(self):

-        """ Calculates the netmask. """

-

-        return self.network.netmask

-

     def calculate(self):

         """ Prints the calculation results. """

 

         print('The result of the calculation is:')

         print(f'-> Input network:\t\t{self.get_network()}')

-        print(f'-> Network address:\t\t{self.get_network_address()}')

-        print(f'-> Netmask:\t\t\t{self.get_netmask()}')

-        print(f'-> Broadcast address:\t\t{self.get_broadcast_address()}')

+        print(f'-> Network address:\t\t{self.network_address}')

+        print(f'-> Netmask:\t\t\t{self.netmask}')

+        print(f'-> Broadcast address:\t\t{self.broadcast_address}')

         print(f'-> First IP address:\t\t{self.get_first_ip()}')

         print(f'-> Last IP address:\t\t{self.get_last_ip()}')

 Bạn có thể thấy rằng có khá nhiều thay đổi.

Bước 22

Hợp nhất refactor branch vào master branch.

student@student-workstation:~/working_directory$ git merge origin/refactor -m "Merging the refactor branch into the master branch"

Auto-merging calculator.py

Merge made by the 'recursive' strategy.

 calculator.py | 36 ++++++++++++++++++------------------

 1 file changed, 18 insertions(+), 18 deletions(-)

Hợp nhất thành công.

Bước 23

Chạy trình tính toán với mạng 192.0.0.0/8.

student@student-workstation:~/working_directory$ python calculator.py -n 192.0.0.0/8

The result of the calculation is:

-> Input network:                              192.0.0.0/8

-> Network address:                         192.0.0.0

-> Netmask:                                       255.0.0.0

-> Broadcast address:                       192.255.255.255

Traceback (most recent call last):

  File "calculator.py", line 64, in <module>

    calc.calculate()

  File "calculator.py", line 58, in calculate

    print(f'-> First IP address:\t\t{self.get_first_ip()}')

  File "calculator.py", line 39, in get_first_ip

    network_address = self.get_network_address()

AttributeError: 'IPCalculator' object has no attribute 'get_network_address'

Bạn có thể thấy rằng bạn có một lỗi ngăn cản việc tính toán địa chỉ IP đầu tiên..

Bước 24

Kiểm tra sự khác biệc của masterrefactor branches.

student@student-workstation:~/working_directory$ git diff master origin/refactor

diff --git a/calculator.py b/calculator.py

index 8a438aa..71982e8 100644

--- a/calculator.py

+++ b/calculator.py

@@ -36,15 +36,13 @@ class IPCalculator:

     def get_first_ip(self):

         """ Calculates the first IP address. """

 

-        network_address = self.get_network_address()

-        first_ip = network_address + 1

+        first_ip = list(self.network.hosts())[0]

         return str(first_ip)

 

     def get_last_ip(self):

         """ Calculates the last IP address. """

 

-        broadcast_address = self.get_broadcast_address()

-        last_ip = broadcast_address - 1

+        last_ip = list(self.network.hosts())[-1]

         return str(last_ip)

 

     def calculate(self):

Bạn có thể thấy rằng sự khác biệt là trong phương thức get_first_ip()get_last_ip(). Master branch hiện tại sử dụng code từ fix branch để tính toán địa chỉ IP đầu tiên và cuối cùng, trong khi refactor branch vẫn sử dụng code cũ từ khi refactor branch được tạo. Code trong các fix branch gọi các phương thức get_network_address()get_broadcast_address(). Thuộc tính Python được tạo thay thế cho thuộc tính đó. Vì không có thay đổi nào trong phương thức get_first_ip()get_last_ip() trong refactor branch, Git đã lấy code từ lần commit cuối cùng trong master branch và hợp nhất cả hai branch.

Bạn có thể thấy tầm quan trọng của việc kiểm tra khi hợp nhất các branch của mình. Code có thể bị lỗi nếu bạn không tuân theo các quy trình kiểm tra thích hợp.

 

 

 

 

 

 

 


FORM ĐĂNG KÝ MUA HÀNG
Đặt hàng
icon-cart
0